Professional Documents
Culture Documents
ITES103/ITEI103:Intro to Programming
SY 2013-2014(1st Term)
1.
2.
3.
4.
5.
Array Subscripts
2/6
#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.
#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;
}
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.
3/6
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]
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
4/6
In a 2-D array, we consider the first index to be row and second index to be column. For example,
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
: 98
: 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
5/6
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
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
6/6
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
:2
:3