How do I average all the numbers in a random number generator string?

I need to find the average of all "CHUMP'S" thickness. I feel like it's really simple, but I just don't know.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma endregion 
				 // Instance Variables
				 Random^ randomNumGen;
				 String^ strLog;

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 DateTime now = DateTime::Now; //Used as seed value
				 randomNumGen = gcnew Random(now.Millisecond);
				 strLog = gcnew String ("Item\t\tThickness\r\n");
				 txtItems->Text = "100";
			 }

private: System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 int items;
				 double thick;

				 Int32::TryParse (txtItems->Text, items);
				 for (int i = 0; i<items; i++)
				 {
					 thick = randomNumGen->Next(0,1000) / 100.0;
					 strLog += "CHUMP  " + i + ":\t" + thick + " mm\r\n";
				 }
				 txtLog->Text = strLog;
			 }
};
}
bump
Still struggling with this problem.
You cannot compute the average without knowing all data points.

Instead, sum up all the values and then divide by how many you generated.
Really appreciate the response.
How would I sum up all the values?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma endregion
				 
				 // Instance Variables
				 Random^ randomNumGen;
				 String^ strLog;

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 DateTime now = DateTime::Now; //Used as seed value
				 randomNumGen = gcnew Random(now.Millisecond);
				 strLog = gcnew String ("Item\t\tThickness\r\n");
				 txtItems->Text = "100";
			 }

private: System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 int items;
				 double thick;
				 int sum;
				 double average;

				 Int32::TryParse (txtItems->Text, items);
				 for (int i = 0; i<items; i++)
				 {
					 thick = randomNumGen->Next(0,1000) / 100.0;
					 for (int counter = 1; counter<= items; counter++)
						 sum += counter;
					 strLog += "CHUMP  " + i + ":\t" + thick + " mm\r\n";
				 }
				 average = sum / items;
				 txtAverage->Text = average.ToString();
				 txtLog->Text = strLog;
			 }
};
}
I just don't understand how to count values & add them to sum while the random numbers are being generated.
Update. I'm beginning to understand the concept. Still not working though..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private: System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 int items;
				 double thick;
				 double average;
				 double sum = 0;

				 Int32::TryParse (txtItems->Text, items);
				 for (int i = 0; i<items; i++)
				 {
					 thick = randomNumGen->Next(0,1000) / 100.0;
					 strLog += "CHUMP  " + i + ":\t" + thick + " mm\r\n";
					 for (int counter = 1; counter < itmes; counter++)
						 sum += thick;
				 }
				 average = sum / items;
				 txtAverage->Text = average.ToString();
				 txtLog->Text = strLog;
			 }
};
}
Last edited on
I'M SO CLOSE! I KNOW I'M JUST MISSING ONE THING AND I CAN'T FIGURE IT OUT.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private: System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 int items;
				 double thick;
				 double average;
				 double sum;

				 Int32::TryParse (txtItems->Text, items);
				 for (int i = 0; i<items; i++)
				 {
					 thick = randomNumGen->Next(0,1000) / 100.0;
					 strLog += "CHUMP  " + i + ":\t" + thick + " mm\r\n";
					 for (int counter = 0; counter <= i; counter++)
						 sum += thick;
				 }
				 average = sum / items;
				 txtAverage->Text = average.ToString();
				 txtLog->Text = strLog;
			 }
};
}
WHAT'S WRONG WITH MY LOGIC?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma endregion
				 
				 // Instance Variables
				 Random^ randomNumGen;
				 String^ strLog;
				 int items;
				 double sum;

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
			 {
				 DateTime now = DateTime::Now; //Used as seed value
				 randomNumGen = gcnew Random(now.Millisecond);
				 strLog = gcnew String ("Item\t\tThickness\r\n");
				 txtItems->Text = "100";
			 }

private: System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 int number;
				 double thick;
				 double average;

				 Int32::TryParse (txtItems->Text, number);
				 for (int i = 1; i <= number; i++)
				 {
					 thick = randomNumGen->Next(0,1000) / 100.0;
					 strLog += "CHUMP  " + i + ":\t" + thick + " mm\r\n";
					 for (int counter = 1; counter <= i; counter++)
						 sum += thick / 100.0;
				 }
				 average = sum / items;
				 txtAverage->Text = average.ToString();
				 txtLog->Text = strLog;
				 items += number;
			 }
};
}
Replace this:
28
29
					 for (int counter = 1; counter <= i; counter++)
						 sum += thick / 100.0;
with this:
28
29
					 //no idea what the for-loop was for
						 sum += thick; //what was the divide by 100.0 for? 
Topic archived. No new replies allowed.