Results 1 to 3 of 3
Like Tree1Thanks
  • 1 Post By veileen

Math Help - If C++ statements

  1. #1
    Senior Member sakonpure6's Avatar
    Joined
    Sep 2012
    From
    Canada
    Posts
    416
    Thanks
    32

    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;
    		
    	}
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2011
    From
    Awetuouncsygg
    Posts
    182
    Thanks
    12

    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;
    Last edited by veileen; March 3rd 2013 at 10:09 AM.
    Thanks from sakonpure6
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Senior Member sakonpure6's Avatar
    Joined
    Sep 2012
    From
    Canada
    Posts
    416
    Thanks
    32

    Re: If C++ statements

    thank you!
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Statements with ln
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: November 30th 2012, 06:11 AM
  2. if then statements
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: October 24th 2010, 10:13 AM
  3. Please Help - statements about functions
    Posted in the Calculus Forum
    Replies: 4
    Last Post: December 13th 2009, 08:37 AM
  4. Statements
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: October 23rd 2009, 09:48 AM
  5. T or F statements
    Posted in the Discrete Math Forum
    Replies: 9
    Last Post: March 29th 2009, 03:36 PM

Search Tags


/mathhelpforum @mathhelpforum