How to store user input in array and sort
Apr 2, 2020 at 2:32am UTC
How would you rewrite the code below to have the user inputs stored in an array and then show the grades after being sorted. If you can, please add comments to explain the changes made to make it a bit easier to understand. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include<iostream>
using namespace std;
int main()
{int numGrades, max = -1, min = 101;
double grade = 0, sum = 0, average;
cout << "\nHow many grades do you have? " ;
cin >> numGrades;
cout << "\nList your grades: " << endl;
for (int i = 1; i <= numGrades; i++){
cout << "Grade " << i << endl;
cin >> grade;
sum += grade;
if ( grade > max){
max = grade ;
}
else if ( grade < min){
min = grade ;
}
}
average = sum / numGrades;
cout<< "\nYour average is = " << average;
cout<< "\nYour highest grade is = " << max
<< "\nYour lowest grade is = " << min;
cout << "\n\n" ;
return 0;
}
Apr 2, 2020 at 2:55am UTC
Fix your spacing.
Apr 2, 2020 at 3:15am UTC
Also, most people here will be a lot more helpful if you give it a try yourself first.
Here's the site's tutorial on arrays:
http://www.cplusplus.com/doc/tutorial/arrays/
Try it out and then show us what doesn't work.
And please indent the part of your code within the for loop for readability.
Topic archived. No new replies allowed.