Finding Highest, Lowest, and Average in an Array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2009
Posts: 4
Reputation: nateysmith is an unknown quantity at this point 
Solved Threads: 0
nateysmith nateysmith is offline Offline
Newbie Poster

Finding Highest, Lowest, and Average in an Array

 
0
  #1
26 Days Ago
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 62
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 15
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #2
26 Days Ago
Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: nateysmith is an unknown quantity at this point 
Solved Threads: 0
nateysmith nateysmith is offline Offline
Newbie Poster
 
0
  #3
26 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
26 Days Ago
  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. }

Originally Posted by mikiurban View Post
Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: nateysmith is an unknown quantity at this point 
Solved Threads: 0
nateysmith nateysmith is offline Offline
Newbie Poster
 
0
  #5
26 Days Ago
I think I have it now. Take a look and let me know.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #6
26 Days Ago
Looks good to me. Try it out. Does it work?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: nateysmith is an unknown quantity at this point 
Solved Threads: 0
nateysmith nateysmith is offline Offline
Newbie Poster
 
0
  #7
26 Days Ago
It does!! Thanks for your help you have been awesome!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC