Misread the instructions

So, looking back to my homework here and I seem to have misread it--no wonder my work wasn't matching up with my given examples!. What I did in my lab was take off the first and last number of five and only average the middle three. What I need to do is ignore the highest and lowest value numbers and average out the scrAvg with the remaining three numbers. Any idea how I can do that? I can't even imagine how to do this outside of an array, but even then I don't know what the numbers will be.

EDIT: I should probably explain a little better to make this simpler. We input 5 numbers, and of those numbers we ignore the highest and lowest value. We add up the remaining three, and divide that total by three before multiplying it by the difLev to give us the average score.


Removed code for the degree of horribleness it was. Thank you and sorry!
Last edited on
quick code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>

int main()
{
    int highest = std::numeric_limits<int>().min();
    int lowest  = std::numeric_limits<int>().max();
    int sum = 0;
    for(int i = 0; i < 5; ++i) {
        int x;
        std::cin >> x;
        highest = std::max(highest, x);
        lowest  = std::min(lowest,  x);
        sum += x;
    }
    sum -= highest + lowest;
    std::cout << sum / 3.0;
}
0 4 5 99 6
5
Okay, so when I did this, I got squiggly red lines beneath max and min. I did toss in the <limits>, and when I scroll over it it gives me an error message that reads:

"Error: expression preceding parenthesis of apparent call must have (pointer-to) function type.

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>
#include <map>
#include <iomanip>
#include <string>
#include <limits>
#include <sstream>
using namespace std;

//Declarations
string name, city;
char answer; //answer = y/n = process new diver?
int min, max, sum = 0;
int highest = numeric_limits<int>().min(), lowest = numeric_limits<int>().max(); //highest and lowest numbers for AVG.
int x = 0, i = 0, nwDvr = 0, dvNum = 0, ttlNum = 0; //x & i = counters; nwDvr = new Diver; diver number; total number of scores
float scr[5], score = 0.0, scrLp = 0.0; //Array (score0, score1, score2, score3, score4), score = counter, score of all loops
float difLvl = 0.0, scrAvg = 0.0, ttlAvg = 0.0; //difficulty level, score average, total avg score, 


for (int i = 0; i < 5; ++i) {
			int x;
			cin >> x;
			highest = max(highest, x);
			lowest = min(lowest, x);
			sum += x;
		}
		sum -= highest + lowest;
		cout << sum / 3.0;
Last edited on
I forgot #include <algorithm>
Thanks for that! I fixed it, but I came upon another issue as I moved things around. My code is no longer printing the average (scrAvg & ttlAvg), it just comes up as 0 every time. I think I may have altered something too much and I've spent too much time trying to figure it out.

EDIT: I've solved one error, but looks like that's not all that was wrong. I'm somehow reaching into the negative digits when I run the program.
Last edited on
1) you should deduct highest and lowest after all input.
2) You should calculate average only after you input difficulty.
3) You should watch which variables you use - you are using score instead of sum on line 78.
4) your for loop is useless.

And lastly: declare variables when you need them (minding scope) and assign them meaningful values here. It is way easier to see problems that way and code will be cleanier.
Thank you for your tips and help! Everything you said makes complete sense. Sometimes I'm just not all here.
Topic archived. No new replies allowed.