Programming C++ : Arrays
Presented By:
Aakif Mairaj, PhD (University of Toledo)
Lecturer at Electrical and Computer Engineering School of Engineering,
Purdue University Northwest
Real-World Examples
Library
Plantation
Pharmacy
Classroom
Arrays
intExp evaluates to a positive integer
3
Accessing Array Components
where indexExp, called an index, whose value is a non-negative integer
4
Accessing Array Components
5
|
|
|
|
|
|
|
|
|
|
List[0]
List[1]
List[2]
List[3]
List[4]
List[5]
List[6]
List[7]
List[8]
List[9]
int List[10];
Accessing Array Components
6
|
|
|
|
|
|
|
|
|
|
List[0]
List[1]
List[2]
List[3]
List[4]
List[5]
List[6]
List[7]
List[8]
List[9]
List[5] = 34;
34
Accessing Array Components
7
|
|
|
|
|
|
|
|
|
|
List[0]
List[1]
List[2]
List[3]
List[4]
List[5]
List[6]
List[7]
List[8]
List[9]
List[3] = 10;
List[6] = 35;
List[4] = List[3] + List[6];
10
35
45
Processing One-Dimensional Arrays
int list[100]; //array of size 100
int i;
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
for (i = 0; i < 100; i++) //Line 1
cin >> list[i]; //Line 2
8
Some Restrictions on Array Processing
9
int myList[5] = {0,4,8,12,16}; //Line 1
int yourList[5]; //Line 2
yourList = myList; //illegal
for (int index = 0; index < 5; index++)
yourList[index] = myList[index];
Some Restrictions on Array Processing
10
for (int index = 0; index < 5; index++) cin >> yourList[index];
cin >> yourList; //illegal
Two-Dimensional Arrays
where intexp1 and intexp2 are expressions yielding positive integer values, and specify the number of rows and the number of columns, respectively, in the array
11
Two-Dimensional Arrays
12
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
[0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
double sales[10][5];
sales
Accessing Array Components
where indexexp1 and indexexp2 are expressions yielding nonnegative integer values, and specify the row and column position
13
Accessing Array Components
14
sales[5][3] = 25.57;
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
[0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
sales
sales[5][3]
25.57
Two-Dimensional Array Initialization During Declaration
15
int board[4][3]= {{2,3,1},
{14,25,13},
{20,4,7},
{11,18,14}};
Thank you
Any questions?