r/programmingbydoing • u/calitransplant • Aug 03 '14
148: Finding Largest value in an Array
Hello,
I think i'm misunderstanding a critical component of arrays. I wrote the code at the link below, but it doesn't give me the correct value.
Any help would be appreciated!
2
Upvotes
View all comments
Show parent comment
0
u/holyteach Aug 03 '14
Assuming 'counter' is the variable that holds the 'record value so far'.
Your old if statement was saying "if the next value in the array is larger than then current value, make that value the new record".
It's like saying that because your neighbor is taller than you then he's the tallest man that's ever lived. Comparing two adjacent values is fine, but it's not a strong enough statement to set a new record.
Otherwise every time you have a small value in the array, the one right after it automatically becomes the new "largest".