Regent University College of Science & Technology
LECTURE 5
ARRAYS
A way to organize data
1
WHAT ARE ARRAYS?
An array is a series of compartments to
store data.
Technology
Regent University College of Science &
Each compartment is appropriately sized
for the particular data type the array is
declared to store.
An array can hold only one type of
data!
E.g. int[] can hold only integers
char[] can hold only characters
2
ARRAY VISUALIZATION
Specifies an array of
variables of type int
Technology
Regent University College of Science &
int primes[10]; /* An array of 10 integers*/
The name of
the array
index values
primes[0] primes[1] primes[2] primes[3] primes[4] primes[9]
3
DECLARING AN ARRAY
VARIABLE
Array declarations use square brackets.
Technology
Regent University College of Science &
type array_name[size];
For example:
int prices[20];
char grades[10];
4
ARRAY INDEXES
Everycompartment in an array is assigned an
Technology
Regent University College of Science &
integer reference.
number is called the index of the
This
compartment
Important: In C++ (and most other languages),
the index starts from 0 and ends at n-1, where
n is the size of the array 5
FILLING AN ARRAY
Technology
Regent University College of Science &
Assign values to compartments:
prices[0] = 6.75;
prices[1] = 80.43;
prices[2] = 10.02;
6
ACCESSING ARRAY ELEMENTS
To access an item in an array, type the name
Technology
Regent University College of Science &
of the array followed by the items index in
square brackets.
For example, the expression:
grades[0]
will return the first element in the grades
array
7
ANOTHER WAY TO CREATE
ARRAYS
Youcan also specify all of the items in an
array at its creation.
Technology
Regent University College of Science &
Usecurly brackets to surround the arrays
data and separate the values with commas:
char grades[] = { A, B,
C, D, E'};
Note that all the items must be of the same
type. Here they are of type char.
Another example: 8
int powers[] = {0, 1, 10, 100};
MODIFYING ARRAY ELEMENTS
Example:
Technology
Regent University College of Science &
grades[0] = F;
Nowthe first grade in grades[] has been
changed from A to F.
Sothe expression grades[0] now evaluates to
F.
Note:The values of compartments can change,
but no new compartments may be added. 9
EXAMPLE
int fibs[10];
Technology
Regent University College of Science &
fibs[0] = 1;
fibs[1] = 1;
for(int i = 2; i < 10; i++) {
fibs[i] = fibs[i-2] + fibs[i-1];
}
Note: array indexes can be expressions
Afterrunning this code, the array fibs[]
contains the first ten Fibonacci numbers:
1 1 2 3 5 8 13 21 34 55 10
EXERCISE 1
Given this code fragment,
Technology
Regent University College of Science &
int data[10];
cout<<data[j]<<endl;
Which of the following is a legal value of
j?
a. -1 // out of range
b. 0 // legal value
c. 3.5 // out of range 11
d. 10 // out of range
EXERCISE 2
Which set of data would not be
suitable for storing in an array?
Technology
Regent University College of Science &
a. the score for each of the four quarters of a
Football match
b. your name, date of birth, and score on your
physics test // these are different types
c. temperature readings taken every hour
throughout a day
d. your expenses each month for an entire year
12
EXERCISE 3
What is the value of c after the following
code segment?
Technology
Regent University College of Science &
int a[] = {1, 2, 3, 4, 5};
int b[] = {11, 12, 13};
int c[4];
int j;
for (j = 0; j < 3; j++) {
c[j] = a[j] + b[j];
}
13
c = [12, 14, 16, 0]
2-DIMENSIONAL ARRAYS
The arrays we've used so far can be
0 1
Technology
Regent University College of Science &
thought of as a single row of values.
0 8 4
A 2-dimensional array can be thought
of as a grid (or matrix) of values 1 9 7
Each element of the 2-D array is
2 3 6
accessed by providing two indexes: value at row index 2,
a row index and a column index column index 0 is 3
(A 2-D array is actually just an array of arrays)
14
2-D ARRAY EXAMPLE
Example:
Technology
Regent University College of Science &
A landscape grid of a 20 x 55 acre piece of land:
We want to store the height of the land at each
row and each column of the grid.
We declare a 2D array using two sets of square brackets:
double heights[20][55];
This 2D array has 20 rows and 55 columns
To access the acre at row index 11 and column index 23
use: heights[11][23] 15
EXERCISE 1
The table below shows rainfall figures (in mm) for
the period Jan Sep in a given year
Technology
Regent University College of Science &
J F M A M J J A S Sum Avg
1.2 2.5 7.5 7.2 20.5 25.7 21.5 19.5 12.0
Write a program to calculate the average rainfall
figure.
The program should display the sum and average
16
EXERCISE 2
Write a program to calculate the average of a list of
numbers using an array.
Technology
Regent University College of Science &
Declare the array
Declare the sum and average
Prompt the user to fill the array
Find the sum
Find the average
Print the results
17