Write a function called setToMin() that will work with the following section of
ID: 3560323 • Letter: W
Question
Write a function called setToMin() that will work with the following section of code. It works for any integer variable and will set it to the lowest of the subsequent three integer parameters. You don't need to put in the prototype, but you need to write a complete function. All the variables are integers.
int x, y, z;
setToMin(x, 1, 4, -6);
// the value of x is now -6
setToMin(y, 4, 2, 8);
// the value of y is now 2
setToMin(z, -9, -8, -7);
// the value of z is now -9
Please make sure you enter the code using the html editor link!
Explanation / Answer
void setToMin(int & a, int first, int second, int third)
{
if( first <= second && first <= third)
a = first;
else if( second <= first && second <= third)
a = second;
else
a = third;
}
This is passing your int x, y, or z by reference to the function, which means that the function can change it. Obviously, x, y, or z will be pass into a, and your three other ints (I'm assuming they are to be user input) will be passed in as first, second, and third.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.