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;
}
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;
}
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.)