Algorithm for max and min? (Objective-C)
Asked Answered
M

4

5

This is a part of a book I'm reading to learn Objective-C.

The following defines a macro called MAX that gives the maximum of two values: #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )

And then there are some exercises in the book that asks the reader to define a macro (MIN) to find the minimum of two values and another that asks to define a macro called MAX3 that gives the maximum of 3 values. I think these two definitions will look similar to MAX, but I don't understand how the MAXformula finds the maximum value. I mean if I just did this

int limits = MAX (4,8)

It'll just assign limits the value of 8. What does that have to do with finding a variable's maximum value?

Maria answered 29/7, 2012 at 16:3 Comment(2)
Please give the title of the book so that others may avoid it - no modern book on C/Objective-C/C++ should be advocating the use of macros for cases where an inline function would be appropriate.Bevy
Read it literally. The answer is right there in yellow: "that gives the maximum OF two values". There are only two values to choose from, what is the maximum it can return.Illustrate
A
12

I think you are confusing value and variable. The macro example you listed expands to a comparison between two values and returns the greater of the two values (i.e. which is greater, a or b). So you are right, int limits = MAX(4,8) just assigns 8 to limits and has nothing to do with finding the maximum value you can store in limits.

The header limits.h defines many values like INT_MAX that will tell you information about the min/max values of variable types on your system.

Amniocentesis answered 29/7, 2012 at 16:14 Comment(0)
F
12

To break it apart:

The declaration:

#define MAX(a,b)

If a is greater than b, use a else use b:

( ((a) > (b)) ? (a) : (b) )

Then to create a MIN expression, use a similar form:

#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
                        ^

Then to create a MAX3 expression, you can combine them:

#define MAX3(a,b,c) ( MAX(a, MAX(b,c)) )

Specifically, this macro's intended to be used with scalars (C builtins) which can be compared using < or >. If you passed an objc variable, it would result in comparison of addresses and MAX would return the one with the higher address (it would be very rare if you actually wanted to compare addresses of objc instances).

Also note that this is the classic example of how macros can bite you. With macros, the preprocessor simply expands (textual copy/paste) the parameters in place, so: int limits = MAX (4,8) literally expands to int limits = (4 > 8 ? 4 : 8). If you write MAX(x,++y), then y will be incremented twice if y is greater than or equal to x because it expands to: int limits = (x > ++y ? x : ++y).

Frederick answered 29/7, 2012 at 16:20 Comment(0)
L
0

generally, you will use a MAX() or MIN() macro to get whichever is the higher/lower of a pair of variables, or of a variable and a constant, or even a pair of macro constants or other non-literal constant expressions. you generally won't supply 2 literal constants as you have done in your question.

Libove answered 29/7, 2012 at 16:11 Comment(1)
Unless the constants are in inferred from a constexpr function or a #define...Fezzan
S
-1

Algorithm for max (Objective-C)

 // get max value
- (float)maxValue:(NSArray *)arrValue
{
    float maxValue = 0.0;
    for (NSString *value in arrValue) {
        float compareValue = [value floatValue];

        if (compareValue > maxValue) {
            maxValue = compareValue;
        }

    }

    return maxValue;
}


NSArray *number=[NSArray arrayWithObjects:[NSNumber numberWithFloat:57.02], [NSNumber numberWithFloat:55.02], [NSNumber numberWithFloat:45.02], nil];

NSLog(@"%f", [self maxValue:number]);

result 57.020000
Summitry answered 8/4, 2015 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.