Common Array Algorithms

This chapter discusses how a for loop can be used to iterate over the elements of a one-dimensional array. In addition, it also discusses enhanced for loops. The chapter demonstrates the use of arrays to solve common problems such as finding sum, average, maximum, and minimum values of numbers stored in array. Pay attention to some of the common programming errors one can make while using arrays.

11. Classic Bug (2): Big Negatives


Answer:

The maximum is   -12.

Classic Bug (2): Big Negatives

numberLineTwo

Classic Bug (2): It is tempting to say that the maximum is -92, since -92 seems to be a bigger number than -12. But this confuses the absolute value of a number with its position on the number line. If you are looking for the maximum of a list of numbers, you are looking for the number which is furthest to the right on the number line, even if the number is negative.

Think of this as pay. If Jill is paid -92 dollars a day and Joe is paid -12 dollars a day, who is paid the most? (And exactly which fast food restaurant do they work at?)

The minimum of a list of numbers is the one furthest to the left on the number line.

Question 11:

What is the minimum of our list of integers:

-45,  -23,  -87,  -56,  -92,  -12,  -36