You are on page 1of 5

COMP 1602: Computer Programming II

Review of Arrays
1) What is an array?
An array can be used in a program to store and manipulate a group of related data as if it
were one unit. The data must be of the same type. All the elements of the group are referred
to by the same name and operations are applied to group as a whole. Thus, the array
simplifies the process of storing and manipulating a large amount of similar data in a program.

2) Why do we need an array?


Consider a situation where we need to store five integer numbers. If we use a simple variable
of the int data type for each value, we can store the five integer values in five simple variables.
Now, let’s assume that we have to store 5000 integer values. Are we going to use 5000 simple
variables?
To handle such situations, almost all the programming languages provide a concept
called array.

3) Declaring arrays
An array makes it possible to store and manipulate a collection of related data as if it were one unit.
For example, the marks of the students enrolled in a course are all integers and thus the marks can
be stored in an array.
The Figure below shows an array which contains ten memory locations for the marks obtained by 10
students enrolled in a course. The memory locations are numbered from 0 to 9. The first location of
an array is always numbered 0.

Figure: Array with 10 Memory Locations to Store Marks

An array must be declared before it can be used. To declare an array, marks, which will contain 10
memory locations for storing integer values, the following declaration must be made:
int marks [10];

1
COMP 1602: Computer Programming II
Review of Arrays
The declaration is similar to the declaration of a single integer variable, except that the square
brackets after the variable name indicate that an array is being declared. The actual size of the array,
10, is specified within the square brackets.

Arrays of different types can be declared in a similar manner. To declare an array of 20 characters, the
following declaration is made:

char name [20];

4) Adding data to arrays: assignment operator, set notation, input from keyboard, reading from
a file
To assign a value to a particular array element, it is first necessary to identify the array element. An
array element is identified by giving the name of the array, followed by the location of the element,
within square brackets. For example, marks[0] is the first element of the marks array and marks[9] is
the last element.

Once identified, an array element can be assigned a value in three ways: using the assignment
operator, inputting a value from the keyboard, and inputting a value from a file.

a) Assignment Operator
The assignment operator, =, can be used to assign a value to an array element. For example, to
assign the value 85 to the element at location 0, the following statement should be used:
marks [0] = 85;

A for loop can be used to generate all the locations from 0 to 9 in increments of 1:

for (i=0; i<10; i=i+1) {


.
.
.
}

Suppose we wanted to assign a value of 0 to all locations in the array. Since the for loop generates
all the locations from 0 to 9 via the loop control variable, i, all that has to be done to refer to the
array elements in the loop is to use the variable i within the square brackets:

for (i=0; i<10; i=i+1) {


marks [i] = 0;
}

2
COMP 1602: Computer Programming II
Review of Arrays
It is also possible to assign a value to an array element based on its previous value. For example,
the following statement adds 1 to the value currently stored in array element 2:

marks [2] = marks [2] + 1;

b) Assigning a value using set notation


List the array values in set notation { } after the declaration.
Here are some examples:
• int list[4] = {2, 4, 6, 8};
list contains 2, 4, 6 and 8
• char letters[5] = {'a', 'e', 'i', 'o', 'u'};
letters contains five characters. ‘a’ is the first character at location 0 and
‘u’ is the last character at location 4.
• double numbers[3] = {3.45, 2.39, 9.1};
numbers contains 3 double values.

c) Assigning a Value using Keyboard Input


We can assign a single value from the keyboard as follows:
cout << "Please enter mark for student " << i << " ";
cin >> marks[i];

All 10 values can be entered from the keyboard using a for loop as follows:
for (i=0; i<10; i=i+1) {
cout << "Please enter mark for student " << i << " ";
cin >> marks[i];
}

d) Assigning a Value using File Input


ifstream inputFile;

inputFile.open("marks.txt");

for (i=0; i<10; i=i+1) {


inputFile >> marks[i];
}

inputFile.close();

5) Displaying the Values Stored in an Array


for (i=0; i<10; i=i+1) {
cout << marks[i] << endl;
}

3
COMP 1602: Computer Programming II
Review of Arrays
6) Arrays that are Completely Filled
a) Highest, Lowest, Sum and Average of elements, location of highest, location of smallest
int i;
int max, maxLocation, min, minLocation, sum;
double average;

max = 0;
min = 101;
sum = 0;

for (i=0; i<10; i=i+1) {


if (marks[i] > max) {
max = marks [i];
maxLocation = i;
}
if (marks[i] < min) {
min = marks [i];
minLocation = i;
}
sum = sum + marks[i];
}

cout << "The highest value is: " << max << endl;
cout << "It is stored in location " << maxLocation << endl;

cout << "The lowest value is: " << min << endl;
cout << "It is stored in location " << minLocation << endl;

average = sum / 10.0;

cout << "The sum of the marks is: " << sum << endl;
cout << "The average mark is: " << average << endl;

b) Copying array elements to another array in same order and reverse order
The elements of an array can be easily copied to another array using a for loop. For example,
suppose that second is another integer array declared as follows:

int second [10];

The values of the marks array can be copied to this new array as follows:

for (i=0; i<10; i=i+1) {


second[i] = marks[i];
}

The values can be copied in reverse order by changing the subscripts as follows:

for (i=0; i<10; i=i+1) {


second[9-i] = marks[i];
}

Note that when i is 0, 9 – i is 9, and when i is 1, 9 – i is 8, and so on. Thus, the value in location 0
in the marks array becomes the value in location 9 in the second array. Similarly, the value in

4
COMP 1602: Computer Programming II
Review of Arrays
location 1 in the marks array becomes the value in location 8 in the second array. This happens
until all the elements are copied from the marks array to second; at this point in time, the
elements in second are the same as the elements in the marks array, except that they are in the
reverse order.

7) Arrays that are not Completely Filled


a) Initializing the array and then adding data especially if the array is not full.
• Use set notation:
int marks [10] ={0,0,0,0,0,0,0,0,0,0};

• Use a for loop:


for (i=0; i<10; i=i+1) {
marks[i] = 0;
}
b) Number of elements in the array
int i = 0;
int count = 0;
while (marks[i] !=0) {
count = count +1;
i = i + 1;
}

8) Begin concept of using index to represent not just a location but a value.
A pack of cards contains 52 cards. There are 4 Aces, 4 Kings, 4 Queens, 4 Jacks, 4 Tens, 4
Nines etc. An ace is assigned the number 1, King the number 13, Queen the number 12, jack
the number 11 and each of the number cards has their corresponding value.
A user enters an unknown amount of numbers. Each number represents a card in the pack.
Each number is one of the numbers 1 to 13 only. A number can appear 0 or more times and
can appear anywhere in the file. Each number is separated by one or more spaces. A value of
-1 ends the data. Some sample data are:
5 3 3 3 3 4 3 3 12 6 2 2 3 4 3 3 2 2 10 2 2 2 2 2 8 5 5 3 3 9 7 13 -1
Write a program to read the data once and print a table showing the card and the number of
times the card is chosen.

You might also like