Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

three integers compare

I have three integers

I would like to determine what is the highest and which is the lowest value using Objective-C

Thank you!

like image 553
EarlGrey Avatar asked Dec 10 '10 19:12

EarlGrey


People also ask

How do you compare three values?

To compare 3 values, use the logical AND (&&) operator to chain multiple conditions. When using the logical AND (&&) operator, all conditions have to return a truthy value for the if block to run. Copied!

How do you compare three things 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.

Can you compare 3 numbers in Java?

We can also compare all the three numbers by using the ternary operator in a single statement. If we want to compare three numbers in a single statement, we must use the following statement. In the following program, we have used a single statement to find the largest of three numbers.


1 Answers

It is good to store that numbers in an array. Just plain C array is good enough and in Objective-C best for performance. To find a minimum you can use this function. Similar for maximum.

int find_min(int numbers[], int N){
    int min = numbers[0];
    for(int i=1;i<N;i++)
        if(min>numbers[i])min=numbers[i];

    return min;
}

If that is just three numbers you can do the comparisons manually for best performance. There is a MIN() and MAX() macro in Cocoa in Foundation/NSObjCRuntime.h. For the maximum, just do:

int m = MAX(myI1, MAX(myI2, myI3));

This may be scaled to more numbers and may be faster than the first approach using loop.

like image 69
Juraj Blaho Avatar answered Jan 02 '23 19:01

Juraj Blaho