Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min . Assume that all the variables have already been declared and that x , y , and z have been assigned values ).

LANGUAGE: C++

CHALLENGE:

Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min . Assume that all the variables have already been declared and that x , y , and z have been assigned values ).

SOLUTION:

The Bad News: The content you are trying to access is only available to members.

The Good News: You too can be a member, simply Click Here.

Already a member?

Posted in ,

5 Comments

  1. Steve Bobson on June 23, 2015 at 4:59 am

    also works

    min = x<y?x:y;
    min = min<z?min:z;

  2. Mark Goyette on June 29, 2015 at 4:04 pm

    This is more strait foward.

    if (x < z && x < y)
    {
    min = x;
    }
    else if (y < z)
    {
    min = y;
    }
    else
    {
    min = z;
    }

  3. Alexandre Guay on October 31, 2016 at 6:14 am

    I’m fairly certain the first posted answer is wrong. In the case where x = 2, y = 6, z = 4, the first part of the code would give us a wrong answer.

    • Phil on December 8, 2016 at 6:11 pm

      it works, i tried it.

  4. Mike on January 30, 2017 at 10:12 pm

    could you do

    min = x;

    if(y < min){
    min = y;
    }
    if(z < min){
    min = z;
    }

Leave a Comment

You must be logged in to post a comment.