944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1718
  • C++ RSS
Nov 2nd, 2009
0

Finding Highest, Lowest, and Average in an Array

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.  
  6. {
  7. int TESTS = 0;
  8. int testScore[TESTS];
  9. int testNum = 0;
  10. int a;
  11. double total = 0;
  12. double average;
  13. testNum = 0;
  14. cout<<"How many scores are there? ";
  15. cin >> TESTS;
  16. cout<<"Enter a score: ";
  17. cin >> testScore[testNum];
  18. while(testNum < TESTS && testScore[testNum])
  19. {
  20. total += testScore[testNum];
  21. ++testNum;
  22. if(testNum < TESTS)
  23. {
  24. cout<<"Enter a score: ";
  25. cin>>testScore[testNum];
  26. }
  27. }
  28. cout<< "The entered test scores are: ";
  29.  
  30. for (a = 0; a < testNum; ++a)
  31. cout<<testScore[a]<<" ";
  32. average = total / testNum;
  33. cout<<endl<<"The average test score is "<<average<<endl;
  34.  
  35. system("PAUSE");
  36.  
  37. }

I am trying to find the highest, lowest and average in this array. I can get the average, but I am not sure how to get it to pull the highest and lowest values that are entered.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nateysmith is offline Offline
4 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:
C++ Syntax (Toggle Plain Text)
  1. for (a = 0; a < testNum; ++a)
  2. cout<<testScore[a]<<" ";
keep comparing them with testScore[a] to see if they are the lowest or highest numbers.
I hope this helps, without giving away too much information.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
I am not really sure what you mean. Can you show me an example? I have just been working on this for 5 hours now and I can't see what is going wrong.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nateysmith is offline Offline
4 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int TESTS = 0;
  7. int testScore[TESTS];
  8. int testNum = 0;
  9. int a;
  10. double total = 0;
  11. double average;
  12. testNum = 0;
  13. cout<<"How many scores are there? ";
  14. cin >> TESTS;
  15. cout<<"Enter a score: ";
  16. cin >> testScore[testNum];
  17. while(testNum < TESTS && testScore[testNum])
  18. {
  19. total += testScore[testNum];
  20. ++testNum;
  21. if(testNum < TESTS)
  22. {
  23. cout<<"Enter a score: ";
  24. cin>>testScore[testNum];
  25. }
  26. }
  27. cout<< "The entered test scores are: ";
  28.  
  29. for (a = 0; a < testNum; ++a)
  30. cout<<testScore[a]<<" ";
  31. average = total / testNum;
  32. cout<<endl<<"The average test score is "<<average<<endl;
  33.  
  34. system("PAUSE");
  35.  
  36. }

Click to Expand / Collapse  Quote originally posted by mikiurban ...
Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:
C++ Syntax (Toggle Plain Text)
  1. for (a = 0; a < testNum; ++a)
  2. cout<<testScore[a]<<" ";
keep comparing them with testScore[a] to see if they are the lowest or highest numbers.
I hope this helps, without giving away too much information.

I think it would be better to compare the minimum and maximum in the WHILE loop instead of the FOR loop, as suggested above, though either is doable. Assuming you do it in the While loop, here's some revised code that can get you on the right track:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.  
  6. {
  7. int TESTS = 0;
  8. int testScore[TESTS];
  9. int testNum = 0;
  10. int a;
  11. double total = 0;
  12. double average;
  13. testNum = 0;
  14. cout<<"How many scores are there? ";
  15. cin >> TESTS;
  16. cout<<"Enter a score: ";
  17. cin >> testScore[testNum];
  18.  
  19. int minimum = testScore[testNum]; // initialize to first score.
  20. int maximum = testScore[testNum]; // initialize to first score.
  21.  
  22.  
  23. while(testNum < TESTS && testScore[testNum])
  24. {
  25. total += testScore[testNum];
  26. ++testNum;
  27. if(testNum < TESTS)
  28. {
  29. cout<<"Enter a score: ";
  30. cin>>testScore[testNum];
  31.  
  32. if (/* compare testScore[testNum] to minimum */)
  33. {
  34. minimum = testScore[testNum];
  35. }
  36. if (/* compare testScore[testNum] to maximum */)
  37. {
  38. maximum = testScore[testNum];
  39. }
  40. }
  41. }
  42. cout<< "The entered test scores are: ";
  43.  
  44. for (a = 0; a < testNum; ++a)
  45. cout<<testScore[a]<<" ";
  46. average = total / testNum;
  47. cout<<endl<<"The average test score is "<<average<<endl;
  48.  
  49. // display maximum and minimum.
  50.  
  51. system("PAUSE");
  52.  
  53. }

See lines 19, 20, 32 - 39, and 49, which I have added. Line 49 is a simple cout statement. Look at lines 32 and 36. You need to test for the appropriate conditions and fill that in where the comment is.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
I think I have it now. Take a look and let me know.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.  
  6. {
  7. int TESTS = 0;
  8. int testScore[TESTS];
  9. int testNum = 0;
  10. int a;
  11. double total = 0;
  12. double average;
  13. testNum = 0;
  14. cout<<"How many scores are there? ";
  15. cin >> TESTS;
  16. cout<<"Enter a score: ";
  17. cin >> testScore[testNum];
  18.  
  19. int minimum = testScore[testNum]; // initialize to first score.
  20. int maximum = testScore[testNum]; // initialize to first score.
  21.  
  22.  
  23. while(testNum < TESTS && testScore[testNum])
  24. {
  25. total += testScore[testNum];
  26. ++testNum;
  27. if(testNum < TESTS)
  28. {
  29. cout<<"Enter a score: ";
  30. cin>>testScore[testNum];
  31.  
  32. if (testScore[testNum] < minimum)
  33. {
  34. minimum = testScore[testNum];
  35. }
  36. if (testScore[testNum] > maximum)
  37. {
  38. maximum = testScore[testNum];
  39. }
  40. }
  41. }
  42. cout<< "The entered test scores are: ";
  43.  
  44. for (a = 0; a < testNum; ++a)
  45. cout<<testScore[a]<<" ";
  46. average = total / testNum;
  47. cout<<endl<<"The average test score is "<<average<<endl;
  48.  
  49. cout<<"The highest score is "<<maximum<<" and the lowest score is "<<minimum<<endl;
  50.  
  51. system("PAUSE");
  52.  
  53. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nateysmith is offline Offline
4 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
Looks good to me. Try it out. Does it work?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 2nd, 2009
0
Re: Finding Highest, Lowest, and Average in an Array
It does!! Thanks for your help you have been awesome!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nateysmith is offline Offline
4 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Assighment
Next Thread in C++ Forum Timeline: Array rotate in wrong way





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC