Need Help with the functions program

A

asif929

BobR said:
(e-mail address removed) wrote in message ...
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 <math.h>
#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));
}
 
I

IR

OK... a few tips now that you got it working... (just to make the
code cleaner, it doesn't change anything to the functionality)

void calculateAverage(double * score, const int SIZE, double &
averageScore)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score;

averageScore = sum/SIZE;
}


Why not return the average score from the function, like you did in
printLetterGrade?

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;
}

If averageScore is between 4.0 (included) and 3.2 (excluded), then
it CAN'T be in one of the other intervals. If the final grade is A,
you don't need to test for B, C, ...
So instead of a series of unconnected "if", you should really have
if / else if / ...

In your case, performance doesn't really matter, but keep in mind
that whenever a project gets big enough (eg. multithreaded server
with hundreds or thousands of clients...), every single clock cycle
does matter.

Although "premature optimization is the root of all evil", this kind
of construct is very easy to spot, it should become a "second
nature"...

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));
}

Same comment as for calculateAverage: why not just make stdDev the
return value? And you don't need to pass averageScore as a reference
(just looking at the function declaration would make me think that
averageScore is modified by the function, which obviously isn't the
case).


Although your program is not yet homogeneous, I guess you did a
pretty god job until now. :)

However, keep in mind... those tips could make a difference between
a C and a A. And in real world programming, they really make a
difference between an easily maintainable program, and a hard-to-
understand code (remember, you're not just writing code right now,
someone will have to mantain it in 1 or 10 years...).

Anyway, keep up the good work :)


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top