Hello

I'm currently having a few problems with trying to read some data into an array and then workout the averages of all the data.

I can read in all the data into an array line by line, the problem is I need to work out the averages of the all of the bits of data in individual columns.

For example the data is stored like this in a text file.

[HRData]
97	117	0	-117	0	50
97	117	0	-117	0	50
97	183	0	-117	0	50
94	183	55	-117	34	50
95	210	56	-117	84	4931
95	207	59	-117	113	5438
95	196	57	-117	125	5944
97	248	56	-117	125	5944
98	255	64	-117	141	5432
100	252	64	-117	141	5432
101	248	79	-117	147	5444
102	237	82	-117	146	6456
104	235	82	-117	142	6457
105	247	84	-117	134	6458
106	261	87	-117	146	7220
106	268	87	-117	146	7220
107	281	91	-117	152	7475
108	292	95	-117	161	7733
109	297	98	-117	173	8245
110	301	101	-117	179	7733
111	299	103	-117	186	6966
112	297	106	-117	193	6445
113	294	105	-117	200	6706
115	293	104	-117	190	6703
116	294	103	-117	191	5685
117	297	105	-117	193	5688
118	294	105	-117	193	5432
118	293	103	-117	206	4922
119	293	103	-117	207	4914
120	294	104	-117	201	5678
120	294	104	-117	211	6195
121	292	103	-117	192	5939
121	289	102	-117	198	6712
122	291	102	-117	191	6201
122	288	102	-117	197	6459
123	286	101	-117	198	7478

I want to get the average of all the bits of data from the first column and then the second and so on.

This is the code I'm using to read in the data.

private List<string> GetDataFromFile()
        {
            List<string> list = new List<string>();
            string tag = "[HRData]";
            bool bGetText = false;
            string path = (Chosen_File);
            using (StreamReader sr = new StreamReader(path))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (bGetText)
                        list.Add(line);
                    else if (line == tag)
                        bGetText = true;
                }
            }
            return list;
        }

Any help would be great, thank you.

Recommended Answers

All 5 Replies

If you want to get together the data in individual column, you better create as many arrays (or even better generic lists) as you have columns. When you will read line by line, you put 1st value into 1st column, 2nd value into 2nd column, and so on.
On the end you will have (in your particular case) 6 arrays and you can easily do an average (sum all and divinde with the number of numbers).
As simple as that.

Thank you for the response.

I understand how it could be done now thank you. The problem is that I'm not the most proficient programmer so I'm struggling a bit to get started. Do you have an example that I could work off to help me?

I changed my mind, I will rather use a dictionary. Its even better tool to use, and most important, you will not have n number of Lists (n = undefined).
This is the solutuion for you:

private void GetDataFromFile()
        {
            Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
            string tag = "[HRData]";
            string path = @"C:\1\test26.txt";
            using (StreamReader sr = new StreamReader(path))
            {
                string line;
                int counter;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line != tag)
                    {
                        counter = 1;
                        string[] lineData = line.Split('\t');
                        foreach (string data in lineData)
                        {
                            int number = Convert.ToInt32(data);
                            if (!dic.ContainsKey(counter))
                                dic.Add(counter, new List<int> { number });
                            else
                                dic[counter].Add(number);
                            counter++;
                        }
                    }
                }
            }

            //getting data of sinlge column:
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, List<int>> kvp in dic)
            {
                var sum = kvp.Value.Sum();
                sb.AppendLine("Column " + kvp.Key.ToString() + ": " + sum.ToString());
            }
            MessageBox.Show("This is the result of summing all the columns:\n" + sb.ToString());
        }

I hope I will get a beer now :)

commented: Cheers! Prosit! +9

Your absolutely class at this, I'll edit this for what I need.
I may come crawling back with another problem in the near future.

Thanks again Mitja.

hehe, We`ll be expecting you Moose :)
Dont be afraid to ask anything...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.