1 of 17

Programming C++ : Arrays

Presented By:

Aakif Mairaj, PhD (University of Toledo)

Lecturer at Electrical and Computer Engineering School of Engineering,

Purdue University Northwest

2 of 17

Real-World Examples

Library

Plantation

Pharmacy

Classroom

3 of 17

Arrays

  • Array: a collection of a fixed number of components wherein all of the components have the same data type
  • One-dimensional array: components are arranged in a list form
  • Syntax for declaring a one-dimensional array:

intExp evaluates to a positive integer

3

4 of 17

Accessing Array Components

  • General syntax:

where indexExp, called an index, whose value is a non-negative integer

  • Index value specifies the position of the component in the array
  • [] is the array subscripting operator
  • The array index always starts at 0

4

5 of 17

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];

6 of 17

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

7 of 17

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

8 of 17

Processing One-Dimensional Arrays

  • Consider the declaration

int list[100]; //array of size 100

int i;

  • Using for loops to access array elements:

for (i = 0; i < 100; i++) //Line 1

//process list[i] //Line 2

  • Example:

for (i = 0; i < 100; i++) //Line 1

cin >> list[i]; //Line 2

8

9 of 17

Some Restrictions on Array Processing

  • Consider the following statements:

  • C++ does not allow aggregate operations on an array:

  • Solution:

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];

10 of 17

Some Restrictions on Array Processing

  • The following is illegal too:

  • Solution:

10

for (int index = 0; index < 5; index++) cin >> yourList[index];

cin >> yourList; //illegal

11 of 17

Two-Dimensional Arrays

  • Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two dimensions
    • Sometimes called matrices or tables
  • Declaration syntax:

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

12 of 17

Two-Dimensional Arrays

12

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

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

[9]

double sales[10][5];

sales

13 of 17

Accessing Array Components

  • Syntax:

where indexexp1 and indexexp2 are expressions yielding nonnegative integer values, and specify the row and column position

13

14 of 17

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

15 of 17

Two-Dimensional Array Initialization During Declaration

  • Two-dimensional arrays can be initialized when they are declared:

    • Elements of each row are enclosed within braces and separated by commas
    • All rows are enclosed within braces
    • For number arrays, if all components of a row aren’t specified, unspecified ones are set to 0

15

int board[4][3]= {{2,3,1},

{14,25,13},

{20,4,7},

{11,18,14}};

16 of 17

17 of 17

Thank you

Any questions?