You are on page 1of 6

LAB

FEU-EAST ASIA COLLEGE


Information Technology Education Department

ITES103/ITEI103:Intro to Programming
SY 2013-2014(1st Term)

Lab 7: C++ Arrays


Objectives
This lab activity aims student to:

1.
2.
3.
4.
5.

Familiarized with array implementation in a C++ program.


Familiarized with the different applications of arrays in C++ programs .
Familiarized with the different types of arrays in C++
To code and execute C++ programs.
To debug a C++ program.

Task 1: Theoretical Framework

An array is a consecutive group of memory locations.


Each group is called an element of the array.
The contents of each element are of the same type.
For example, could be an array of int, double, char,
We can refer to individual elements by giving the position number (index) of the element in
the array.

C++ Arrays start at 0 !!!!!!!

The first element is the 0th element!


If you declare an array of n elements, the last one is number n-1.
If you try to access an element more than number n then it is an error!

Array Subscripts

The element numbers are called subscripts.


For example: In foo[i], foo is the name of the array, and i is the subscript.
A subscript can be any integer expression:
These are all valid subscripts:

foo[17] foo[i+3] foo[a+b+c]

FEU-EAC, ITE Department


ITES103/ITEI103: Intro to Programming

2/6

SY 2013-2014 (1st Term)


Lab 6: C++ While Loop Statements

#include <iostream>
using namespace std;
int main()
{
int facs[10]; //declare an int array of size 10
for (int i=0;i<10;i++)
facs[i] = i;
//initialize
for (int i=0;i<10;i++)
cout << facs[ << i << ] is << facs[i] << endl;
}

Initialization

You can initialize an array when you declare it (just like with variables):
int foo[5] = { 1,8,3,6,12};

//size of array is 5.

double d[2] = { 0.707, 0.707};


char s[] = { 'J', 'S', 'C' };

//size of array is automatically determined


//from the size of elements

#include <iostream>
using namespace std;
int main()
{
int
ar1[100];
// 100 elements, 0 to 99
float ar2[4] = { 1.01, 2.02, 3.03, 4.04 };
int
i;
cout << "size of ar2 " << sizeof(ar2)<< endl;
for (i = 0; i <= 99; ar1[i] = i, i++);
for (i = 0; i < 4; cout << ar2[i] << endl, i++);
return 0;
}

This example shows us quite a bit about arrays:

The array ar1 has 100 elements indexed from 0 to 99


Array ar2 has 4 elements and each element is initialized to contain some value, eg ar2[0]
contains 1.01.
The index for ar2 could have been omitted, we could have written
float ar2[] = { 1.01, 2.02, 3.03, 4.04 };.
The size of ar2 would be determined at compile time by the number of initialization values.
The sizeof() function is a standard function which returns the size, in bytes, of its argument.
The second for loop uses the comma operator:
ar2[i] << endl, i++

We can declare arrays of any data type and the size of an array is only limited by the available
memory. Remember that each element of an array is the same kind.

FEU-EAC, ITE Department


ITES103/ITEI103: Intro to Programming

3/6

SY 2013-2014 (1st Term)


Lab 6: C++ While Loop Statements

Two-Dimensional Arrays
A 2D array is an array that has both rows and columns. You must use 2 sets of square brackets when
declaring a 2D array and when using it.
int arr[3][3];
arr[0][0]
arr[0][1]
arr[0][2]
arr[1][0]
arr[1][1]
arr[1][2]
arr[2][0]
arr[2][1]
arr[2][2]

//declare a 3 x 3 array. There are 3 rows, each with three columns.


= 5;
= 2;
= 4;
= 3;
= 7;
= 9;
= 6;
= 1;
= 8;

Here is a table that shows you what a 2D array looks like.


arr
012
0524
1379
2618
You can initialize all the elements of an array to 0 using a loop instead of setting them each individually.

int arr[3];
for (int x=0; x<3; x++)
arr[x] = 0;
When you do this with a 2D array you need to use 2 loops.
int arr[3][3];
for (int x=0; x<3; x++)
for (int y=0; y<3; y++)
arr[x][y] = 3*x + y;
Notice that this generates a table as follows:

0 1 2
0 0 1 2
Rows

1 3 4 5
2 6 7 8

Columns
A 2-dimensional array usually stored in computer memory as 1-dimensional.
For example, the array above may be stored as row major

0
0

1
1

Row 0

2
2

3
3

4
4

Row 1

5
5

6
6

7
7

8
8

Row 2

Index
Value

FEU-EAC, ITE Department


ITES103/ITEI103: Intro to Programming

4/6

SY 2013-2014 (1st Term)


Lab 6: C++ While Loop Statements

In a 2-D array, we consider the first index to be row and second index to be column. For example,

int arr[3][4]; // 3 rows and 4 columns

0 [0][0] [0][1] [0][2] [0][3]


1 [1][0] [1][1] [1][2] [1][3]
2 [2][0] [2][1] [2][2] [2][3]

Activity 1: Write a program that reads a series of numbers (5-10) from the keyboard and print them in
reverse order. Store all numbers in an array named Number[]. User may enter 5 to 10 integers only.
Sample output:
How many would you like to enter? (5-10): 12
Invalid
How many would you like to enter? (5-10): 5
Enter your numbers:
99 5 34 19 44
Your numbers reversed:
44 19 34 5 99

Activity 2: Write a program which will generate an array of 9 integers with random integer values
between 20 and 99, print them out, print out the first, middle and the last elements. The output should
look like this:
Random numbers are: 45 23 20 47 98 76 75 22 99
The first element

: 45

The middle element

: 98

The last element

: 99

Activity 3: Write a program that reads five numbers from the keyboard and print the largest and
smallest numbers. Store all numbers in an array named FindMax[]. Sample output:
Enter 5 numbers:
99 5 34 19 44
The largest number is 99 at index 0
The smallest number is 5 at index 1

FEU-EAC, ITE Department


ITES103/ITEI103: Intro to Programming

5/6

SY 2013-2014 (1st Term)


Lab 6: C++ While Loop Statements

Activity 4: Write a program which declares three arrays called Array A, Array B and Array C. All of
the arrays contain 2 rows and 2 columns. Perform matrix addition and subtraction of Array A and
Array B. Store the result in Array C. Sample output:
Array A:
Enter a number for index[0][0]: 2
Enter a number for index[0][1]: 4
Enter a number for index[1][0]: 6
Enter a number for index[1][1]: 4
Array B:
Enter a number for index[0][0]: 4
Enter a number for index[0][1]: 5
Enter a number for index[1][0]: 8
Enter a number for index[1][1]: 7
Choose option (1-Addition, 2-Subtraction): 1
Array A
2

Array B
4

Array C
6

14

11

Press any key to continue

Activity 5: A bowling team consists of three players. Each player bowls three games. Write a program
that uses 2D array to store the bowling scores. Then compute and display the total score for each game.
Assume that each bowler has the following score:
Game 1

Game 2

Game 3

Bowler # 1:

286

252

265

Bowler # 2:

212

186

215

Bowler # 3:

252

232

216

Bowler # 4:

192

201

235

Bowler # 5:

186

236

272

Total

1128

1107

1203

FEU-EAC, ITE Department


ITES103/ITEI103: Intro to Programming

6/6

SY 2013-2014 (1st Term)


Lab 6: C++ While Loop Statements

Activity 5: Use arrays to hold the prices, models and CCs for Toyotas car as listed below. The program
will allow users to choose the model (Camry, Vios or Altis) and the CC of the car (2.0, 1.8, or 1.5) in
order to get the price of the car.
Example of output screen:
WELCOME TO TOYOTA
+++++++++++++++++
Model

CC

Price (Php)

--------

------------

-------------

Camry

2.0

171000

Camry

1.8

151000

Camry

1.6

140000

Vios

2.0

88000

Vios

1.8

84000

Vios

1.6

79000

Altis

2.0

125000

Altis

1.8

116000

Altis

1.6

100000

Choose MODEL (1:Camry 2:Vios 3:Altis)

:2

Choose CC (1:2.0 2:1.8 3:1.6)

:3

-----MODEL INFO----VIOS, 1.6 CC, Php 79, 000.00

You might also like