Part 1:
Write a program that uses a while loop to read in test scores until the user enters a negative number.
Print the average score after dropping the highest and lowest scores.
Print an error message if the user enters less than 3 scores.
Part 2:
Do the same calculation using a for loop, but in this case first ask the user how many scores they
want to enter, then get the scores in a for loop.
You can assume the test scores are from 0 to 100.
Algorithm:
After the loop completes, you need to have 4 pieces of information:
1. How many scores: count
2. The sum of all the scores: sum
3. The highest score: hi
4. The lowest score: lo
The answer is then: (sum - hi - lo)/(count - 2)
The hi and lo values can be computed in the loop with the following algorithm:
for each score:
if score < lo
lo = score
else
if score > hi
hi = score
Remember you need to initialize count, sum, hi, and lo before the loop begins.
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 count, sum, lo, hi, num;
int temp=count;
if(count<3) cout<<"error"<<endl;
cin>>num;
lo=num;
hi=num;
count--;
while ((num > 0)&&(count--))
{
cin>>num;
if(num<lo)lo=num;
if(num>hi)hi=num;
}
(sum-hi-lo)/(temp-2);
return 0;
}
|
The code above is what I've come up with through my own comprehension and from a friend more well versed in coding than me. I still need some help to complete it though so any help would be appreciated.