Initializing an Array
An array is a set (group) of numbers. (C does not allow for arrays of characters or strings. Use Perl for these.) Here is a sample array:
int years[3] = { 1800, 1900, 2000 };
This array is a set (group) of three years: 1800, 1900, and 2000. Each of these is called an element. I put the number of elements in square brackets, like this:
int years[3]
The individual elements are written inside curly braces like this:
{ 1800, 1900, 2000 }
Together, these initialize the array:
int years[3] = { 1800, 1900, 2000 }
Isolating Elements
To effectively use an array, I must isolate the elements inside the array. Each element has anindex number, which refers to the element’s position in the array.
Index numbers in C (and other languages, such as Perl) begin with the number 0. This means that the first element is in index 0, the second is in index 1, and the third is in index 2. We write index numbers with square brackets, like this:
years[0]
years[1]
years[2]
In our array of years, int years[3] = { 1800, 1900, 2000 }; we can see that:
years[0] = 1800
years[1] = 1900
years[2] = 2000
So, if I wanted to print a sentence with 1900 in it, I would use the index number, like this:
printf(“\nRodin exhibited some of his best sculptures in %d.”, years[1]);
Using an Array
Let’s write a C program that uses an array. During the early 1900s explorers sailed to Antarctica and tried to reach the South Pole by foot and by sled (or “sledge” if you’re British). This script gives a timeline for these expeditions:
#include<stdio.h> void main( ) { int years[3] = { 1901, 1907, 1911 }; printf(“\nThere were many great years of Antarctic exploration.”); printf(“\nIn %d Scott and Shackleton almost reached the South Pole.”, years[0]); printf(“\nIn %d Shackleton came within 97 miles of the South Pole.”, years[1]); printf(“\nIn %d Amundsen narrowly beat Scott to the Pole.”, years[2]); }
|
As you can see, we use the index number to isolate each element in the array. Save your source code as explore.c. Compile it. Link it. Run it.
Calculations from an Array
Now that we understand index numbers, we can use array elements in calculations, like this:
years = voyages[3] – voyages[0];
Let’s use this calculation in a program. One of the greatest Antarctic explorers was Sir Ernest Shackleton. Here is a program that recounts his voyages:
#include<stdio.h> void main( ) { int voyages[4] = { 1901, 1907, 1915, 1921 }; int years; printf(“\nSir Ernest Shackleton made four voyages to Antarctica:”); printf(“\n1. as part of Scott’s %d Discovery trip”, voyages[0]); printf(“\n2. leading the Nimrod expedition in %d”, voyages[1]); printf(“\n3. keeping his crew alive on the ice after the Endurance was crushed in %d”, voyages[2]); printf(“\n4. and dying en route to Antarctica on the Quest in %d.”, voyages[3]); years = voyages[3] – voyages[0]; printf(“\nShackleton spent %d years in Antarctic exploration.\n”, years); }
|
Save your source code as shack.c. Compile it. Link it. Run it.
The final sentence should say: Shackleton spent 20 years in Antarctic exploration.
Leave a comment