Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 4 variables to find the lowest C++

Tags:

c++

I want to find the lowest number of the four, but this looks kinda wierd , isnt there a smarter and shorter way to do it?

That is what I have:

int findlowest(int one, int two, int three, int four) {
    int output = one //as of now , we will be outputting one , except if we find a lower score.
    if(output > two) { out = two;} // if output is proven to be bigger than two, two is our new output.
    if(output > three){ output = three;} //same operation with three
    if(output > four){ output = four;} // same operation with four
    return output;
}
like image 233
aviau Avatar asked Jul 05 '11 06:07

aviau


People also ask

How do you find the minimum of 4 numbers in C++?

std::min in C++ std::min is defined in the header file <algorithm> and is used to find out the smallest of the number passed to it. It returns the first of them, if there are more than one.

How do you compare three variables in C?

Comparing three integer variables is one of the simplest program you can write at ease. In this program, you can either take input from user using scanf() function or statically define in the program itself. We expect it to be a simple program for you as well.

How do you compare variables with multiple values?

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable.


1 Answers

c++11:

int minimum = std::min( { 1,2,3,4,5 } );
like image 69
log0 Avatar answered Oct 05 '22 04:10

log0