| | |
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:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int TESTS = 0; int testScore[TESTS]; int testNum = 0; int a; double total = 0; double average; testNum = 0; cout<<"How many scores are there? "; cin >> TESTS; cout<<"Enter a score: "; cin >> testScore[testNum]; while(testNum < TESTS && testScore[testNum]) { total += testScore[testNum]; ++testNum; if(testNum < TESTS) { cout<<"Enter a score: "; cin>>testScore[testNum]; } } cout<< "The entered test scores are: "; for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" "; average = total / testNum; cout<<endl<<"The average test score is "<<average<<endl; system("PAUSE"); }
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.
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 15
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:
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.
C++ Syntax (Toggle Plain Text)
for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" ";
I hope this helps, without giving away too much information.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#4 26 Days Ago
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int TESTS = 0; int testScore[TESTS]; int testNum = 0; int a; double total = 0; double average; testNum = 0; cout<<"How many scores are there? "; cin >> TESTS; cout<<"Enter a score: "; cin >> testScore[testNum]; while(testNum < TESTS && testScore[testNum]) { total += testScore[testNum]; ++testNum; if(testNum < TESTS) { cout<<"Enter a score: "; cin>>testScore[testNum]; } } cout<< "The entered test scores are: "; for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" "; average = total / testNum; cout<<endl<<"The average test score is "<<average<<endl; system("PAUSE"); }
•
•
•
•
Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:
keep comparing them with testScore[a] to see if they are the lowest or highest numbers.C++ Syntax (Toggle Plain Text)
for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" ";
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)
#include <iostream> using namespace std; int main() { int TESTS = 0; int testScore[TESTS]; int testNum = 0; int a; double total = 0; double average; testNum = 0; cout<<"How many scores are there? "; cin >> TESTS; cout<<"Enter a score: "; cin >> testScore[testNum]; int minimum = testScore[testNum]; // initialize to first score. int maximum = testScore[testNum]; // initialize to first score. while(testNum < TESTS && testScore[testNum]) { total += testScore[testNum]; ++testNum; if(testNum < TESTS) { cout<<"Enter a score: "; cin>>testScore[testNum]; if (/* compare testScore[testNum] to minimum */) { minimum = testScore[testNum]; } if (/* compare testScore[testNum] to maximum */) { maximum = testScore[testNum]; } } } cout<< "The entered test scores are: "; for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" "; average = total / testNum; cout<<endl<<"The average test score is "<<average<<endl; // display maximum and minimum. system("PAUSE"); }
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.
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#5 26 Days Ago
I think I have it now. Take a look and let me know.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int TESTS = 0; int testScore[TESTS]; int testNum = 0; int a; double total = 0; double average; testNum = 0; cout<<"How many scores are there? "; cin >> TESTS; cout<<"Enter a score: "; cin >> testScore[testNum]; int minimum = testScore[testNum]; // initialize to first score. int maximum = testScore[testNum]; // initialize to first score. while(testNum < TESTS && testScore[testNum]) { total += testScore[testNum]; ++testNum; if(testNum < TESTS) { cout<<"Enter a score: "; cin>>testScore[testNum]; if (testScore[testNum] < minimum) { minimum = testScore[testNum]; } if (testScore[testNum] > maximum) { maximum = testScore[testNum]; } } } cout<< "The entered test scores are: "; for (a = 0; a < testNum; ++a) cout<<testScore[a]<<" "; average = total / testNum; cout<<endl<<"The average test score is "<<average<<endl; cout<<"The highest score is "<<maximum<<" and the lowest score is "<<minimum<<endl; system("PAUSE"); }
![]() |
Similar Threads
- getting my code for highest and lowest value in a numeric array to work (C++)
- ignoring values if it is the highest or lowest... (C)
- How do I get the highest and lowest of an array? (Pascal and Delphi)
- calculating sum, average, highest and lowest grades (Visual Basic 4 / 5 / 6)
- How can I get Highest, Lowest and Average ? (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Assighment
- Next Thread: Array rotate in wrong way
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






