In the program AvgDiff in your notes, the array was created after the number of students were known so it was the size we needed. Often we do not know how many items we need to store in the array. In this case we create an array of the maximum size we might need, but only use as much of it as is required. Study the program AvgDiffa. In this program the user enters marks for each student and enters a sentinel value -1 when there are no more students to be processed.
In the first part of the program a count is maintained as each student mark is entered. That will also indicate how much of the array was filled. In the second half instead of using arrayname.length to indicate the end of the array we are interested only in the number of slots that actually contains marks so the student count is used in the for loop to determine when to stop.
Study the program AvgDiffa.java
It can be useful to work through code on paper and simulate what it does by noting the contents of variables. The worksheet
is designed to give you practice at distinguishing between the name of the array variable, the index to the array and the contents of the array. This is an exercise to be done on paper.