A
asif929
BobR said:(e-mail address removed) wrote in message ...// >#include <math.h>Thanks Bob for you help. But, i figured it out by myself earlier. Well,
it's been good straight 7 hours struggling to create a program. As you
can see majority of the part has been completed. The only problem i am
getting that grade 'letter' is not showing in the output. Even though,
the program is compiling and managing to output the average and
standard deviation.
Can you look at the coding below and point out the error(s).
Thanks.
#include said:void printLetterGrade(char, double &);
int main(){
const int SIZE = 6;
double score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0;
cout << fixed << setprecision(2);
enterScores(score, SIZE);
calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);char letter;
printLetterGrade(letter, averageScore);cout << "average score is : " << averageScore <<"."<< endl;
cout << "standard deviation is : " << stdDev <<"."<< endl;
cout << "The final grade is: " << letter << endl;
}
void printLetterGrade(char letter, double & averageScore){
Here, a copy of 'letter' has been made.
if (averageScore <= 4.0 && averageScore > 3.2){
letter = 'A';
You store a character in 'letter'.
and as soon as you hit the next line, 'letter' is destroyed.
You passed the char by value, when it seems you want to pass by non-const
reference.
Thanks everybody for the help especially Bob R. Your tips really walk
me through the program and help me debug it. Well its been good
straight 10 hours doing this program. i am exausted and tires and
satisfied with the work i done
Below are the codes i am pasting for everybody.
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void enterScores(double *, const int);
void calculateAverage(double *, const int, double &);
void calculateStandardDeviation(double *, const int, double &, double
&);
char printLetterGrade (double);
int main()
{
const int SIZE = 6;
double score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0;
cout << fixed << setprecision(2);
enterScores(score, SIZE);
calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);
printLetterGrade(averageScore);
char letter = printLetterGrade(averageScore);
cout << "The average score is : " << averageScore <<"."<< endl;
cout << "The final grade is: " << letter << "." << endl;
cout << "The standard deviation is : " << stdDev <<"."<< endl;
}
void enterScores(double * score, const int SIZE)
{
for (int grade = 0; grade < SIZE; ++grade)
{
cout <<" Please enter grade #"<< grade + 1 << ": ";
cin >> score[grade];
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
cout <<" Invalid grade - please Re-enter a grade"
<< " between 0 and 4 inclusive : ";
cin >> score[ grade ];
}
}
}
void calculateAverage(double * score, const int SIZE, double &
averageScore)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score;
averageScore = sum/SIZE;
}
char printLetterGrade(double averageScore)
{
char letter = 'O';
if (averageScore <= 4.0 && averageScore > 3.2)
{
letter = 'A';
}
if (averageScore <= 3.2 && averageScore > 2.4)
{
letter = 'B';
}
if (averageScore <= 2.4 && averageScore > 1.6)
{
letter = 'C';
}
if (averageScore <= 1.6 && averageScore > 0.8)
{
letter = 'D';
}
if (averageScore <= 0.8 && averageScore >= 0)
{
letter = 'F';
}
return letter;
}
void calculateStandardDeviation(double * score, const int SIZE, double
& averageScore, double & stdDev)
{
double sum_of_squares = 0;
for (int grade = 0; grade < SIZE; ++grade)
{
double diff = (score[grade] - averageScore);
sum_of_squares +=(diff * diff);
}
stdDev = sqrt (sum_of_squares / (SIZE - 1));
}