How to Show Highest and Lowest number
Apr 1, 2020 at 8:09pm UTC
Hello, I was wondering how to print out the users highest grade and lowest grade after the loop ends. Also if you can please provide a description of how to do it please do so. I am a beginner and lots of these concepts are still very confusing to me, so any help is much appreciated.
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
#include<iostream>
using namespace std;
int main()
{int numGrades;
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;
}
average = sum / numGrades;
cout<< "\n Your average is = " << average;
cout << "\n\n" ;
return 0;
}
Apr 1, 2020 at 8:25pm UTC
pseudo code:
1 2 3 4 5 6 7
highest_grade = -1; // some initial low number
each loop:
if the current grade is higher than the highest_grade,
set the highest_grade to be the current grade
print highest_grade
Apr 1, 2020 at 8:37pm UTC
Something like
Output:
"Your average is = "
"Your highest grade is = "
"Your lowest grade is = "
at the end
Apr 1, 2020 at 9:06pm UTC
Yes, so just as you have a variable for average, you're going to need to declare two more variables, one for highest grade, one for lowest grade.
Apr 2, 2020 at 12:56am UTC
I seemed to get the maximum working, but the minimum doesnt.
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;
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 ( max < grade){
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 12:58am UTC
You never initialized min to a proper value before comparing it.
Turn on compiler warnings (-Wall in GCC).
In function 'int main()':
26:40: warning: 'min' may be used uninitialized in this function [-Wmaybe-uninitialized]
Normally you would set min to either initially be an invalid value, really big value, or if you want to be extra safe,
1 2 3 4 5
#include <limits>
// ...
int min = std::numeric_limits<int >::max();
Last edited on Apr 2, 2020 at 1:01am UTC
Apr 2, 2020 at 1:12am UTC
Okay got it. How would I make sure the user inputs a number 100 or below? edit: or actually between 0 and 100 only
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 ( max < grade){
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;
}
Last edited on Apr 2, 2020 at 1:16am UTC
Apr 2, 2020 at 1:21am UTC
Simplest option would be to check that the conditions you want are true after line 8 (after input is received).
If it's not true, then either loop until it is true, or return early from the function.
Topic archived. No new replies allowed.