Help!!!

I'm trying to eliminate the highest and lowest number out of 5 inputs then get the average of the remaining 3 inputs...
In some instances, it gives me incorrect average..
(We are not suppose to use arrays yet)

Here's my code:

``````#include <iostream>
using namespace std;

void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
//Ask the user to input numbers

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
//should calculate and display the average of the 3 remaining numbers

int main()
{
double score1, score2, score3, score4, score5;

getData(score1, score2, score3, score4, score5);
calcscore(score1, score2, score3, score4, score5);

}

void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
{

cout<<"Input 5 numbers :  \n";
cin>> input1
>> input2
>> input3
>> input4
>> input5;

}

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
{

if (0 > num2&&num3&&num1&&num5&&num4)
{

cout<<"Error! Please enter numbers between 0 to 10 only"  ;
cout<<"\n\n\n";

}

else if (num1 > num2&&num3&&num4 > num5)
{

cout<<"Average: "<<(num2 + num3 + num4)/3  ;
cout<<"\n\n\n";
}

else if (num2 > num1&&num3&&num4 > num5)
{

cout<<"Average: "<<(num1 + num3 + num4)/3  ;
cout<<"\n\n\n";
}

else if (num3 > num1&&num2&&num4 > num5)
{

cout<<"Average: "<<(num1 + num2 + num4)/3  ;
cout<<"\n\n\n";
}

else if (num4 > num1&&num2&&num3 > num5)
{

cout<<"Average: "<<(num1 + num2 + num3)/3  ;
cout<<"\n\n\n";
}

else if (num5 > num4&&num2&&num3 > num1)
{

cout<<"Average: "<<(num4 + num2 + num3)/3  ;
cout<<"\n\n\n";
}

else if (num1 > num5&&num2&&num3 > num4)
{

cout<<"Average: "<<(num5 + num2 + num3)/3  ;
cout<<"\n\n\n";
}

else if (num2 > num5&&num1&&num3 > num4)
{

cout<<"Average: "<<(num5 + num1 + num3)/3  ;
cout<<"\n\n\n";
}

else if (num3 > num5&&num2&&num1 > num4)
{

cout<<"Average: "<<(num5 + num2 + num1)/3  ;
cout<<"\n\n\n";
}

else if (num5 > num1&&num2&&num3 > num4)
{

cout<<"Average: "<<(num1 + num2 + num3)/3  ;
cout<<"\n\n\n";
}

else if (num4 > num1&&num2&&num5 > num3)
{

cout<<"Average: "<<(num1 + num2 + num5)/3  ;
cout<<"\n\n\n";
}

else if (num1 > num4&&num2&&num5 > num3)
{

cout<<"Average: "<<(num4 + num2 + num5)/3  ;
cout<<"\n\n\n";
}

else if (num2 > num4&&num1&&num5 > num3)
{

cout<<"Average: "<<(num4 + num1 + num5)/3  ;
cout<<"\n\n\n";
}

else if (num4 > num2&&num1&&num5 > num3)
{

cout<<"Average: "<<(num2 + num1 + num5)/3  ;
cout<<"\n\n\n";
}

else if (num5 > num2&&num1&&num4 > num3)
{

cout<<"Average: "<<(num2 + num1 + num4)/3  ;
cout<<"\n\n\n";
}

else if (num3 > num4&&num1&&num5 > num2)
{

cout<<"Average: "<<(num4 + num1 + num5)/3  ;
cout<<"\n\n\n";
}
else if (num1 > num2&&num1&&num5 > num2)
{

cout<<"Average: "<<(num2 + num1 + num5)/3  ;
cout<<"\n\n\n";
}
else if (num3 > num4&&num1&&num5 > num2)
{

cout<<"Average: "<<(num4 + num1 + num5)/3  ;
cout<<"\n\n\n";
}
else if (num4 > num3&&num1&&num5 > num2)
{

cout<<"Average: "<<(num3 + num1 + num5)/3  ;
cout<<"\n\n\n";
}
else if (num5 > num3&&num1&&num4 > num2)
{

cout<<"Average: "<<(num3 + num1 + num4)/3  ;
cout<<"\n\n\n";
}
else if (num2 > num3&&num1&&num5 > num4)
{

cout<<"Average: "<<(num3 + num1 + num5)/3  ;
cout<<"\n\n\n";
}

}
``````

## All 7 Replies

Your problem is that you're writing very complicated and redundant code and thus it's hard to think about what your code is doing. Use a simpler algorithm to find and remove the largest and smallest values.

``````#include <iostream>
using namespace std;

void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
//Ask the user to input numbers

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
//should calculate and display the average of the 3 remaining numbers

int main()
{
double score1, score2, score3, score4, score5;

getData(score1, score2, score3, score4, score5);
calcscore(score1, score2, score3, score4, score5);

return 0; // Added a return
}

void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
{
cout<<"Input 5 numbers : \n";
cin>> input1 >> input2 >> input3 >> input4 >> input5;
}

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
{
if (0 > num2&&num3&&num1&&num5&&num4)
{
cout<<"Error! Please enter numbers between 0 to 10 only" ;
cout<<"\n\n\n";
}
else if (num1 > num2&&num3&&num4 > num5)
{
cout<<"Average: "<<(num2 + num3 + num4)/3 ;
cout<<"\n\n\n";
}
else if (num2 > num1&&num3&&num4 > num5)
{
cout<<"Average: "<<(num1 + num3 + num4)/3 ;
cout<<"\n\n\n";
}
else if (num3 > num1&&num2&&num4 > num5)
{
cout<<"Average: "<<(num1 + num2 + num4)/3 ;
cout<<"\n\n\n";
}
else if (num4 > num1&&num2&&num3 > num5)
{
cout<<"Average: "<<(num1 + num2 + num3)/3 ;
cout<<"\n\n\n";
}
else if (num5 > num4&&num2&&num3 > num1)
{
cout<<"Average: "<<(num4 + num2 + num3)/3 ;
cout<<"\n\n\n";
}
else if (num1 > num5&&num2&&num3 > num4)
{
cout<<"Average: "<<(num5 + num2 + num3)/3 ;
cout<<"\n\n\n";
}
else if (num2 > num5&&num1&&num3 > num4)
{
cout<<"Average: "<<(num5 + num1 + num3)/3 ;
cout<<"\n\n\n";
}
else if (num3 > num5&&num2&&num1 > num4)
{
cout<<"Average: "<<(num5 + num2 + num1)/3 ;
cout<<"\n\n\n";
}
else if (num5 > num1&&num2&&num3 > num4)
{
cout<<"Average: "<<(num1 + num2 + num3)/3 ;
cout<<"\n\n\n";
}
else if (num4 > num1&&num2&&num5 > num3)
{
cout<<"Average: "<<(num1 + num2 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num1 > num4&&num2&&num5 > num3)
{
cout<<"Average: "<<(num4 + num2 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num2 > num4&&num1&&num5 > num3)
{
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num4 > num2&&num1&&num5 > num3)
{
cout<<"Average: "<<(num2 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num5 > num2&&num1&&num4 > num3)
{
cout<<"Average: "<<(num2 + num1 + num4)/3 ;
cout<<"\n\n\n";
}
else if (num3 > num4&&num1&&num5 > num2)
{
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num1 > num2&&num1&&num5 > num2)
{
cout<<"Average: "<<(num2 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num3 > num4&&num1&&num5 > num2)
{
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num4 > num3&&num1&&num5 > num2)
{
cout<<"Average: "<<(num3 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
else if (num5 > num3&&num1&&num4 > num2)
{
cout<<"Average: "<<(num3 + num1 + num4)/3 ;
cout<<"\n\n\n";
}
else if (num2 > num3&&num1&&num5 > num4)
{
cout<<"Average: "<<(num3 + num1 + num5)/3 ;
cout<<"\n\n\n";
}
}``````

EDIT: If you're interested in keeping your original code, then here's a little simplification:

``````#include <iostream>
using namespace std;

void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
//Ask the user to input numbers

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
//should calculate and display the average of the 3 remaining numbers

int main()
{
double score1, score2, score3, score4, score5;

getData(score1, score2, score3, score4, score5);
calcscore(score1, score2, score3, score4, score5);

return 0; // Added a return
}

void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
{
cout << "Input 5 numbers : \n";
cin >> input1 >> input2 >> input3 >> input4 >> input5;
}

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
{
if (0 > num2&&num3&&num1&&num5&&num4)
cout<<"Error! Please enter numbers between 0 to 10 only" ;
else if (num1 > num2&&num3&&num4 > num5)
cout<<"Average: "<<(num2 + num3 + num4)/3 ;
else if (num2 > num1&&num3&&num4 > num5)
cout<<"Average: "<<(num1 + num3 + num4)/3 ;
else if (num3 > num1&&num2&&num4 > num5)
cout<<"Average: "<<(num1 + num2 + num4)/3 ;
else if (num4 > num1&&num2&&num3 > num5)
cout<<"Average: "<<(num1 + num2 + num3)/3 ;
else if (num5 > num4&&num2&&num3 > num1)
cout<<"Average: "<<(num4 + num2 + num3)/3 ;
else if (num1 > num5&&num2&&num3 > num4)
cout<<"Average: "<<(num5 + num2 + num3)/3 ;
else if (num2 > num5&&num1&&num3 > num4)
cout<<"Average: "<<(num5 + num1 + num3)/3 ;
else if (num3 > num5&&num2&&num1 > num4)
cout<<"Average: "<<(num5 + num2 + num1)/3 ;
else if (num5 > num1&&num2&&num3 > num4)
cout<<"Average: "<<(num1 + num2 + num3)/3 ;
else if (num4 > num1&&num2&&num5 > num3)
cout<<"Average: "<<(num1 + num2 + num5)/3 ;
else if (num1 > num4&&num2&&num5 > num3)
cout<<"Average: "<<(num4 + num2 + num5)/3 ;
else if (num2 > num4&&num1&&num5 > num3)
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
else if (num4 > num2&&num1&&num5 > num3)
cout<<"Average: "<<(num2 + num1 + num5)/3 ;
else if (num5 > num2&&num1&&num4 > num3)
cout<<"Average: "<<(num2 + num1 + num4)/3 ;
else if (num3 > num4&&num1&&num5 > num2)
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
else if (num1 > num2&&num1&&num5 > num2)
cout<<"Average: "<<(num2 + num1 + num5)/3 ;
else if (num3 > num4&&num1&&num5 > num2)
cout<<"Average: "<<(num4 + num1 + num5)/3 ;
else if (num4 > num3&&num1&&num5 > num2)
cout<<"Average: "<<(num3 + num1 + num5)/3 ;
else if (num5 > num3&&num1&&num4 > num2)
cout<<"Average: "<<(num3 + num1 + num4)/3 ;
else if (num2 > num3&&num1&&num5 > num4)
cout<<"Average: "<<(num3 + num1 + num5)/3 ;
cout<<"\n\n\n";
}``````

Help!!!
I'm trying to eliminate the highest and lowest number out of 5 inputs then get the average of the remaining 3 inputs...
In some instances, it gives me incorrect average..
(We are not suppose to use arrays yet)

It would be SO much easier using arrays and loops. - Can you give some examples of the incorrect averages you get, and is it every time, or just sometimes?

It would be SO much easier using arrays and loops. - Can you give some examples of the incorrect averages you get, and is it every time, or just sometimes?

- Try this:
3rd number = highest; 5th number = lowest

>>
I'm trying to eliminate the highest and lowest number out of 5 inputs then get the average of the remaining 3 inputs...
In some instances, it gives me incorrect average..
<<

First find the highest/lowest integer.

When getting input, make a variable that checks for high and low numbers.

you can do something like ths :

``````int num1, num2, num3, num4, num5;
int max(0),min(0);
cin >> num1;
max = min = num1;
//then get other inputs while comparing the value``````

Then post back when you get that done.

- Try this:
3rd number = highest; 5th number = lowest

There's an error in one of your or a lack of a "else if".

Btw, this will do, however I forgot that you weren't supposed to use arrays, but like, just something alike I had laying around.

``````#include <iostream>
using namespace std;

void getData();
int CheckIsMaxOrMin();
void calcscore();

double input[5];
bool IsMaxOrMin[5];

int main()
{
getData();
calcscore();

system("PAUSE");
return 0; // Added a return
}

int CheckIsMaxOrMin()
{
int count=0;
for (int a=0; a < 5; a++)
if ((IsMaxOrMin[a]==true)){count++;}
return count;
}

void getData()
{
cout << "Input 5 numbers : \n";
cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];
double minimum=input[0];
for (int a=0; a < 5; a++)
if (input[a] < minimum){minimum=input[a];}
for (int a=0; a < 5; a++)
if (input[a] == minimum && (CheckIsMaxOrMin()!=1)){IsMaxOrMin[a]=true;}

double maximum=input[0];
for (int a=0; a < 5; a++)
if (input[a] > maximum){maximum=input[a];}
for (int a=0; a < 5; a++)
if (input[a] == maximum && (CheckIsMaxOrMin()!=2)){IsMaxOrMin[a]=true;}

for (int a=0; a < 5; a++)
cout << input[a] << " " << IsMaxOrMin[a] << endl;
}

void calcscore()
{
double num[3]={0,0,0};
for (int a=0, b=0; a < 5; a++)
{
if (!(IsMaxOrMin[a])){num[b]=input[a];b++;}
}
cout << "Average: " << (num[0]+num[1]+num[2])/3;
cout<<"\n\n\n";
}``````
``````#include <iostream>
using namespace std;

void getData();
int CheckIsMaxOrMin();
void calcscore();

double input[5];
bool IsMaxOrMin[5];

int main()
{
getData();
calcscore();

system("PAUSE");
return 0; // Added a return
}

int CheckIsMaxOrMin()
{
int count=0;
for (int a=0; a < 5; a++)
if ((IsMaxOrMin[a]==true)){count++;}
return count;
}

void getData()
{
cout << "Input 5 numbers : \n";
cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];
double minimum=input[0];
for (int a=0; a < 5; a++)
if (input[a] < minimum){minimum=input[a];}
for (int a=0; a < 5; a++)
if (input[a] == minimum && (CheckIsMaxOrMin()!=1)){IsMaxOrMin[a]=true;}

double maximum=input[0];
for (int a=0; a < 5; a++)
if (input[a] > maximum){maximum=input[a];}
for (int a=0; a < 5; a++)
if (input[a] == maximum && (CheckIsMaxOrMin()!=2)){IsMaxOrMin[a]=true;}

for (int a=0; a < 5; a++)
cout << input[a] << " " << IsMaxOrMin[a] << endl;
}

void calcscore()
{
double num[3]={0,0,0};
for (int a=0, b=0; a < 5; a++)
{
if (!(IsMaxOrMin[a])){num[b]=input[a];b++;}
}
cout << "Average: " << (num[0]+num[1]+num[2])/3;
cout<<"\n\n\n";
}
``````

Marvelous! This one actually works..Thanks for your help, really.
Problem solved, finally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.