Std Deviation

I posted a question about a loop in here a few days ago that wouldn't work correctly but now I'm having a bit of trouble with it again. It calculates the averages of each P and T values and outputs them in a similar format to the input file. I also have added calculations for the "averages of the averages" at the bottom. Now I need the standard deviation of those averages. I have tried doing something like
sdtotal = sdtotal + ((progavg - paverage) * (progavg - paverage));
That part would just get the part on top of the formula obviously but when I go to output that part just to see if it calculated correctly it doesn't work. I have also tried typing in the "paverage" variable manually and it comes up with something different. I'm a little confused on how to set this up. I obviously understand the formula but I'm not sure how to implement it. I'm posting the input file and the loop... I can post the output as well, if needed. Thanks so much for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
# Stus		# Progs		# Tests
   10		    3		    3
  ID 		P1	P2	P3	T1	T2	T3
904084117	100	100	100	100	78	86
904254870	95	100	100	0	84	87
904278054	82	95	82	100	56	61
905299003	95	100	90	100	57	78
903191606	100	95	100	90	50	35
906354053	100	95	90	100	87	42
906445013	90	95	100	90	78	67
906729792	97	95	100	100	60	73
900299833	100	100	100	0	90	67
905158302	85	90	91	100	57	49

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
	for (numLoops = 0; numLoops < students; numLoops++) {
		progtotal = 0.0;
		testtotal = 0.0;
		In >> studentid;
		Out << studentid;

		for (progLoops = 0; progLoops < programs; progLoops++) {
			In >> progscore;
			progtotal = progtotal + progscore;
			progavg = floor((progtotal / programs) + 0.5);
		}
		if (progavg < 100.0)
			Out << "      " << progavg; 
		if (progavg >= 100.0)
			Out << "     " << progavg; 

		for (testLoops = 0; testLoops < tests; testLoops++) {
			In >> testscore;
			testtotal = testtotal + testscore;
			testavg = floor((testtotal / tests) + 0.5);
		}
		if (testavg < 100.0)
			Out << "         " << testavg << endl;
		if (testavg >= 100.0)
			Out << "        " << testavg << endl;

		pavtotal = pavtotal + progavg;
		paverage = pavtotal / students;

		tavtotal = tavtotal + testavg;
		taverage = tavtotal / students;	
	}
Obviously one needs to know the average in order to calculate the standard deviation, so the standard deviation calculation would need to come in another loop.

The code you posted above is only the first loop that calculates the average. Can you post the loop where you're calculating the standard deviation?
That's just the thing... I'm not even TO that point yet. I was under the assumption that I could do it in the same loop? Everything I need to calculate it has already been done in this loop. I'm assuming I need to read through the input file again for the other loop is that correct? I'm also guessing that the next loop would need to have the same calculations as this one minus the total average calculations. I'm a little confused about how to set it up. I've tried a couple things but they all produce the same results as when I tried to enter the "sdtotal" variable calculation in the 1st loop. Thanks for the help.
Unless there's some clever way to do it (that I don't know about), you have to put the standard deviation calculation in another loop. If you try to put it in the same loop, the average value will be different for every iteration and it will screw up your standard deviation calculations. Considering the way you've read the data for the first loop, you will have to read through the input file again for the other loop.

Howver, I suggest to follow this approach:

1. Create a Student class (or even a simple struct) with attributes id, p1, p2, p3, t1, t2, t3.
2. In your program, use a vector which will store your unknown number of student objects which you will read from your file. Alternatively you quickly run through the file first and find the number of students and then use an array with dynamically allocated memory.
3. Create the first loop: this would go through the file, creating student objects using the data from each line, and add these student objects to your collection one by one.
4. Create the second loop: this would calculate the averages that you need to know using only your collection of students (i.e. no file reading required for this loop)
5. Create the third loop: this would once again go through the collection of students and use the average info calculated in (4) to calculate the standard deviation information you want.

I suggest to follow that approach. You can combine steps 3 and 4 into one loop if you're feeling adventurous (i.e. populate the collection and calculate the average at the same time).

I hope I explained it clearly enough, and I hope it helps. Let us know how you go!
I'm sure what you said will work but unfortunately I have no idea how to do any of that stuff and I'm trying to keep it simple. Sorry haha. But I will show you specifically what problem I am running into if that will help. Breaking the SD formula down, I tried doing the top half of the formula just for the programs in the following loop but it doesn't come out correctly. This involves subtracting the total average from each individual average and squaring it each time. The answer should be 188.1 but I'm getting 39.69. There's no point in continuing to write code for the rest of the formula if I can't even get that part right. I just don't understand why this code won't calculate that correctly? Also keep in mind... this loop comes AFTER I close the input file so this is reading through it again like you said. Thanks a lot for the help and sorry I'm ignorant when it comes to most of this stuff!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	for (numLoops = 0; numLoops < students; numLoops++) {
		progtotal = 0.0;
		testtotal = 0.0;
		sdtotal = 0.0;
		In >> studentid;

		for (progLoops = 0; progLoops < programs; progLoops++) {
			In >> progscore;
			progtotal = progtotal + progscore;
			progavg = floor((progtotal / programs) + 0.5);
		}
		
		for (testLoops = 0; testLoops < tests; testLoops++) {
			In >> testscore;
			testtotal = testtotal + testscore;
			testavg = floor((testtotal / tests) + 0.5);
		}

		sdtotal = sdtotal + ((progavg - paverage) * (progavg - paverage));
	}
Hmm so I was playing around with some stuff and I removed the "sdtotal = 0.0" and now I'm getting the correct answer! I'm good for now but thank you for all your help!
Topic archived. No new replies allowed.