can you guys help me out. must be a logic error
Feb 27, 2012 at 3:18am UTC
My algorithm is working and it compiles correctly but for some reason i get NaN for the value of Xstd which is standard deviation heres my code:
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 170
//-| ---------------------------------------------------------------
//-| 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;
}
Feb 27, 2012 at 4:07am UTC
Xvar is negative (¿?) check your formula.
By the way, if you are not going to reuse the inputted values you could just use 1. If you are going to use them, use an array
Take a look at loops
Feb 27, 2012 at 5:04am UTC
i figured it was the formula, but that is the one our professor issued us to use... and ill look up loops but this is a intro to programming class and i think we are just to chapter 6 in the c++ book.
Topic archived. No new replies allowed.