473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Need help with C++ Functions Program

I do not figure out how to print the letter grades in functions. The program below compute the average of 6 grades or GPA and its standard deviation. The thing i cant figure out how to print the Letter Grades from 'A' to 'F' to the following equivalences:
3.2 < grade <=4.0 : A
2.4 < grade <=3.2 : B
1.6 < grade <=2.4 : C
0.8 < grade <=3.2 : D
0 <= grade <= 0.8 : F
I know it can be done by if statments but cant figure out how to declare and print them in functions.

Thanks for the help.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6. void enterScores(int *, const int);
  7. void calculateAverage(int *, const int, double &);
  8. void calculateStandardDeviation(int *, const int, double &, double &);
  9. int main()
  10. {
  11.    const int SIZE = 6;
  12.    double score[SIZE];
  13.    double averageScore = 0.0;
  14.    double stdDev = 0.0;
  15.    cout << fixed << setprecision(2);
  16.    enterScores(score, SIZE);
  17.    calculateAverage(score, SIZE, averageScore);
  18.    calculateStandardDeviation(score, SIZE, averageScore, stdDev);
  19.    cout << "average score is : " << averageScore << endl;
  20.    cout << "standard deviation is : " << stdDev << endl;
  21. }
  22.  
  23. void enterScores(int * score, const int SIZE)
  24. {
  25.   for (int grade = 0; grade < SIZE; ++grade)
  26.   {
  27.      cout <<" Please enter grade #"<< grade + 1 << ": ";
  28.      cin >> score[grade];
  29.      while( score[ grade ] > 4 || score[ grade ] < 0 )
  30.      {
  31.                cout <<" Invalid grade - please Re-enter a grade"
  32.                     << " between 0 and 4 inclusive : ";
  33.    cin >> score[ grade ];
  34.      }
  35.   }  
  36. }
  37.  
  38. void calculateAverage(int * score, const int SIZE, double & averageScore)
  39. {
  40.    int sum = 0;
  41.    for(int i = 0; i < SIZE; ++i)
  42.       sum += score[i];
  43.  
  44.    averageScore = sum/SIZE;
  45. }  
  46.  
  47. void calculateStandardDeviation(int * score, const int SIZE, double & averageScore, double & stdDev)
  48.  
  49. {
  50.     int sum_of_squares = 0;
  51.     for (int grade = 0; grade < SIZE; ++grade)
  52.  {   double diff = (score[grade] - averageScore);
  53.       sum_of_squares +=(diff * diff);
  54.     }
  55.     stdDev = sqrt (averageScore / SIZE);
  56. }  
  57.  
Dec 9 '06 #1
4 2105
DeMan
1,806 1GB
you could do something like....
Expand|Select|Wrap|Line Numbers
  1. char getGrade(double score)
  2. {
  3.   char rtn='Z' //Set variable to nonsense value, so we can test later if our function succeeded
  4.   if(score<=4.0&&score>=0.0) //Make sure the  score is within reasonable range
  5.   {
  6.     if(score > 3.2) //We have already checked the upper bound
  7.     {
  8.       rtn='A';
  9.     }
  10.     else if(score>2.4) //again we have already eliminated the A
  11.     {
  12.       rtn = 'B';
  13.     }
  14. /* I leave it to you to finish the grades*/
  15.   }
  16.   return rtn;
  17. }
  18.  
Dec 10 '06 #2
The program is compiling now but it's showing the letter grade in the output. the output just blank. I am not sure how can i declare variable in fuction with the coding example you showed on the post.

Anbody look at the codes at point out the error(s).


Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. #include <iomanip> 
  3. #include <math.h> 
  4.  
  5.  
  6. using namespace std; 
  7.  
  8.  
  9. void enterScores(double *, const int); 
  10. void calculateAverage(double *, const int, double &); 
  11. void calculateStandardDeviation(double *, const int, double &, double &); 
  12. void printLetterGrade(char, double &); 
  13.  
  14.  
  15. int main() 
  16.    const int SIZE = 6; 
  17.    double score[SIZE]; 
  18.    double averageScore = 0.0; 
  19.    double stdDev = 0.0; 
  20.    char letter; 
  21.    cout << fixed << setprecision(2); 
  22.  
  23.  
  24.    enterScores(score, SIZE); 
  25.    calculateAverage(score, SIZE, averageScore); 
  26.    calculateStandardDeviation(score, SIZE, averageScore, stdDev); 
  27.    printLetterGrade(letter, averageScore); 
  28.  
  29.  
  30.    cout << "average score is : " << averageScore <<"."<< endl; 
  31.    cout << "standard deviation is : " << stdDev <<"."<< endl; 
  32.    cout << "The final grade is: " << letter << endl; 
  33.  
  34. void enterScores(double * score, const int SIZE) 
  35.   for (int grade = 0; grade < SIZE; ++grade) 
  36.   { 
  37.      cout <<" Please enter grade #"<< grade + 1 << ": "; 
  38.      cin >> score[grade]; 
  39.  } 
  40.  
  41.   } 
  42.  
  43.  
  44. void calculateAverage(double * score, const int SIZE, double & 
  45. averageScore) 
  46.    double sum = 0; 
  47.    for(int i = 0; i < SIZE; ++i) 
  48.       sum += score[i]; 
  49.  
  50.    averageScore = sum/SIZE; 
  51.  
  52. void calculateStandardDeviation(double * score, const int SIZE, double 
  53. & averageScore, double & stdDev) 
  54.  
  55.     double sum_of_squares = 0; 
  56.     for (int grade = 0; grade < SIZE; ++grade) 
  57.     {   double diff = (score[grade] - averageScore); 
  58.       sum_of_squares +=(diff * diff); 
  59.     } 
  60.     stdDev = sqrt (sum_of_squares / SIZE); 
  61.  
  62.  
  63.  
  64.  
  65.  
  66. void printLetterGrade(char letter, double & averageScore) 
  67.  
  68.     if (averageScore <= 4.0 && averageScore > 3.2) 
  69.    { 
  70.  letter = 'A'; 
  71.    } 
  72.     if (averageScore <= 3.2 && averageScore > 2.4) 
  73.    { 
  74.      letter = 'B'; 
  75.    } 
  76.     if (averageScore <= 2.4 && averageScore > 1.6) 
  77.    { 
  78.      letter = 'C'; 
  79.    } 
  80.     if (averageScore <= 1.6 && averageScore > 0.8) 
  81.    { 
  82.      letter = 'D'; 
  83.    } 
  84.     if (averageScore <= 0.8 && averageScore >= 0) 
  85.    { 
  86.      letter = 'F'; 
  87.    } 
  88.  
Dec 10 '06 #3
DeMan
1,806 1GB
If you want to return a character through your variable 'letter' you bneed to pass the function a pointer to it.

Because you are returning a simple type (and only 1) I think you would be better served to explicitly return the character
Expand|Select|Wrap|Line Numbers
  1. char printLetterGrade( double  averageScore) 
  2. char letter='Z';
  3.     if (averageScore <= 4.0 && averageScore > 3.2) 
  4.    { 
  5.  letter = 'A'; 
  6.    } 
  7.     if (averageScore <= 3.2 && averageScore > 2.4) 
  8.    { 
  9.      letter = 'B'; 
  10.    } 
  11.     if (averageScore <= 2.4 && averageScore > 1.6) 
  12.    { 
  13.      letter = 'C'; 
  14.    } 
  15.     if (averageScore <= 1.6 && averageScore > 0.8) 
  16.    { 
  17.      letter = 'D'; 
  18.    } 
  19.     if (averageScore <= 0.8 && averageScore >= 0) 
  20.    { 
  21.      letter = 'F'; 
  22.    } 
  23. return letter;
  24. }
  25.  
You can now call your function as follows:
Expand|Select|Wrap|Line Numbers
  1. letter=printLetterGrade(averageScore);
  2.  
I would suggest modifying your other methods this way also.....
Dec 10 '06 #4
Thanks everybody for the help. 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 tired but satisfied with the work i done:)


Below are the codes i pasted for everybody. Enjoy.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. void enterScores(double *, const int);
  8. void calculateAverage(double *, const int, double &);
  9. void calculateStandardDeviation(double *, const int, double &, double &);
  10. char printLetterGrade (double);
  11.  
  12. int main()
  13. {
  14.    const int SIZE = 6;
  15.    double score[SIZE];
  16.    double averageScore = 0.0;
  17.    double stdDev = 0.0;
  18.  
  19.    cout << fixed << setprecision(2);
  20.  
  21.    enterScores(score, SIZE);
  22.    calculateAverage(score, SIZE, averageScore);
  23.    calculateStandardDeviation(score, SIZE, averageScore, stdDev);
  24.    printLetterGrade(averageScore);
  25.    char letter = printLetterGrade(averageScore);   
  26.  
  27.    cout << "The average score is : " << averageScore <<"."<< endl; 
  28.    cout << "The final grade is: " << letter << "." << endl;
  29.    cout << "The standard deviation is : " << stdDev <<"."<< endl;
  30. }
  31.  
  32.  
  33. void enterScores(double * score, const int SIZE)
  34. {
  35.   for (int grade = 0; grade < SIZE; ++grade)
  36.   {
  37.      cout <<" Please enter grade #"<< grade + 1 << ": ";
  38.      cin >> score[grade];
  39.      while( score[ grade ] > 4 || score[ grade ] < 0 )
  40.      {
  41.                cout <<" Invalid grade - please Re-enter a grade"
  42.                     << " between 0 and 4 inclusive : ";
  43.                cin >> score[ grade ];
  44.      }
  45.   } 
  46. }
  47.  
  48. void calculateAverage(double * score, const int SIZE, double & averageScore)
  49. {
  50.     double sum = 0;
  51.     for(int i = 0; i < SIZE; ++i)
  52.     sum += score[i];
  53.  
  54.     averageScore = sum/SIZE;
  55. }
  56.  
  57. char printLetterGrade(double averageScore)
  58. {
  59.     char letter = 'O';
  60.  
  61.     if (averageScore <= 4.0 && averageScore > 3.2) 
  62.    { 
  63.      letter = 'A'; 
  64.    } 
  65.     if (averageScore <= 3.2 && averageScore > 2.4) 
  66.    { 
  67.      letter = 'B'; 
  68.    } 
  69.     if (averageScore <= 2.4 && averageScore > 1.6) 
  70.    { 
  71.      letter = 'C'; 
  72.    } 
  73.     if (averageScore <= 1.6 && averageScore > 0.8) 
  74.    { 
  75.      letter = 'D'; 
  76.    } 
  77.     if (averageScore <= 0.8 && averageScore >= 0) 
  78.    { 
  79.      letter = 'F'; 
  80.    } 
  81.    return letter;
  82. }
  83.  
  84. void calculateStandardDeviation(double * score, const int SIZE, double & averageScore, double & stdDev)
  85.  
  86. {
  87.     double sum_of_squares = 0;
  88.  
  89.     for (int grade = 0; grade < SIZE; ++grade)
  90.     {   
  91.        double diff = (score[grade] - averageScore);
  92.       sum_of_squares +=(diff * diff);
  93.     }
  94.     stdDev = sqrt (sum_of_squares / (SIZE - 1));
Dec 10 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
6
by: Benjamin Walling | last post by:
We have 109 remote offices all running Sybase ASA Server. We collect data from these offices and consolidate it into our main server. I have written a program that will read from each office and...
17
by: Rajnee Kanth | last post by:
Hi, I want some info on the following items....send me what ever info u have pls..... 1) Segmentaion Voilation 2) Shared Objects files 3) Virtual Functions
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
2
by: martoncho | last post by:
Hi all, I am facing what is for me a really complicated situation. I program C but not this advanced (I mean direct hardware access etc). Here is the background: I am using DJGPP. I am...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
7
TRScheel
by: TRScheel | last post by:
I have seen this question appear many times in the forums, so I am writing this to hopefully help those who have this question. First, a history lesson! Your earlier languages compiled to assembly...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.