C# Transfer data from text file into graphic

I have a text file with data, I want my graphic to use that data

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 3662 Views - Last Post: 14 September 2010 - 05:03 AM Rate Topic: ***** 1 Votes

#1 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

C# Transfer data from text file into graphic

Posted 09 September 2010 - 10:59 AM

Im writting an application used to calculate descriptive statistics and a mass of data contained in a text file, I will use it to construct the corresponding frequency distribution and graph the results using a histogram and a relative frequency polygon. My text file has data like this. I have attached a picture of what my histogram has to be like.

Here are some calculations that I should do with my files :
determine the number of data
determine the smallest value
determine the largest value
determine the extent of data
the arithmetical average
calculate the standard deviation of two sample

Data text file exemple :
22.08788214
98.58476277
90.30419021
66.95541915
71.69797604
88.34645813
24.89229343
79.37111899

I have done research and found nothing on getting my data calculated. Any good mathematician ? Thanks

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: C# Transfer data from text file into graphic

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • Posts: 548
  • Joined: 07-July 10

Re: C# Transfer data from text file into graphic

Posted 09 September 2010 - 12:48 PM

I'm not a mathamatician, I only play one in my head. :online2long: But here is some pseudo code to help you along.

greenwood said:

Here are some calculations that I should do with my files :
determine the number of data

Read in the data and store as a list. (I like StreamReader). listName.Count() will give you the number of datapoints. (Please remember that you will be reading the items in as a string. You can either convert them after reading them all, or convert them as they come it).

Quote

determine the smallest value
determine the largest value

Create variables min and max. Use foreach to loop through the data. If the value is less than min, set value for the min. If the value is greater than max, set value for the max. After the loop you'll have your min and max.

Quote

the arithmetical average

Create variable sum. Use foreach to loop through the data. Add the value to sum. Divide sum by the number of items in the list.

Quote

determine the extent of data
calculate the standard deviation of two sample

I don't know the math to help you on this one. But hopefully you know the math and can use the above steps to figure it out. Just think of how you would do it, step by step. Then convert those steps into code.
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C# Transfer data from text file into graphic

Posted 09 September 2010 - 08:18 PM

I wrote a function to calculate the standard deviation of a List<double> data set. The comments should explain the process.
http://en.wikipedia....ndard_deviation

I'm not sure what extent of data means though. Is is the range of values from min to max?

Function:
        static double calculateStdDeviation(List<double> dataset)
        {
            double average = 0.0;

            // Calculate mean
            foreach (double d in dataset)
            {
                average += d;
            }

            average /= (double)dataset.Count;

            // Calculate standard deviation

            // First get all values (value - average)^2
            List<double> stdDev = new List<double>();
            foreach (double d in dataset)
            {
                stdDev.Add(Math.Pow((d - average), 2));
            }

            // Sum of all values, divide by the average and take sqrt to get
            // standard deviation
            double standardDeviation = Math.Sqrt((stdDev.Sum() / average));

            return standardDeviation;
        }

This post has been edited by eker676: 09 September 2010 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 12 September 2010 - 01:47 PM

Yes the extent of data is the reference of the range between the min and max of my data on file.
Was This Post Helpful? 0
  • +
  • -

#5 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 12:46 PM

View Posteker676, on 09 September 2010 - 07:18 PM, said:

I wrote a function to calculate the standard deviation of a List<double> data set. The comments should explain the process.
http://en.wikipedia....ndard_deviation

I'm not sure what extent of data means though. Is is the range of values from min to max?

Function:
        static double calculateStdDeviation(List<double> dataset)
        {
            double average = 0.0;

            // Calculate mean
            foreach (double d in dataset)
            {
                average += d;
            }

            average /= (double)dataset.Count;

            // Calculate standard deviation

            // First get all values (value - average)^2
            List<double> stdDev = new List<double>();
            foreach (double d in dataset)
            {
                stdDev.Add(Math.Pow((d - average), 2));
            }

            // Sum of all values, divide by the average and take sqrt to get
            // standard deviation
            double standardDeviation = Math.Sqrt((stdDev.Sum() / average));

            return standardDeviation;
        }


Thanks for the snip, but if I wanted to show the result(return standardDeviation) in a textBox or MessageBox, I tried to but gave me some errors.
Was This Post Helpful? -1
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4439
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 12:51 PM

Quote

I tried to but gave me some errors.


Sentences like this are some of the most frustrating I ever see on forums. You got "errors" but don't bother to tell us what they are?

That's like going to your mechanic and saying "there's something wrong with my car" or writing an essay for a class and your professor saying "there were problems with your paper" and not telling you what they were.
Was This Post Helpful? 2
  • +
  • -

#7 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 12:58 PM

View PostinsertAlias, on 13 September 2010 - 11:51 AM, said:

Quote

I tried to but gave me some errors.


Sentences like this are some of the most frustrating I ever see on forums. You got "errors" but don't bother to tell us what they are?

That's like going to your mechanic and saying "there's something wrong with my car" or writing an essay for a class and your professor saying "there were problems with your paper" and not telling you what they were.


Thanks and really sorry :-) Sorry again will not happen again...
The error message it gave me was ( An object reference is requiered for the non-static field, method, or property...)
After the (return standardDeviation) I have : textBox1.Text += standardDeviation;
And it gives me that error mentionned above.

This post has been edited by greenwood: 13 September 2010 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • Posts: 548
  • Joined: 07-July 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 02:39 PM

It might be because standardDeviation is a double. Try "... += standardDeviation.ToString();" or "... += string.Format("{0}", standardDeviation);"
Was This Post Helpful? 0
  • +
  • -

#9 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 04:39 PM

View PostRobin19, on 13 September 2010 - 01:39 PM, said:

It might be because standardDeviation is a double. Try "... += standardDeviation.ToString();" or "... += string.Format("{0}", standardDeviation);"



It still gives me the same error message, I tried both suggestion...I pasted all the code I have below
please advise. Thanks


namespace test
{
    public partial class Form1 : Form
    {
        List<double> dataValues = new List<double>();
           
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                String nbr;

                while ((nbr = sr.ReadLine()) != null)
                {
                    double data = 0.0;
                    if ((Double.TryParse(nbr, out data)))
                        dataValues.Add(data);
                                        }
                sr.Close();
                fs.Close();       
            }
        }
        
        static double std(List<double> dataValues)
        {
            double average = 0.0;

            foreach (double d in dataValues)
            {
                average += d;
            }

            average /= (double)dataValues.Count;

            //Calculate standard deviation
            List<double> stdDev = new List<double>();
            foreach (double d in dataValues)
            {
                stdDev.Add(Math.Pow((d - average), 2));
            }

            //Sum of all values, divide by the average and take sqrt to get std
            double standardDeviation = Math.Sqrt((stdDev.Sum() / average));

              return standardDeviation;
              textBox1.Text += string.Format("{0}", standardDeviation); 
        }


Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 04:53 PM

return standardDeviation;
textBox1.Text += string.Format("{0}", standardDeviation);

That line after return never happens.

Copy and paste the error exactly as it appears...on what line is it occurring?
Was This Post Helpful? 0
  • +
  • -

#11 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 05:02 PM

View PostJackOfAllTrades, on 13 September 2010 - 03:53 PM, said:

return standardDeviation;
textBox1.Text += string.Format("{0}", standardDeviation);

That line after return never happens.

Copy and paste the error exactly as it appears...on what line is it occurring?



It happens on the texbox1.Text +=....line, whether I use ( string.Format("{0}", standardDeviation) or the ToString it gives me the same error..Attached Image I have attached a print screen of the error.
See picture attached. Thanks
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 05:06 PM

Are you sure your namespace is test, or did you just copy and paste that from my example earler?
Was This Post Helpful? 0
  • +
  • -

#13 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 05:10 PM

View PostJackOfAllTrades, on 13 September 2010 - 04:06 PM, said:

Are you sure your namespace is test, or did you just copy and paste that from my example earler?


No it is test, when I created my project I named it "test" also.
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 05:14 PM

If that's the case, then it appears you do not have a textBox1 member in your Form1 class. If you do, show us the entire file in which it appears.
Was This Post Helpful? 0
  • +
  • -

#15 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Transfer data from text file into graphic

Posted 13 September 2010 - 05:19 PM

View PostJackOfAllTrades, on 13 September 2010 - 04:14 PM, said:

If that's the case, then it appears you do not have a textBox1 member in your Form1 class. If you do, show us the entire file in which it appears.



Here my code from form1.Designer.cs I have attached a screen shot of my form as well.

namespace test
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ofd = new System.Windows.Forms.OpenFileDialog();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fichierToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ouvrirToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statistiqueDescriptiveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.sommeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.moyenneToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // ofd
            // 
            this.ofd.FileName = "openFileDialog1";
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fichierToolStripMenuItem,
            this.statistiqueDescriptiveToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(740, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fichierToolStripMenuItem
            // 
            this.fichierToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ouvrirToolStripMenuItem});
            this.fichierToolStripMenuItem.Name = "fichierToolStripMenuItem";
            this.fichierToolStripMenuItem.Size = new System.Drawing.Size(54, 20);
            this.fichierToolStripMenuItem.Text = "Fichier";
            // 
            // ouvrirToolStripMenuItem
            // 
            this.ouvrirToolStripMenuItem.Name = "ouvrirToolStripMenuItem";
            this.ouvrirToolStripMenuItem.Size = new System.Drawing.Size(107, 22);
            this.ouvrirToolStripMenuItem.Text = "Ouvrir";
            this.ouvrirToolStripMenuItem.Click += new System.EventHandler(this.ouvrirToolStripMenuItem_Click);
            // 
            // statistiqueDescriptiveToolStripMenuItem
            // 
            this.statistiqueDescriptiveToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.sommeToolStripMenuItem,
            this.moyenneToolStripMenuItem});
            this.statistiqueDescriptiveToolStripMenuItem.Name = "statistiqueDescriptiveToolStripMenuItem";
            this.statistiqueDescriptiveToolStripMenuItem.Size = new System.Drawing.Size(135, 20);
            this.statistiqueDescriptiveToolStripMenuItem.Text = "Statistique Descriptive";
            // 
            // sommeToolStripMenuItem
            // 
            this.sommeToolStripMenuItem.Name = "sommeToolStripMenuItem";
            this.sommeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.sommeToolStripMenuItem.Text = "Somme";
            this.sommeToolStripMenuItem.Click += new System.EventHandler(this.sommeToolStripMenuItem_Click);
            // 
            // moyenneToolStripMenuItem
            // 
            this.moyenneToolStripMenuItem.Name = "moyenneToolStripMenuItem";
            this.moyenneToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.moyenneToolStripMenuItem.Text = "Moyenne";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(111, 60);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(517, 230);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(740, 424);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.OpenFileDialog ofd;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fichierToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem ouvrirToolStripMenuItem;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.ToolStripMenuItem statistiqueDescriptiveToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem sommeToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem moyenneToolStripMenuItem;
    }
}




Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2