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.
我正在处理一个赋值,在这个赋值中,我必须使用for循环查找数组中最高的值,并且不进行排序。我觉得到目前为止,我已经接近正确,但还没有完全正确。下面是我到目前为止的代码示例。
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!
谢谢!
4 个解决方案
#1
15
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
参见马特·汤普森的这篇有趣的文章:KVC集合操作符
#2
0
KVC answer is best, but if you want to do it with a loop..
KVC的答案是最好的,但是如果你想做一个循环…
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.
而且,如果数组中有5个对象,那么循环不变量必须测试i < 5,而不是i < 4。这将在索引3结束时停止,在索引4之后停止。最后,如果您想比较浮点数或其他数值类型,那么只能按当前的情况比较整数值。您应该记住指定适当的属性。你可以在这里看到它们。
Edit: (Downvotes for what? This answer is correct..)
编辑:(Downvotes为了什么?这个答案是正确的. .)
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)
为了澄清你是新手,if语句也可以写成:a = (b > c ?(如果为真,返回此):(如果为假,返回此)
#3
-1
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
我忽略了return 0,因为我不确定您的目的是什么。但是您需要在循环之外声明id最大编号,以继续跟踪它
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
还有另一种方法使用foreach方法。
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
如果你想让我解释一下,请评论。
#4
-2
You've got too much inside the for loop.
在for循环中有太多内容了。
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.
您需要将最大数量的声明放在for循环之外。同样,如果if语句中的“return 0”将立即退出整个函数,并且由于它位于for循环中,所以它在第一次通过时退出。
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
#1
15
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
参见马特·汤普森的这篇有趣的文章:KVC集合操作符
#2
0
KVC answer is best, but if you want to do it with a loop..
KVC的答案是最好的,但是如果你想做一个循环…
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.
而且,如果数组中有5个对象,那么循环不变量必须测试i < 5,而不是i < 4。这将在索引3结束时停止,在索引4之后停止。最后,如果您想比较浮点数或其他数值类型,那么只能按当前的情况比较整数值。您应该记住指定适当的属性。你可以在这里看到它们。
Edit: (Downvotes for what? This answer is correct..)
编辑:(Downvotes为了什么?这个答案是正确的. .)
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)
为了澄清你是新手,if语句也可以写成:a = (b > c ?(如果为真,返回此):(如果为假,返回此)
#3
-1
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
我忽略了return 0,因为我不确定您的目的是什么。但是您需要在循环之外声明id最大编号,以继续跟踪它
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
还有另一种方法使用foreach方法。
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
如果你想让我解释一下,请评论。
#4
-2
You've got too much inside the for loop.
在for循环中有太多内容了。
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.
您需要将最大数量的声明放在for循环之外。同样,如果if语句中的“return 0”将立即退出整个函数,并且由于它位于for循环中,所以它在第一次通过时退出。
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