slowly but surely

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

slowly but surely

 
0
  #1
Jul 3rd, 2009
This is still the readStuData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany) assignment. I've hit a small snag. This piece of the code works....kind of. When the program is run it prints out the table and stuff, however, when printing the grade it doesn't do it correctly. If I take away the for-loop code for printGrade and just leave the if statements, then hardcode 'int i = #', where # = a number from 0 - 21, it will print that one id and score repeatedly as well as the correct "grade" with it. But if left as is, it doesn't print "grade" correctly.

Not sure where the issue lies with this one.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void printTable(int score[], int id[], int count)
  9. {
  10. void printGrade(int oneScore, float average);
  11. const int MAX_SIZE = 21;
  12. int oneScore = 0;
  13. float average = 0;
  14. string grade;
  15. id[MAX_SIZE];
  16. score[MAX_SIZE];
  17.  
  18. cout << left << setw(9) << "ID#s" << setw(9) << "Scores" << setw(9) << "Grades" << endl << endl;
  19. //for(count = 0; count < MAX_SIZE; count++)
  20. //{
  21. printGrade(oneScore,average);
  22. //}
  23.  
  24. }
  25. void printGrade(int oneScore, float average)
  26. {
  27. const int MAX_SIZE = 21;
  28.  
  29. int id[MAX_SIZE] = {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121};
  30. int scores[MAX_SIZE] = {100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0};
  31. oneScore = 0;
  32. average = 0;
  33. string grade;
  34.  
  35. for(int i = 0; i < MAX_SIZE; i++)
  36. {
  37. oneScore = oneScore + scores[i];
  38. average = oneScore / MAX_SIZE;
  39.  
  40. if(scores[i] > average + 10)
  41. {
  42. grade = "outstanding";
  43. }
  44. else if(scores[i] < average - 10)
  45. {
  46. grade = "unsatisfactory";
  47. }
  48. else
  49. {
  50. grade = "satisfactory";
  51. }
  52.  
  53. cout << left << setw(9) << id[i] << setw(9) << scores[i] << setw(9) << grade << endl;
  54. }
  55.  
  56. }
  57. int main()
  58. {
  59. const int MAX_SIZE = 21;
  60. int id[MAX_SIZE] = {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121};
  61. int score[MAX_SIZE] = {100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0};
  62. int count = 0;
  63. int oneScore = 0;
  64. float average = 0;
  65.  
  66. printTable(score, id, count);
  67.  
  68. return 0;
  69. }
Last edited by Ancient Dragon; Jul 3rd, 2009 at 1:19 pm. Reason: add line numbers
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: slowly but surely

 
0
  #2
Jul 3rd, 2009
Its not calculating the average score correctly. It should be <sum of all scores> / MAX_SIZE. To do that you need another loop before the one you already have
  1. int sum = 0;
  2. for(int i = 0; i < MAX_SIZE; i++)
  3. sum += scores[i];
  4. average = sum / MAX_SIZE;

Now delete lines 37 and 38 in the code snippet you posted.
Last edited by Ancient Dragon; Jul 3rd, 2009 at 1:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Re: slowly but surely

 
0
  #3
Jul 3rd, 2009
Thanks!! Now it works!!
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC