Finding the highest and the lowest number out of 5 inputs...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 4
Reputation: violet101 is an unknown quantity at this point 
Solved Threads: 0
violet101 violet101 is offline Offline
Newbie Poster

Finding the highest and the lowest number out of 5 inputs...

 
0
  #1
28 Days Ago
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";
}

}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
2
  #2
28 Days Ago
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.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 52
Reputation: Skeen is an unknown quantity at this point 
Solved Threads: 1
Skeen's Avatar
Skeen Skeen is offline Offline
Junior Poster in Training
 
1
  #3
28 Days Ago
Use code tags please:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
  5. //Ask the user to input numbers
  6.  
  7. void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
  8. //should calculate and display the average of the 3 remaining numbers
  9.  
  10. int main()
  11. {
  12. double score1, score2, score3, score4, score5;
  13.  
  14. getData(score1, score2, score3, score4, score5);
  15. calcscore(score1, score2, score3, score4, score5);
  16.  
  17. return 0; // Added a return
  18. }
  19.  
  20. void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
  21. {
  22. cout<<"Input 5 numbers : \n";
  23. cin>> input1 >> input2 >> input3 >> input4 >> input5;
  24. }
  25.  
  26. void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
  27. {
  28. if (0 > num2&&num3&&num1&&num5&&num4)
  29. {
  30. cout<<"Error! Please enter numbers between 0 to 10 only" ;
  31. cout<<"\n\n\n";
  32. }
  33. else if (num1 > num2&&num3&&num4 > num5)
  34. {
  35. cout<<"Average: "<<(num2 + num3 + num4)/3 ;
  36. cout<<"\n\n\n";
  37. }
  38. else if (num2 > num1&&num3&&num4 > num5)
  39. {
  40. cout<<"Average: "<<(num1 + num3 + num4)/3 ;
  41. cout<<"\n\n\n";
  42. }
  43. else if (num3 > num1&&num2&&num4 > num5)
  44. {
  45. cout<<"Average: "<<(num1 + num2 + num4)/3 ;
  46. cout<<"\n\n\n";
  47. }
  48. else if (num4 > num1&&num2&&num3 > num5)
  49. {
  50. cout<<"Average: "<<(num1 + num2 + num3)/3 ;
  51. cout<<"\n\n\n";
  52. }
  53. else if (num5 > num4&&num2&&num3 > num1)
  54. {
  55. cout<<"Average: "<<(num4 + num2 + num3)/3 ;
  56. cout<<"\n\n\n";
  57. }
  58. else if (num1 > num5&&num2&&num3 > num4)
  59. {
  60. cout<<"Average: "<<(num5 + num2 + num3)/3 ;
  61. cout<<"\n\n\n";
  62. }
  63. else if (num2 > num5&&num1&&num3 > num4)
  64. {
  65. cout<<"Average: "<<(num5 + num1 + num3)/3 ;
  66. cout<<"\n\n\n";
  67. }
  68. else if (num3 > num5&&num2&&num1 > num4)
  69. {
  70. cout<<"Average: "<<(num5 + num2 + num1)/3 ;
  71. cout<<"\n\n\n";
  72. }
  73. else if (num5 > num1&&num2&&num3 > num4)
  74. {
  75. cout<<"Average: "<<(num1 + num2 + num3)/3 ;
  76. cout<<"\n\n\n";
  77. }
  78. else if (num4 > num1&&num2&&num5 > num3)
  79. {
  80. cout<<"Average: "<<(num1 + num2 + num5)/3 ;
  81. cout<<"\n\n\n";
  82. }
  83. else if (num1 > num4&&num2&&num5 > num3)
  84. {
  85. cout<<"Average: "<<(num4 + num2 + num5)/3 ;
  86. cout<<"\n\n\n";
  87. }
  88. else if (num2 > num4&&num1&&num5 > num3)
  89. {
  90. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  91. cout<<"\n\n\n";
  92. }
  93. else if (num4 > num2&&num1&&num5 > num3)
  94. {
  95. cout<<"Average: "<<(num2 + num1 + num5)/3 ;
  96. cout<<"\n\n\n";
  97. }
  98. else if (num5 > num2&&num1&&num4 > num3)
  99. {
  100. cout<<"Average: "<<(num2 + num1 + num4)/3 ;
  101. cout<<"\n\n\n";
  102. }
  103. else if (num3 > num4&&num1&&num5 > num2)
  104. {
  105. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  106. cout<<"\n\n\n";
  107. }
  108. else if (num1 > num2&&num1&&num5 > num2)
  109. {
  110. cout<<"Average: "<<(num2 + num1 + num5)/3 ;
  111. cout<<"\n\n\n";
  112. }
  113. else if (num3 > num4&&num1&&num5 > num2)
  114. {
  115. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  116. cout<<"\n\n\n";
  117. }
  118. else if (num4 > num3&&num1&&num5 > num2)
  119. {
  120. cout<<"Average: "<<(num3 + num1 + num5)/3 ;
  121. cout<<"\n\n\n";
  122. }
  123. else if (num5 > num3&&num1&&num4 > num2)
  124. {
  125. cout<<"Average: "<<(num3 + num1 + num4)/3 ;
  126. cout<<"\n\n\n";
  127. }
  128. else if (num2 > num3&&num1&&num5 > num4)
  129. {
  130. cout<<"Average: "<<(num3 + num1 + num5)/3 ;
  131. cout<<"\n\n\n";
  132. }
  133. }

EDIT: If you're interested in keeping your original code, then here's a little simplification:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
  5. //Ask the user to input numbers
  6.  
  7. void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
  8. //should calculate and display the average of the 3 remaining numbers
  9.  
  10. int main()
  11. {
  12. double score1, score2, score3, score4, score5;
  13.  
  14. getData(score1, score2, score3, score4, score5);
  15. calcscore(score1, score2, score3, score4, score5);
  16.  
  17. return 0; // Added a return
  18. }
  19.  
  20. void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
  21. {
  22. cout << "Input 5 numbers : \n";
  23. cin >> input1 >> input2 >> input3 >> input4 >> input5;
  24. }
  25.  
  26. void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
  27. {
  28. if (0 > num2&&num3&&num1&&num5&&num4)
  29. cout<<"Error! Please enter numbers between 0 to 10 only" ;
  30. else if (num1 > num2&&num3&&num4 > num5)
  31. cout<<"Average: "<<(num2 + num3 + num4)/3 ;
  32. else if (num2 > num1&&num3&&num4 > num5)
  33. cout<<"Average: "<<(num1 + num3 + num4)/3 ;
  34. else if (num3 > num1&&num2&&num4 > num5)
  35. cout<<"Average: "<<(num1 + num2 + num4)/3 ;
  36. else if (num4 > num1&&num2&&num3 > num5)
  37. cout<<"Average: "<<(num1 + num2 + num3)/3 ;
  38. else if (num5 > num4&&num2&&num3 > num1)
  39. cout<<"Average: "<<(num4 + num2 + num3)/3 ;
  40. else if (num1 > num5&&num2&&num3 > num4)
  41. cout<<"Average: "<<(num5 + num2 + num3)/3 ;
  42. else if (num2 > num5&&num1&&num3 > num4)
  43. cout<<"Average: "<<(num5 + num1 + num3)/3 ;
  44. else if (num3 > num5&&num2&&num1 > num4)
  45. cout<<"Average: "<<(num5 + num2 + num1)/3 ;
  46. else if (num5 > num1&&num2&&num3 > num4)
  47. cout<<"Average: "<<(num1 + num2 + num3)/3 ;
  48. else if (num4 > num1&&num2&&num5 > num3)
  49. cout<<"Average: "<<(num1 + num2 + num5)/3 ;
  50. else if (num1 > num4&&num2&&num5 > num3)
  51. cout<<"Average: "<<(num4 + num2 + num5)/3 ;
  52. else if (num2 > num4&&num1&&num5 > num3)
  53. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  54. else if (num4 > num2&&num1&&num5 > num3)
  55. cout<<"Average: "<<(num2 + num1 + num5)/3 ;
  56. else if (num5 > num2&&num1&&num4 > num3)
  57. cout<<"Average: "<<(num2 + num1 + num4)/3 ;
  58. else if (num3 > num4&&num1&&num5 > num2)
  59. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  60. else if (num1 > num2&&num1&&num5 > num2)
  61. cout<<"Average: "<<(num2 + num1 + num5)/3 ;
  62. else if (num3 > num4&&num1&&num5 > num2)
  63. cout<<"Average: "<<(num4 + num1 + num5)/3 ;
  64. else if (num4 > num3&&num1&&num5 > num2)
  65. cout<<"Average: "<<(num3 + num1 + num5)/3 ;
  66. else if (num5 > num3&&num1&&num4 > num2)
  67. cout<<"Average: "<<(num3 + num1 + num4)/3 ;
  68. else if (num2 > num3&&num1&&num5 > num4)
  69. cout<<"Average: "<<(num3 + num1 + num5)/3 ;
  70. cout<<"\n\n\n";
  71. }
Last edited by Skeen; 28 Days Ago at 4:43 am.
// Skeen
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 52
Reputation: Skeen is an unknown quantity at this point 
Solved Threads: 1
Skeen's Avatar
Skeen Skeen is offline Offline
Junior Poster in Training
 
0
  #4
28 Days Ago
Originally Posted by violet101 View Post
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?
// Skeen
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: violet101 is an unknown quantity at this point 
Solved Threads: 0
violet101 violet101 is offline Offline
Newbie Poster
 
0
  #5
28 Days Ago
Originally Posted by Skeen View Post
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

Thanks for the reply...=)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,244
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 154
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
28 Days Ago
>>
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 :
  1. int num1, num2, num3, num4, num5;
  2. int max(0),min(0);
  3. cin >> num1;
  4. max = min = num1;
  5. //then get other inputs while comparing the value

Then post back when you get that done.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 52
Reputation: Skeen is an unknown quantity at this point 
Solved Threads: 1
Skeen's Avatar
Skeen Skeen is offline Offline
Junior Poster in Training
 
0
  #7
28 Days Ago
Originally Posted by violet101 View Post
- Try this:
3rd number = highest; 5th number = lowest

Thanks for the reply...=)
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.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void getData();
  5. int CheckIsMaxOrMin();
  6. void calcscore();
  7.  
  8. double input[5];
  9. bool IsMaxOrMin[5];
  10.  
  11. int main()
  12. {
  13. getData();
  14. calcscore();
  15.  
  16. system("PAUSE");
  17. return 0; // Added a return
  18. }
  19.  
  20. int CheckIsMaxOrMin()
  21. {
  22. int count=0;
  23. for (int a=0; a < 5; a++)
  24. if ((IsMaxOrMin[a]==true)){count++;}
  25. return count;
  26. }
  27.  
  28. void getData()
  29. {
  30. cout << "Input 5 numbers : \n";
  31. cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];
  32. double minimum=input[0];
  33. for (int a=0; a < 5; a++)
  34. if (input[a] < minimum){minimum=input[a];}
  35. for (int a=0; a < 5; a++)
  36. if (input[a] == minimum && (CheckIsMaxOrMin()!=1)){IsMaxOrMin[a]=true;}
  37.  
  38. double maximum=input[0];
  39. for (int a=0; a < 5; a++)
  40. if (input[a] > maximum){maximum=input[a];}
  41. for (int a=0; a < 5; a++)
  42. if (input[a] == maximum && (CheckIsMaxOrMin()!=2)){IsMaxOrMin[a]=true;}
  43.  
  44. for (int a=0; a < 5; a++)
  45. cout << input[a] << " " << IsMaxOrMin[a] << endl;
  46. }
  47.  
  48. void calcscore()
  49. {
  50. double num[3]={0,0,0};
  51. for (int a=0, b=0; a < 5; a++)
  52. {
  53. if (!(IsMaxOrMin[a])){num[b]=input[a];b++;}
  54. }
  55. cout << "Average: " << (num[0]+num[1]+num[2])/3;
  56. cout<<"\n\n\n";
  57. }
// Skeen
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: violet101 is an unknown quantity at this point 
Solved Threads: 0
violet101 violet101 is offline Offline
Newbie Poster
 
0
  #8
27 Days Ago
#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.
Reply With Quote Quick reply to this message  
Reply

Tags
c++

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC