[cs13001] No quality explanation on partially filled arrays.

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Oct 16 16:37:08 EDT 2012


> 
> Can you just explain to me the idea in a bit of detail? I understand
> that we don't usually know the size of the array that we'd want, but
> how exactly do we program it?
> 
> I don't understand the whole score[number_used - 1], why do we need the -1 there? 
> 
> What I understand is, we signal the end of the numbers to be
> implemented in the array by inputting a -1 (for example after
> inputting 5 numbers needed out of a possible maximum of 10).
> 
> What I don't understand is the fact that there are 3 functions, all
> of which use the number_used argument, but only one of them has it
> as a call by reference. Shouldn't all of them be used as a call by
> reference? For consistency, and if ever we call a function inside
> another function?

You cannot change the number of elements in an array once it is
declared. So, if an array is partially filled, there needs to be some
way to distinguish used elements of the array from the unused
ones. One way is to declare a variable that will contain the number of
used elements. For example number_used.  Another way is to fill the
unused elements with a value that used elements cannot hold. For
example, we used -1 as an unused value for the lottery number and for
a ship's coordinate in the labs.

Now, in 

   score[number_used - 1]

minus one, probably refers to the index of the last used (filled)
element. For example, if array "score" has altogether 10 elements, and
5 of them are used, then the index of the last used element is 5-1=4.

Thanks,
--
Mikhail


More information about the cs13001 mailing list