Creating an Array
In the previous lesson, on loops, you learned to repeat code. In this lesson, you'll learn to store many values under one name.
Imagine you want to keep your points from five tests. With what you know so far, you would need five variables:
int score1 = 80;
int score2 = 92;
int score3 = 75;
int score4 = 60;
int score5 = 88;That's a lot of typing, and thirty tests would need thirty variables. Worse, a loop can't go through them: score1 and score2 are just two names that have nothing to do with each other. That's what an array is for. An array stores several values of the same type under one name:
int scores[5] = {80, 92, 75, 60, 88};The values in an array are its elements. This array has five elements, in a fixed order: 80 is the first one, 88 the last one.
Let's go through the line from left to right:
intis the data type of the elements. All elements of an array have the same type, so this array holds only whole numbers.scoresis the name of the array. The same rules apply to it as to the names of variables.[5]in square brackets is the size of the array: the number of its elements.- The curly braces
{ }hold the elements, separated by commas.
You don't have to write the size yourself. If you leave the square brackets empty, the compiler counts the values in the braces and makes the array exactly that big:
int scores[] = {80, 92, 75, 60, 88};This is the same array of five elements. Arrays of other types are created the same way, for example the prices in a shop or the answers to a quiz:
double prices[] = {29.90, 149.00, 99.50};
char answers[] = {'a', 'c', 'b'};An array of int holds only whole numbers. If you write the decimal number 92.5 into it, the element gets just 92. The decimal part is cut off, and no error message tells you.
The size of an array is fixed. It's set when the array is created and can't change later: you can't add a sixth element to scores. Even if you don't know the values yet, you must say how many elements there will be. If you write neither the size nor the values, as in int scores[];, the program doesn't compile:
main.c:4:9: error: definition of variable with array type needs an explicit size or an initializerThe message says that an array variable needs an explicit size or starting values (an initializer). The compiler must know how much room in the computer's memory to set aside for the array.
To print one element, write its number in square brackets after the array's name. scores[0] is the first element of scores:
int scores[] = {80, 92, 75, 60, 88};
printf("%d\n", scores[0]);This prints:
80Why the first element has the number 0, you'll find out in the next exercise. The program in the editor prints the goals from the first match in the same way.
Instructions
Below the comment, create an array of whole numbers named scores with the size 4 and the elements 85, 92, 78 and 90, in this order.
Below the array, print the first score with a label, the same way the program prints the goals from the first match. The output should include the line First score: 85.
Start programming for free
1/12