adeslauriers89
adeslauriers89

Reputation: 91

Objective-C: How to find the highest value in an array without sorting?

Im working on an assignment in which I have to find the highest value in an array using a for loop, and without sorting it. I feel like what I have so far is close to being correct but not quite there. Below is an example of the code I have so far.

NSArray *unsortedArray = @[ @2, @44, @11, @99, @35 ];


for (int i = 0; i < 4 ; i++) {
    id number = [unsortedArray objectAtIndex:i];
    id largestNumber = 0;
    if (largestNumber < number) {
        largestNumber = number;


        NSLog(@"%@ is the largest number", largestNumber );

  return 0;
    }

}

Now this is only returning the first value in the area then the loop is stopping, am I assigning the wrong values to largestNumber and number? Very new to code, so I would appreciate and feedback and examples.

Thanks!

Upvotes: 7

Views: 7450

Answers (4)

Micrified
Micrified

Reputation: 3650

KVC answer is best, but if you want to do it with a loop..

NSArray *unsortedArray = @[ @2, @44, @11, @99, @35 ];

NSNumber *l = [unsortedArray objectAtIndex:0];
for (int i = 1; i < 5; i++) {
    l = ([[unsortedArray objectAtIndex:i]integerValue] > [l integerValue] ? [unsortedArray objectAtIndex:i]:l);
}
NSLog(@"Largest = %@\n",l);

Also, given you have five objects in the array, your loop invariant must test for i < 5, not i < 4. That will stop at index 3, when you should stop after index 4. Finally, this compares integer values only as it stands, if you want to compare floating point or other numeric types. You should remember to specify the appropriate property. You can see them all here.

Edit: (Downvotes for what? This answer is correct..)

To clarify since you're new, an if statement can also be written as follows: a = (b > c ? (return this if true):(return this if false)

Upvotes: 1

CStreel
CStreel

Reputation: 2642

I am ignoring the return 0, as I am unsure about what your purpose is. But you need to declare id largestNumber outside the loop to keep tracking it

NSArray *unsortedArray = @[ @2, @44, @11, @99, @35 ];

NSNumber *largestNumber = @0;
for (int i = 0; i < [unsortedArray count] ; i++) {
    NSNumber *number = [unsortedArray objectAtIndex:i];
    if ([number isGreaterThan:largestNumber]) {
        largestNumber = number;
    }
}
NSLog(@"%@ is the largest number", largestNumber );

This will keep track of changes to the largestNumber array and then log the result after it's finished iterating.

There is also another approach which uses foreach approach

NSLog(@"%@ is the largest number", largestNumber );
NSNumber *largestNumber = @0;
for (NSNumber *number in unsortedarray) {
    if ([number isGreaterThan:largestNumber]) {
        largestNumber = number;
    }
}
NSLog(@"%@ is the largest number", largestNumber );

If you want me to explain foreach, just comment

Upvotes: 0

vadian
vadian

Reputation: 285072

Key-Value-Coding solution, no loop

NSArray *unsortedArray = @[ @2, @44, @11, @99, @35 ];
NSNumber *maxNumber = [unsortedArray valueForKeyPath:@"@max.self"];
NSLog(@"%@", maxNumber); // 99

See this interesting article by Mattt Thompson: KVC Collection Operators

Upvotes: 24

Adam B
Adam B

Reputation: 3833

You've got too much inside the for loop.

You need to put the declaration of largestNumber outside the for loop. Also you might be missing a } since the "return 0" in the if statement will exit the entire function immediately and since its inside the for loop it exits on the first pass through.

id largestNumber = 0;

for (int i = 0; i < 4 ; i++) {
    id number = [unsortedArray objectAtIndex:i];

    if (largestNumber < number) {
        largestNumber = number;
    }
}

NSLog(@"%@ is the largest number", largestNumber );
return 0;  // Not sure if you even need this

Upvotes: -1

Related Questions