tinOfBeans - 10 months ago 44

C++ Question

Hi guys I'm new to c++ but have some good experience with java.

I am trying to make a simple program that takes in 5 integers (as judges scores). The largest and smallest scores are discarded and an average must be calculated from the middle 3 scores that are left.

I thought this would be fairly simple but for some reason my program always gives me an answer slightly larger than what it should.

I should also state that the answer can be a real number while the inputs have to be integers.

Below is what I've got so far, it's pretty much complete but has some hidden schoolboy error somewhere that I haven't found for a couple hours.

I know there are probably tones of other more efficient ways to do this, but this was the best I could think of as I've only just started c++.

Any help would be greatly appreciated!!

`#include <iostream>`

using namespace std;

int getSmallest(int first, int second, int third, int fourth, int fifth);

int getLargest(int first, int second, int third, int fourth, int fifth);

double calculateAverage(int largest, int smallest, int sum);

int main()

{

int first, second, third, fourth, fifth;

int smallest, largest, sum;

//double ave;

//read input of 5 scores from judges

cin >> first;

cin >> second;

cin >> third;

cin >> fourth;

cin >> fifth;

smallest = getSmallest ( first, second, third, fourth, fifth );

largest = getLargest ( first, second, third, fourth, fifth );

sum = (first + second + third + fourth + fifth);

//ave = calculateAverage(largest, smallest, sum);

//cout << ave << endl;

cout << "The average is " << (double)calculateAverage(largest, smallest, sum) << endl;

return 0;

}

int getSmallest(int first, int second, int third, int fourth, int fifth)

{

int smallest = 0;

if ( first <= smallest )

{

smallest = first;

}

if ( second <= smallest )

{

smallest = second;

}

if ( third <= smallest )

{

smallest = third;

}

if ( fourth <= smallest )

{

smallest = fourth;

}

if ( fifth <= smallest )

{

smallest = fifth;

}

return smallest;

}

int getLargest(int first, int second, int third, int fourth, int fifth)

{

int largest = 0;

if ( first >= largest )

{

largest = first;

}

if ( second >= largest )

{

largest = second;

}

if ( third >= largest )

{

largest = third;

}

if ( fourth >= largest )

{

largest = fourth;

}

if ( fifth >= largest )

{

largest = fifth;

}

return largest;

}

double calculateAverage(int largest, int smallest, int sum)

{

return (((double)sum) - ((double)largest + (double)smallest)) / 3.0;

}

Answer Source

In the `getSmallest`

routine, you have to set

```
int smallest = INT_MAX;
```

or `smallest`

will be 0 whatever you input.

(include `<climits>`

for `INT_MAX`

to be available)

EDIT: it works but is not efficient. You could save one test (no need for `INT_MAX`

in that case) since first condition will always be true:

```
int getSmallest(int first, int second, int third, int fourth, int fifth)
{
int smallest = first;
if ( second <= smallest )
{
smallest = second;
}
```

same optimization goes for `getLargest`

```
int getLargest(int first, int second, int third, int fourth, int fifth)
{
int largest = first;
if ( second >= largest )
{
largest = second;
}
```