# If C++ statements

• March 1st 2013, 04:30 PM
sakonpure6
If C++ statements
Hi I have worked on a question where you input 3 positive number and the program gives you the middle of those so if i put in 1,2,3 it will give me 2. Here is the code but it is not complete. I do not know what I am missing because if I put the numbers : 9,6,7 or 4,2,3 it does not out put the correct number. Also if you think there is a better way of writing this code, please tell me!

Code:

```//Challenge 4#include <iostream> using namespace std; int main () {         int x;         int y;         int z;                 int counter=0;         int m=0;                                                     cout<<"Please input the weight of all three bowls:  ";                 cin>>x;                 cin>>y;                 cin>>z;         if(x>y&&y>z)                 m=y;         if(x>y&&y<z)                 m=z;         if(x>z&&y>z)                 m=y;         if(x>z&&y<z)                 m=z;         if(y>x&&x<z)                 m=z;         if(y>x&&x>z)                 m=x;         if(y>z&&z>x)                 m=z;         if(y>z&&z<x)                 m=x;         if(z>y&&y>x)                 m=y;         if(z>y&&y<x)                 m=x;         if(z>x&&y>x)                 m=y;         if(z>x&&y<x)                 m=x;         //does not work for 967 or 423 ....?? why?         cout<<m<<endl;         return 0;                         }```
• March 3rd 2013, 10:01 AM
veileen
Re: If C++ statements
Uhm, there are just $3!=6$ cases:

x > y > z
x > z > y
y > x > z
y > z > x
z > x > y
z > y > x

Code:

```#include<iostream> using namespace std; main () { int x, y, z, m; cout<<"Please input the weight of all three bowls: \n"; cin>>x>>y>>z; if (x>y && y>z)   m=y; if (x>z && z>y)   m=z; if (y>x && x>z)   m=x; if (y>z && z>x)   m=z; if (z>x && x>y)   m=x; if (z>y && y>x)   m=y; cout<<m; }```
For your code and x = 9, y = 6, z = 7:

Code:

```if(x>y&&y>z)   m=y; if(x>y&&y<z)   m=z; //m=7 if(x>z&&y>z)   m=y; if(x>z&&y<z)   m=z; if(y>x&&x<z)   m=z; if(y>x&&x>z)   m=x; if(y>z&&z>x)     m=z; if(y>z&&z<x)   m=x; if(z>y&&y>x)   m=y; if(z>y&&y<x)   m=x; //m=9 if(z>x&&y>x)   m=y; if(z>x&&y<x)   m=x;```
• March 3rd 2013, 10:16 AM
sakonpure6
Re: If C++ statements
thank you!