Arrays are special variables which can hold more than one value using the same variable, using an index. Arrays are defined in a very
/* defines an array of 10 integers */
Accessing a number from the array is done using the same syntax. Notice that arrays in C are zero-based, which means that if we
defined an array of size 10, then the array cells 0 through 9 (inclusive) are defined.
numbers is not an actual value.
/* populate the array */
numbers = 10;
numbers = 20;
numbers = 30;
numbers = 40;
numbers = 50;
numbers = 60;
numbers = 70;
/* print the 7th number from the array, which has an index of 6 */
printf("The 7th number in the array is %d", numbers);
Arrays can only have one type of variable, because they are implemented as a sequence of values in the computer's memory.
Because of that, accessing a random array cell is very efficient.
- The code below does not compile, because the
grades variable is missing.
- One of the grades is missing. Can you define it so the grade average will be 85?