Need help with averaging program

I have a program that averages float numbers, with the exception of the highest and lowest number. However, it does not work. How do I get it working? I don't want full code, just hints.

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
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
float n;
float average = 0;
float o;
float value = 0;
float highest = 0;
float lowest = 0;
float oth = value;
//  cin >> o;
n = 7;
cin >> oth;
highest = oth;
lowest = oth;
for (float i = 1; i < n; ++i) {
 //   highest = value;
   // lowest = value;
    cin >> value;
     
    if (value > highest) {
        highest = value;
    }
    if (value < lowest) {
        lowest = value;
    }
   
    average += value;
   
}
// debugging code cout << highest << endl;
// see above cout << lowest << endl;
    if (value != highest || value != lowest) {
    average /= 5;
}
// average = average + 1;
cout << "Average score: " << setprecision(3) << average;
}

For example, entering
9.2,4.3,6,8.7,7.3,8.0,and 9.1 gives me an average of 8.68 instead of 7.82. What do I do?
1
2
//   highest = value;
   // lowest = value; 

^I don't see the point of the code above.



If I understand you clearly, your goal is to get an average of all inputted numbers except for the highest and lowest (example: input: 2, 3, 7, 4, 5; output: 4 (since (3+4+5)/3 equals 4) ).
Then you should subract highest and lowest value from the average after your for loop. Then your bottom if loop loses its point, so you should delete it.

Also, you could divide your "average" variable it by (n - 2), as it would be easier to incorporate the arbitrary number of inputted numbers later.

EDIT:
Also, you forgot to add the value of the first input (cin >> oth;) to the average, so it's completely out of the calculation.
Last edited on
Topic archived. No new replies allowed.