help with loops. (or maybe understanding problem.)

my teacher gave us an assignment to alter a previous program with loops. i cant seem to get what i am supposed to do. can someone help me to understand?

here are his instructions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Convert your solution to previous assignment into
                 a program with loops.



REQUIREMENTS for Statistics (mandatory):

   0. Data values are of type float.

   1. Program must require user to enter the number of values,
      and must prompt user for each value.

      Input prompt 1: "Enter number of values: "
      Input prompt 2: "Enter next value: "

   2. Your program must avoid dividing by zero.

   3. Your program should work with these test cases:

      3  10.0 10.10 10.2 
      0
      -1 
      7  1 2 3 4 50 60 70 

and here was my solution to the first program:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//-| ---------------------------------------------------------------
//-| Assignment ID: PROG5
//-| File name:     curve.cpp  
//-| Due:           Monday, Feb 27 at 12 pm
//-| Author: bfields Byron Fields
//-| Description:   Extend the program statistics.cpp to curve the grades on
//-|                ten exam scores. 
//-| ---------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;            

int main ()
{


//-| --------------------------------------------------
//-| Declare Variables
//-| --------------------------------------------------
float SumX;
float SumXsq;
int TestScore1;
int TestScore2;
int TestScore3;
int TestScore4;
int TestScore5;
int TestScore6;
int TestScore7;
int TestScore8;
int TestScore9;
int TestScore10;
float Xavg;
float Xvar;
float Xstd;
float C_lowerbound = Xavg - 1/2 * Xstd;
float D_lowerbound = Xavg - 3/2 * Xstd;
float B_lowerbound = Xavg + 1/2 * Xstd;
float A_lowerbound = Xavg + 3/2 * Xstd;
//-| --------------------------------------------------
//-| 1. Initialize accumulators, SumX and SumXsq, to 0.
//-| --------------------------------------------------
SumX = 0;
SumXsq = 0;
//-| --------------------------------------------------
//-| 2. Repeat 10 times
//-|     2.a) Read a Score
//-|     2.b) Add Score to SumX.
//-|     2.c) Add Score*Score to SumXsq.
//-| --------------------------------------------------
cin >> TestScore1;
SumX = SumX + TestScore1;
SumXsq = SumXsq + TestScore1*TestScore1;
cin >> TestScore2;
SumX = SumX + TestScore2;
SumXsq = SumXsq + TestScore2*TestScore2;
cin >> TestScore3;
SumX = SumX + TestScore3;
SumXsq = SumXsq + TestScore3*TestScore3;
cin >> TestScore4;
SumX = SumX + TestScore4;
SumXsq = SumXsq + TestScore4*TestScore4;
cin >> TestScore5;
SumX = SumX + TestScore5;
SumXsq = SumXsq + TestScore5*TestScore5;
cin >> TestScore6;
SumX = SumX + TestScore6;
SumXsq = SumXsq + TestScore6*TestScore6;
cin >> TestScore7;
SumX = SumX + TestScore7;
SumXsq = SumXsq + TestScore7*TestScore7;
cin >> TestScore8;
SumX = SumX + TestScore8;
SumXsq = SumXsq + TestScore8*TestScore8;
cin >> TestScore9;
SumX = SumX + TestScore9;
SumXsq = SumXsq + TestScore9*TestScore9;
cin >> TestScore10;
SumX = SumX + TestScore10;
SumXsq = SumXsq + TestScore10*TestScore10;
//-| -------------------------------------------------------
//-| 3. Compute the mean (average) (Avg) using equation (d).
//-|     (d) Avg = SumX / N
//-| -------------------------------------------------------
Xavg = SumX / 10;
//-| -------------------------------------------------------      
//-| 4. Compute variance (Var) using equation (f).
//-| (f) Var = ( SumXsq - 2*Avg*SumX + N*Avg )/N
//-| -------------------------------------------------------
Xvar = ( SumXsq - 2*Xavg*SumX + 10*Xavg )/10;
//-| ----------------------------------------------------------
//-| 5. Compute standard deviation (StdDev) using equation (g).
//-| (g) StdDev = sqrt(Var)
//-| ----------------------------------------------------------
Xstd = sqrt(Xvar);
//-| ----------------------------------------------------------
//-|  6. Display the computed values.
//-|  Outputs:      
//-|               STATISTICAL ANALYSIS
//-|               ====================
//-|               SUM OF VALUES  =    xxxx.x
//-|               SUM OF SQUARES = xxxxxxx.x
//-|               AVERAGE        =    xxx.xx
//-|               VARIANCE       =  xxxxx.xx
//-|               STD DEVIATION  =    xxx.xx
//-|
//-|               ====================
//-| ----------------------------------------------------------
cout << setw(26) << "STATISTICAL ANALYSIS" << endl;
cout << setw(26) << "====================" << endl;
cout << setw(26) << "SUM OF VALUES  =    ";
cout << fixed << setprecision(2) << right << SumX << endl;
cout << setw(23) << "SUM OF SQUARES = ";
cout << fixed << setprecision(2) << SumXsq << endl;
cout << setw(26) << "AVERAGE        =    ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(24) << "VARIANCE       =  ";
cout << fixed << setprecision(2) << right << Xvar << endl;
cout << setw(26) << "STD DEVIATION  =    ";
cout << fixed << setprecision(2) << right << Xstd << endl;
cout << " " << endl;
cout << setw(26) << "====================" << endl;
//-| -----------------------------------------------------------
//-| 8. Compute the lower bound for each letter grade.
//-|          (a) C_lowerbound = Avg - 1/2 * StdDev
//-|          (b) D_lowerbound = Avg - 3/2 * StdDev
//-|          (c) B_lowerbound = Avg + 1/2 * StdDev
//-|          (d) A_lowerbound = Avg + 3/2 * StdDev
//-| -----------------------------------------------------------
C_lowerbound = Xavg - 1/2 * Xstd;
D_lowerbound = Xavg - 3/2 * Xstd;
B_lowerbound = Xavg + 1/2 * Xstd;
A_lowerbound = Xavg + 3/2 * Xstd;
//-| -----------------------------------------------------------
//-| 9. Display the score ranges for each letter grade.
//-|              GUIDE FOR CURVING GRADES
//-|                ========================
//-|
//-|              MEAN (AVG)   = xxx.xx
//-|              STD DEV (SD) = xxx.xx
//-|
//-|              Scores for A: aaa.a AND ABOVE
//-|              Scores for B: [ bbb.b - aaa.a ]
//-|              Scores for C: [ ccc.c - bbb.b ]
//-|              Scores for D: [ ddd.d - ccc.c ]
//-|              Scores for F: BELOW ddd.d
//-| -----------------------------------------------------------
cout << setw(22) << "MEAN (AVG)   =  ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(23) << "STD DEV (SD) =   ";
cout << fixed << setprecision(2) << Xstd << endl;
cout << endl;
cout << setw(20) << "Scores for A: ";
cout << fixed << setprecision(2) << right << A_lowerbound <<" AND ABOVE" << endl;
cout << setw(21) << "Scores for B: [";
cout << fixed << setprecision(2) << right << B_lowerbound << " -  " << A_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for C: [";
cout << fixed << setprecision(2) << right << C_lowerbound << " -  " << B_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for D: [";
cout << fixed << setprecision(2) << right << D_lowerbound << " -  " << C_lowerbound << "  ]" << endl;
cout << setw(26) << "Scores for F: BELOW ";
cout << fixed << setprecision(2) << right << D_lowerbound << endl;
//-| -----------------------------------------------------------
//-| Print Out Copyright
//-| -----------------------------------------------------------
cout << "(c) 2012, bfields Byron Fields" << endl;

   return 0;
}
It seems your teacher wants you to shorten this and make the number of test scores variable using a for loop and check for valid input using a while loop.

For example, if I wanted to get some number, n, of strings, I might have:
1
2
3
4
5
6
7
8
9
unsigned int n = 0;
while (n<1) {
  std::cin>>nX;
  n = (unsigned int)std::atof(nX);
}
std::string stuff[n]
for (unsigned int i = 0; i<n; i++) {
  std::cin>>stuff[i];
}
thanks man. you just made something click in my brain.
can anyone tell me why i keep getting these errors?:
test.cpp: In function `int main()':
test.cpp:46: parse error before `)' token
test.cpp:52: parse error before `;' token
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-| ---------------------------------------------------------------
//-| Assignment ID: PROG5
//-| File name:     curve.cpp  
//-| Due:           Monday, Feb 27 at 12 pm
//-| Author: bfields Byron Fields
//-| Description:   Extend the program statistics.cpp to curve the grades on
//-|                ten exam scores. 
//-| ---------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;            

int main ()
{


//-| --------------------------------------------------
//-| Declare Variables
//-| --------------------------------------------------
float SumX;
float SumXsq;
float TestScore;
float Xavg;
float Xvar;
float Xstd;
float C_lowerbound = Xavg - 1/2 * Xstd;
float D_lowerbound = Xavg - 3/2 * Xstd;
float B_lowerbound = Xavg + 1/2 * Xstd;
float A_lowerbound = Xavg + 3/2 * Xstd;
float Rep;
float X;
//-| --------------------------------------------------
//-| 1. Initialize accumulators, SumX and SumXsq, to 0.
//-| --------------------------------------------------
SumX = 0;
SumXsq = 0;
//-| --------------------------------------------------
//-| 2. Repeat 10 times
//-|     2.a) Read a Score
//-|     2.b) Add Score to SumX.
//-|     2.c) Add Score*Score to SumXsq.
//-| --------------------------------------------------
cout << "Enter number of values: " << endl;
  cin >> X;
for(Rep == X)
{
 
while (Rep > 0) 
{ cout << "Enter next value: " << endl;
cin >> TestScore;
 SumX = SumX + TestScore;
 SumXsq = SumXsq + TestScore*TestScore;
}
}
//-| -------------------------------------------------------
//-| 3. Compute the mean (average) (Avg) using equation (d).
//-|     (d) Avg = SumX / N
//-| -------------------------------------------------------
Xavg = SumX / 10;
//-| -------------------------------------------------------      
//-| 4. Compute variance (Var) using equation (f).
//-| (f) Var = ( SumXsq - 2*Avg*SumX + N*Avg )/N
//-| -------------------------------------------------------
Xvar = ( SumXsq - 2*Xavg*SumX + 10*Xavg )/10;
//-| ----------------------------------------------------------
//-| 5. Compute standard deviation (StdDev) using equation (g).
//-| (g) StdDev = sqrt(Var)
//-| ----------------------------------------------------------
Xstd = sqrt(Xvar);
//-| ----------------------------------------------------------
//-|  6. Display the computed values.
//-|  Outputs:      
//-|               STATISTICAL ANALYSIS
//-|               ====================
//-|               SUM OF VALUES  =    xxxx.x
//-|               SUM OF SQUARES = xxxxxxx.x
//-|               AVERAGE        =    xxx.xx
//-|               VARIANCE       =  xxxxx.xx
//-|               STD DEVIATION  =    xxx.xx
//-|
//-|               ====================
//-| ----------------------------------------------------------
cout << setw(26) << "STATISTICAL ANALYSIS" << endl;
cout << setw(26) << "====================" << endl;
cout << setw(26) << "SUM OF VALUES  =    ";
cout << fixed << setprecision(2) << right << SumX << endl;
cout << setw(23) << "SUM OF SQUARES = ";
cout << fixed << setprecision(2) << SumXsq << endl;
cout << setw(26) << "AVERAGE        =    ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(24) << "VARIANCE       =  ";
cout << fixed << setprecision(2) << right << Xvar << endl;
cout << setw(26) << "STD DEVIATION  =    ";
cout << fixed << setprecision(2) << right << Xstd << endl;
cout << " " << endl;
cout << setw(26) << "====================" << endl;
//-| -----------------------------------------------------------
//-| 8. Compute the lower bound for each letter grade.
//-|          (a) C_lowerbound = Avg - 1/2 * StdDev
//-|          (b) D_lowerbound = Avg - 3/2 * StdDev
//-|          (c) B_lowerbound = Avg + 1/2 * StdDev
//-|          (d) A_lowerbound = Avg + 3/2 * StdDev
//-| -----------------------------------------------------------
C_lowerbound = Xavg - 1/2 * Xstd;
D_lowerbound = Xavg - 3/2 * Xstd;
B_lowerbound = Xavg + 1/2 * Xstd;
A_lowerbound = Xavg + 3/2 * Xstd;
//-| -----------------------------------------------------------
//-| 9. Display the score ranges for each letter grade.
//-|              GUIDE FOR CURVING GRADES
//-|                ========================
//-|
//-|              MEAN (AVG)   = xxx.xx
//-|              STD DEV (SD) = xxx.xx
//-|
//-|              Scores for A: aaa.a AND ABOVE
//-|              Scores for B: [ bbb.b - aaa.a ]
//-|              Scores for C: [ ccc.c - bbb.b ]
//-|              Scores for D: [ ddd.d - ccc.c ]
//-|              Scores for F: BELOW ddd.d
//-| -----------------------------------------------------------
cout << setw(22) << "MEAN (AVG)   =  ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(23) << "STD DEV (SD) =   ";
cout << fixed << setprecision(2) << Xstd << endl;
cout << endl;
cout << setw(20) << "Scores for A: ";
cout << fixed << setprecision(2) << right << A_lowerbound <<" AND ABOVE" << endl;
cout << setw(21) << "Scores for B: [";
cout << fixed << setprecision(2) << right << B_lowerbound << " -  " << A_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for C: [";
cout << fixed << setprecision(2) << right << C_lowerbound << " -  " << B_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for D: [";
cout << fixed << setprecision(2) << right << D_lowerbound << " -  " << C_lowerbound << "  ]" << endl;
cout << setw(26) << "Scores for F: BELOW ";
cout << fixed << setprecision(2) << right << D_lowerbound << endl;
//-| -----------------------------------------------------------
//-| Print Out Copyright
//-| -----------------------------------------------------------
cout << "(c) 2012, bfields Byron Fields" << endl;

   return 0;
}
got it to compile but not getting the desired results:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-| ---------------------------------------------------------------
//-| Assignment ID: PROG5
//-| File name:     curve.cpp  
//-| Due:           Monday, Feb 27 at 12 pm
//-| Author: bfields Byron Fields
//-| Description:   Extend the program statistics.cpp to curve the grades on
//-|                ten exam scores. 
//-| ---------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;            

int main ()
{


//-| --------------------------------------------------
//-| Declare Variables
//-| --------------------------------------------------
float SumX;
float SumXsq;
float TestScore;
float Xavg;
float Xvar;
float Xstd;
float C_lowerbound = Xavg - 1/2 * Xstd;
float D_lowerbound = Xavg - 3/2 * Xstd;
float B_lowerbound = Xavg + 1/2 * Xstd;
float A_lowerbound = Xavg + 3/2 * Xstd;
float Rep;
float X;
//-| --------------------------------------------------
//-| 1. Initialize accumulators, SumX and SumXsq, to 0.
//-| --------------------------------------------------
SumX = 0;
SumXsq = 0;
//-| --------------------------------------------------
//-| 2. Repeat 10 times
//-|     2.a) Read a Score
//-|     2.b) Add Score to SumX.
//-|     2.c) Add Score*Score to SumXsq.
//-| --------------------------------------------------
cout << "Enter number of values: " << endl;
  cin >> X;
while(Rep == X)
do
{

 cout << "Enter next value: " << endl;
 cin >> TestScore;
 SumX = SumX + TestScore;
 SumXsq = SumXsq + TestScore*TestScore;
}
while(Rep == X);
//-| -------------------------------------------------------
//-| 3. Compute the mean (average) (Avg) using equation (d).
//-|     (d) Avg = SumX / N
//-| -------------------------------------------------------
Xavg = SumX / 10;
//-| -------------------------------------------------------      
//-| 4. Compute variance (Var) using equation (f).
//-| (f) Var = ( SumXsq - 2*Avg*SumX + N*Avg )/N
//-| -------------------------------------------------------
Xvar = ( SumXsq - 2*Xavg*SumX + 10*Xavg )/10;
//-| ----------------------------------------------------------
//-| 5. Compute standard deviation (StdDev) using equation (g).
//-| (g) StdDev = sqrt(Var)
//-| ----------------------------------------------------------
Xstd = sqrt(Xvar);
//-| ----------------------------------------------------------
//-|  6. Display the computed values.
//-|  Outputs:      
//-|               STATISTICAL ANALYSIS
//-|               ====================
//-|               SUM OF VALUES  =    xxxx.x
//-|               SUM OF SQUARES = xxxxxxx.x
//-|               AVERAGE        =    xxx.xx
//-|               VARIANCE       =  xxxxx.xx
//-|               STD DEVIATION  =    xxx.xx
//-|
//-|               ====================
//-| ----------------------------------------------------------
cout << setw(26) << "STATISTICAL ANALYSIS" << endl;
cout << setw(26) << "====================" << endl;
cout << setw(26) << "SUM OF VALUES  =    ";
cout << fixed << setprecision(2) << right << SumX << endl;
cout << setw(23) << "SUM OF SQUARES = ";
cout << fixed << setprecision(2) << SumXsq << endl;
cout << setw(26) << "AVERAGE        =    ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(24) << "VARIANCE       =  ";
cout << fixed << setprecision(2) << right << Xvar << endl;
cout << setw(26) << "STD DEVIATION  =    ";
cout << fixed << setprecision(2) << right << Xstd << endl;
cout << " " << endl;
cout << setw(26) << "====================" << endl;
//-| -----------------------------------------------------------
//-| 8. Compute the lower bound for each letter grade.
//-|          (a) C_lowerbound = Avg - 1/2 * StdDev
//-|          (b) D_lowerbound = Avg - 3/2 * StdDev
//-|          (c) B_lowerbound = Avg + 1/2 * StdDev
//-|          (d) A_lowerbound = Avg + 3/2 * StdDev
//-| -----------------------------------------------------------
C_lowerbound = Xavg - 1/2 * Xstd;
D_lowerbound = Xavg - 3/2 * Xstd;
B_lowerbound = Xavg + 1/2 * Xstd;
A_lowerbound = Xavg + 3/2 * Xstd;
//-| -----------------------------------------------------------
//-| 9. Display the score ranges for each letter grade.
//-|              GUIDE FOR CURVING GRADES
//-|                ========================
//-|
//-|              MEAN (AVG)   = xxx.xx
//-|              STD DEV (SD) = xxx.xx
//-|
//-|              Scores for A: aaa.a AND ABOVE
//-|              Scores for B: [ bbb.b - aaa.a ]
//-|              Scores for C: [ ccc.c - bbb.b ]
//-|              Scores for D: [ ddd.d - ccc.c ]
//-|              Scores for F: BELOW ddd.d
//-| -----------------------------------------------------------
cout << setw(22) << "MEAN (AVG)   =  ";
cout << fixed << setprecision(2) << right << Xavg << endl;
cout << setw(23) << "STD DEV (SD) =   ";
cout << fixed << setprecision(2) << Xstd << endl;
cout << endl;
cout << setw(20) << "Scores for A: ";
cout << fixed << setprecision(2) << right << A_lowerbound <<" AND ABOVE" << endl;
cout << setw(21) << "Scores for B: [";
cout << fixed << setprecision(2) << right << B_lowerbound << " -  " << A_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for C: [";
cout << fixed << setprecision(2) << right << C_lowerbound << " -  " << B_lowerbound << "  ]" << endl;
cout << setw(21) << "Scores for D: [";
cout << fixed << setprecision(2) << right << D_lowerbound << " -  " << C_lowerbound << "  ]" << endl;
cout << setw(26) << "Scores for F: BELOW ";
cout << fixed << setprecision(2) << right << D_lowerbound << endl;
//-| -----------------------------------------------------------
//-| Print Out Copyright
//-| -----------------------------------------------------------
cout << "(c) 2012, bfields Byron Fields" << endl;

   return 0;
}
1
2
3
4
5
6
7
8
9
10
while(Rep == X) // What is the value of Rep here??
do
{

 cout << "Enter next value: " << endl;
 cin >> TestScore;
 SumX = SumX + TestScore;
 SumXsq = SumXsq + TestScore*TestScore;
}
while(Rep == X);



Also your code for Xavg and Xvar assume always 10 values input by user.
What if user only enters 3 values?

1
2
3
4
5
Xavg = SumX / 10;

//...

Xvar = ( SumXsq - 2*Xavg*SumX + 10*Xavg )/10;


Don't use integer division, use 1.0/2.0 or 0.5
1
2
3
4
C_lowerbound = Xavg - 1/2 * Xstd; // = Xavg - 0 * Xstd = Xavg
D_lowerbound = Xavg - 3/2 * Xstd;
B_lowerbound = Xavg + 1/2 * Xstd;
A_lowerbound = Xavg + 3/2 * Xstd;


Is this formula correct? Var should not be negative.

(f) Var = ( SumXsq - 2*Avg*SumX + N*Avg )/N
Last edited on
Topic archived. No new replies allowed.