By Dan Gookin

An array in the C programming language is series of variables of the same type: a dozen int variables, two or three double variables, or a string of char variables. The array doesn’t contain all the same values. No, it’s more like a series of cubbyholes into which you stick different values.

An array is declared like any other variable. It’s given a type and a name and then also a set of square brackets. The following statement declares the highscore array:

int highscore[];

This declaration is incomplete; the compiler doesn’t yet know how many items, or elements, are in the array. So if the highscore array were to hold three elements, it would be declared like this:

int highscore[3];

This array contains three elements, each of them its own int value. The elements are accessed like this:

highscore[0] = 750;
highscore[1] = 699;
highscore[2] = 675;

An array element is referenced by its index number in square brackets. The first item is index 0, which is something you have to remember. In C, you start counting at 0, which has its advantages, so don’t think it’s stupid.

In the preceding example, the first array element, highscore[0], is assigned the value 750; the second element, 699; and the third, 675.

After initialization, an array variable is used like any other variable in your code:

var = highscore[0];

This statement stores the value of array element highscore[0] to variable var. If highscore[0] is equal to 750, var is equal to 750 after the statement executes.

HIGH SCORES, THE AWFUL VERSION

#include <stdio.h>
int main()
{
  int highscore1,highscore2,highscore3;
  printf("Your highest score: ");
  scanf("%d",&highscore1);
  printf("Your second highest score: ");
  scanf("%d",&highscore2);
  printf("Your third highest score: ");
  scanf("%d",&highscore3);
  puts("Here are your high scores");
  printf("#1 %dn",highscore1);
  printf("#2 %dn",highscore2);
  printf("#3 %dn",highscore3);
  return(0);
}

Exercise 1: Rewrite the source code from High Scores, the Awful Version, adding a fourth high score and using an array — but keep in mind that your array holds four values, not three.

Many solutions exist for Exercise 1. The brute-force solution has you stuffing each array variable individually, line after line, similar to the source code in High Scores, the Awful Version. A better, more insightful solution is offered in High Scores, a Better Version.

HIGH SCORES, A BETTER VERSION

#include <stdio.h>
int main()
{
 int highscore[4];
 int x;
 for(x=0;x<4;x++)
 {
 printf("Your #%d score: ",x+1);
 scanf("%d",&highscore[x]);
 }
 puts("Here are your high scores");
 for(x=0;x<4;x++)
 printf("#%d %dn",x+1,highscore[x]);
 return(0);
}

Most of the code from High Scores, a Better Version should be familiar to you, albeit the new array notation. The x+1 arguments in the printf() statements (Lines 10 and 16) allow you to use the x variable in the loop but display its value starting with 1 instead of 0. Although C likes to start numbering at 0, humans still prefer starting at 1.

Exercise 2: Type the source code from High Scores, a Better Version into your editor and build a new project. Run it.

Though the program’s output is pretty much the same as the output in Exercise 1, the method is far more efficient, as proven by working Exercise 3:

Exercise 3: Modify the source code from High Scores, a Better Version so that the top ten scores are input and displayed.

Imagine how you’d have to code the answer to Exercise 3 if you chose not to use arrays!

  • The first element of an array is 0.

  • When declaring an array, use the full number of elements, such as 10 for ten elements. Even though the elements are indexed from 0 through 9, you still must specify 10 when declaring the array’s size.