Node:Arrays and for loops, Next:, Previous:Array bounds, Up:Arrays



Arrays and for loops

When you declare an array, the computer allocates a block of memory for it, but the block contains garbage (random values). Therefore, before using an array, you should initialise it. It is usually a good idea to set all elements in the array to zero.

The easiest way to initialise an array is with a for loop. The following example loops through every element in the array my_array and sets each to zero.

Remember, because arrays in C are zero-based, the indices of the array my_array in the example below run 0 through 9, rather than 1 through 10. The effect is the same, however: an array of ARRAY_SIZE (that is, 10) elements.

#include <stdio.h>
#define ARRAY_SIZE 10

int main ()
{
  int index, my_array[ARRAY_SIZE];

  for (index = 0; index < ARRAY_SIZE; index++)
    {
      my_array[index] = 0;
      printf ("my_array[%d] = %d\n", index, my_array[index]);
    }
  printf("\n");

  return 0;
}

The output from the above example is as follows:

my_array[0] = 0
my_array[1] = 0
my_array[2] = 0
my_array[3] = 0
my_array[4] = 0
my_array[5] = 0
my_array[6] = 0
my_array[7] = 0
my_array[8] = 0
my_array[9] = 0

You can use similar code to fill the array with different values. The following code example is nearly identical to the one above, but the line my_array[index] = index; fills each element of the array with its own index:

#include <stdio.h>
#define ARRAY_SIZE 5

int main ()
{
  int index, my_array[ARRAY_SIZE];

  for (index = 0; index < ARRAY_SIZE; index++)
    {
      my_array[index] = index;
      printf ("my_array[%d] = %d\n", index, my_array[index]);
    }
  printf("\n");

  return 0;
}

The output is as follows:

my_array[0] = 0
my_array[1] = 1
my_array[2] = 2
my_array[3] = 3
my_array[4] = 4

Here is a human's-eye view of the internal representation of the array (how the array "looks" to the computer):

index        0   1   2   3   4
            -------------------
element    | 0 | 1 | 2 | 3 | 4 |
            -------------------

You can use loops to do more than initialize an array. The next code example demonstrates the use of for loops with an array to find prime numbers. The example uses a mathematical device called the Sieve of Erastosthenes. Erastosthenes of Cyrene discovered that one can find all prime numbers by first writing down a list of integers from 2 (the first prime number) up to some arbitrary number, then deleting all multiples of 2 (which are by definition not prime numbers), finding the next undeleted number after 2 (which is 3), deleting all its multiples, finding the next undeleted number after that (5), deleting all its multiples, and so on. When you have finished this process, all numbers that remain are primes.

The following code example creates a Sieve of Erastosthenes for integers up to 4999, initializes all elements with 1, then deletes all composite (non-prime) numbers by replacing the elements that have an index equal to the composite with the macro DELETED, which equals 0.

#include <stdio.h>

#define ARRAY_SIZE 5000
#define DELETED    0

int sieve[ARRAY_SIZE];


int main ()
{
  printf ("Results of Sieve of Erastosthenes:\n\n");

  fill_sieve();
  delete_nonprimes();
  print_primes();
}


fill_sieve ()
{
  int index;

  for (index = 2; index < ARRAY_SIZE; index++)
    sieve[index] = 1;
}


delete_nonprimes ()
{
  int index;

  for (index = 2; index < ARRAY_SIZE; index++)
    {
      if (sieve[index] != DELETED)
	delete_multiples_of_prime (index);
    }
}


delete_multiples_of_prime (int prime)
{
  int index, multiplier = 2;

  for (index = prime * multiplier; index < ARRAY_SIZE; index = prime * multiplier++)
    sieve[index] = DELETED;
}


print_primes ()
{
  int index;

  for (index = 2; index < ARRAY_SIZE; index++)
    {
      if (sieve[index] != DELETED)
	printf ("%d ", index);
    }
  printf("\n\n");
}

Part of the output from the above program is shown below, for values up to 500. (The full output is considerably longer.)

Results of Sieve of Erastosthenes:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191
193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283
293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 ...