Well, yes, writing code is a creative endeavor and that’s where the interpretation comes in.
The key is to break down the problem into manageable chunks. In this case you’ve got essentially four steps:
- read in the scores; is there a fixed number - are you told up front how many scores there will be - or do you just need to keep going until you reach the end of the input? How is the end of input determined?
- you need to keep track of the minima and maxima. Either do that as you go, or batch process it with the sort method.
- calculate the average after taking into account the minima and maxima you found in the previous step.
- output the result.
Make sure you have a clear understanding of what is required in each step. Step 1 is important because that will determine what data structure you need: can you get away with just a few ints and/or floats, or do you need something a little more sophisticated like a vector? Step 4 is straightforward but vital because that provides the results to whoever or whatever runs your program and presumably makes a decision dependent upon the result you provide.
You have to keep at the problem until you get the correct result. The more you do the work, the more tips and tricks and methods you understand, the easier it gets, but you can’t really short-circuit it. The great thing about computers is that you can make a hundred attempts and it costs virtually nothing other than time.
From your original post the only bit you have to complete is the part where you exclude the minimum and maximum values. If you’re stumped you could write out examples on a piece of paper, e.g. start with something simple like a monotonic sequence: 1, 2, 3, 4, 5 e.g. and figure out what are the minimum and maximum values: 1 and 5. How did you determine that? Once you’ve determined what the minimum and maximum are, how do you exclude them? Then calculating the average of the remainder is straightforward: (2 + 3 + 4) / 3 = 3 in this case. If your program can accept repeated values such as 1, 1, 2, 3, 4, 5, 5 then how would you deal with that? What about 5, 5, 4, 3, 2, 1, 1 or 4, 2, 5, 1, 3, 5, 1, etc.
You really only have two problems: how do you store the data your program receives, and how do you account for minimum and maximum values? The latter is achieved by comparing values, and the former is a design decision you have to make based on the requirements of your program. If you can read values from stdin and write them into an array or vector then you basically have the sequence like in the examples above, and you can then set about figuring out which are the values you need to exclude. The brute force method - usually the first approximation in solving any software problem - would be to compare every value you read into an array with every other value. You can do that in one pass by
doing something like the following, assuming you read data into a vector v:
float min = POSITIVE_INFINITY; // define some real value outside the expected range
float max = NEGATIVE_INFINITY; // same in the opposite direction on the number line
float sum = 0.0f;
for (size_t i = 0; i < v.size(); ++i) {
if (v[i] < min)
min = v[i];
if (v[i] > max)
max = v[i];
sum += v[i];
}
Then what do you do with min, max and sum? How do you modify the code to handle repeated minimum and maximum values? What if the minimum and maximum values are the same?
Note that comparing floats or doubles can be problematic because decimal values can’t always be exactly represented by single or double precision format binary numbers. This isn’t usually an issue - you might have to compare within an error range that’s usually something like 10^-6 - but it’s something to be aware of depending on your expected input values. min() and max() type comparisons are usually best done on integer numeric values (but any quantity will work - that’s a detail you probably don’t have to be bothered with in such an exercise).