Level-up challenge · CSCD210

One program that builds, fills, walks, and reports on an array

You have finished the first five rooms of the catch-up path and you want to put the whole array pattern into one program before you go on to files.

Unlocked at the end of: Arrays and file I/O, from zero, after the room computing-while-you-walk. Room 5 is the last of the five array rooms. After it you have made an array, read and written one slot, walked every slot, and computed over the values you walked past.

Outcomes this shows6Inputs it is run on4Timeabout 45 minutesDifficulty3 of 3

What this shows you can do

Each line below is one outcome, written the way the course records it. A program that does the whole task shows all of them at once.

  1. Student can write a new-with-size construction for any primitive or reference element type, using a literal or variable size.

    In this program: The size is read at run time and differs in every check, so a construction with a literal length passes at most one of them, and the reported slot count comes from the array itself.

    Not shown by a run:"for any primitive or reference element type". This program builds one int[]. A reference element type is not in it.

  2. Student can predict the value read by a[i] and the effect of a[i] = v for any array a and in-range int expression i, including expressions that involve arithmetic on the index, and identify whether a given access is a read or a write from its position in the statement.

    In this program: Filling the array is a write in a loop, the first and last lines of the report are reads, and the last one needs arithmetic on the index.

    Not shown by a run:"identify whether a given access is a read or a write from its position in the statement". A student names that in words. A run cannot see it.

  3. Student can produce a traditional for loop header that visits every element of an array in order, using int i = 0, i < a.length, and i++, and explain which part of the loop body needs the index variable (versus only the value).

    In this program: Every reported number needs a loop that visits every slot exactly once, and the position of the largest value cannot be produced by a loop that never holds the index.

    Not shown by a run:"explain which part of the loop body needs the index variable". The program needs both kinds of loop body, so a correct run shows the student used them, but the explanation itself is not in the output.

  4. Student can identify i < a.length as the correct loop bound for a forward array traversal, explain the half-open-interval convention that motivates the strict <, and predict the runtime outcome of using <= instead.

    In this program: A bound of i <= xs.length ends the run with ArrayIndexOutOfBoundsException before the report is printed, at every one of the four sizes.

    Not shown by a run:"explain the half-open-interval convention" and "predict the runtime outcome of using <= instead". Both are said, not run.

  5. Student can produce a method that sums an int[] using the accumulator pattern (declare-before, add-inside, return-after), and a separate method that returns the average as a double with the correct cast to avoid integer division.

    In this program: Checks c1, c3 and c4 all have averages that are not whole numbers, so an average computed with integer division prints a different value in each.

  6. Student can produce a method that returns the smallest (or largest) element of an int[] using the best-so-far pattern, seeding from xs[0] and starting the loop at index 1, and identify why Integer.MAX_VALUE as a seed is the wrong choice in CSCD 210 style.

    In this program: Check c1 is all positive, so a smallest seeded with zero prints zero. Check c4 is all negative, so a largest seeded with zero prints zero. Check c3 has the largest value twice, which pins which index the report names.

    Not shown by a run:"identify why Integer.MAX_VALUE as a seed is the wrong choice in CSCD 210 style". A sentinel seed gives the right answer on every non-empty input, and every input here is non-empty, so no run can tell it apart.

What to build

Write one program named ReadingsReport.

It reads from standard input. The first number is how many readings follow. Each reading after that is a whole number, and there is at least one.

Store the readings in an int[] with exactly as many slots as there are readings. Then print eight lines, in this order, with the labels spelled exactly as shown here:

slots
first
last
sum
average
smallest
largest
largest at index

Each line is the label, a colon, one space, and the value. Print the average as Java prints a double, with no formatting. If the largest value appears more than once, report the first position where it appears. Print nothing else, and print no prompt.

The rest of the program is yours to shape.

Write these methods with these headers

One worked example

The first of the inputs, with what the program prints for it.

Five readings, all positive

All positive is what makes a smallest seeded with zero visible, and 348 over 5 is not a whole number, which is what makes integer division visible.

Input
5
71
68
78
62
69
Output
slots: 5
first: 71
last: 69
sum: 348
average: 69.6
smallest: 62
largest: 78
largest at index: 2

How your program is checked

Your program is run once for each input and its output is compared with what is shown. The worked example above is the first of them. You can run every one of them yourself before you hand anything in.

One reading

The smallest array that the spec allows. First and last are the same slot, and a best-so-far loop that starts at index 1 never runs.

Input
1
40
Output
slots: 1
first: 40
last: 40
sum: 40
average: 40.0
smallest: 40
largest: 40
largest at index: 0

Seven readings, mixed signs, with the largest value appearing twice

The repeat pins the first-position rule, and the average repeats forever, which is where integer division is most visible.

Input
7
-3
12
5
-8
20
1
20
Output
slots: 7
first: -3
last: 20
sum: 47
average: 6.714285714285714
smallest: -8
largest: 20
largest at index: 4

Three readings, all negative

All negative is what makes a largest seeded with zero visible, which the all-positive check cannot show.

Input
3
-12
-7
-25
Output
slots: 3
first: -12
last: -25
sum: -44
average: -14.666666666666666
smallest: -25
largest: -7
largest at index: 1

What to watch for

Each one is copied from the notes for the rooms this challenge draws on, with what a run shows when it happens.

What this puts on your resume

You have written Java that allocates an array at a size decided while the program runs, fills it from input, and computes a total, an average, a smallest value, a largest value, and the position of the largest in single passes over it.

Notes for the author, not part of the student page

This block is here so the manifest can be reviewed on the page instead of in the file. It is not rendered on a student page.

Outcomes considered and left out

Carried in from earlier rooms

Assumed from earlier in the course

Where the expected output came from

Provenance