Section 17.1 Arrays of Integers
Up to this point we have been using
singular data types, such as
int i, j, k;
float x, y, z;
double u, v, w;
Often situations arise in which it is more natural to store a
collection of values together in a
single variable .
We will first focus on storing integers in an array. For example, we may wish to store all students’ grades on an exam or the coordinates for an object or the values of a function at regular intervals, etc.
An
array (think of a collection of data items of the
same type ) is just what we need to accomplish this efficiently.
If you cannot see this codecast, please click here .
Video Description.
Declaring arrays of integers
Assigning values to arrays
NEW SYNTAX: int array[3];
creates a variable named array and reserves space to hold three integers
NEW SYNTAX: array[0] = 16;
initializes the first entry in the array with the value "16"
Arrays in C are zero-based
Check Your Understanding Check Your Understanding
1.
How much space is reserved with the following declaration:
int numbers[10];
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Now that we know how to create these arrays, how do we actually use them?
If you cannot see this codecast, please click here .
Video Description.
Arrays and for
-loops
Reading values from the user input into an array
NEW SYNTAX: you can fill the array using a for
-loop, where you use the following line to fill each array element
scanf("%d", &array[i]);
Check Your Understanding Check Your Understanding
1.
int num[50];
what is the value of
num[5]
?
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!