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.
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.
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.Student can predict the value read by
a[i]and the effect ofa[i] = vfor any arrayaand in-rangeintexpressioni, 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.
Student can produce a traditional
forloop header that visits every element of an array in order, usingint i = 0,i < a.length, andi++, 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.
Student can identify
i < a.lengthas 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.lengthends 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.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 adoublewith 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.
Student can produce a method that returns the smallest (or largest) element of an
int[]using the best-so-far pattern, seeding fromxs[0]and starting the loop at index 1, and identify whyInteger.MAX_VALUEas 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_VALUEas 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 indexEach 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
- public static int sum(final int[] xs)
- public static double average(final int[] xs)
- public static int smallest(final int[] xs)
- public static int largest(final int[] xs)
- public static int indexOfLargest(final int[] xs)
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.
5
71
68
78
62
69
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.
1
40
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.
7
-3
12
5
-8
20
1
20
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.
3
-12
-7
-25
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.
putting a number in the brackets on the left
What a run shows: Nothing runs. The compiler stops at the declaration.
indexing from 1 instead of from 0
What a run shows: A fill loop that starts at 1 and stops at the count ends the run with ArrayIndexOutOfBoundsException, and a report that reads
xs[1]as the first value prints the second reading on the first line.confusing array access
xs[i]with method callxs.get(i)What a run shows: Nothing runs. The compiler stops at the call.
copying the loop with
<=from a non-array contextWhat a run shows: The run ends with ArrayIndexOutOfBoundsException and prints no report, at every one of the four input sizes.
forgetting to update
iWhat a run shows: The run does not end on its own and the sandbox stops it at the CPU limit, so there is no output to compare.
declaring the accumulator inside the loop
What a run shows: Nothing runs. The accumulator is out of scope at the return.
writing
return total / xs.length;and expecting adoubleresultWhat a run shows: The average line prints the truncated value. On check c1 it prints 69.0 where the captured output is 69.6.
seeding
bestwith0What a run shows: The smallest line prints 0 on check c1, where every reading is positive, and the largest line prints 0 on check c4, where every reading is negative.
confusing the value-of-min with the index-of-min
What a run shows: The last line prints a reading instead of a position. On check c1 it prints 78 where the captured output is 2.
starting the loop at
i = 0after seeding fromxs[0]What a run shows: Nothing. The first comparison is a value against itself, so the answer is right and all four checks pass. Only a reader sees this one.
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.
Mark it built
Nothing on this page runs your program. You run it yourself, once for each input above, and compare what it prints with what is shown. When all of them match, check the box. The mark is your own note about your own work, and it stays in this browser.
Unchecking the box takes the mark back off. Each outcome above then reads whatever the rest of your work shows.