How Many Things Can You Count Up To On Your Fingers?

Let’s talk about zero — that pesky number!

--

For the count on your fingers? Well, let’s try ..

0 … 1 … 2 … 3 … 4 … 5 … 6 … 7 … 8 … 9 … 10

That’s 11! That pesky zero has managed to muscle itself into our counting. Zero, itself, is a valid number of our count, as we need to tell someone that we have no items.

But zero causes many problems in programming, as we need to know where we start our indexing. Should we start at zero and count up, or should we start at 1? And, if we want n elements, do we count up to n, or n-1? This is especially important when we are indexing our array elements, as we compute the location in memory and which relates to its index value and the number of bytes that each element represents.

In Pascal, to count from 1 to 10:

n: array [1..10] of integer;
for i:= 1 to 10 do
writeln('[', i, '] = ', n[i] );

But, in C, we would use:

  int n[5];

for(int i = 0; i < 5; ++i) {
printf("%d", n[i]);
}

In the C example, our last element is n[4] and which is the fifth element. A standard novice issue is to access the n[5] element (and which is unallocated):

int n[5];

for(int…

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.