CSC113 Programming Fundamentals Lecture 5 1 Lecture Outline

  • Slides: 22
Download presentation
CSC-113 Programming Fundamentals Lecture 5 1

CSC-113 Programming Fundamentals Lecture 5 1

Lecture Outline • Arrays • Two Dimension Array • Three Dimension Array • Examples

Lecture Outline • Arrays • Two Dimension Array • Three Dimension Array • Examples • Multi Dimensional Array 2

Two-dimensional array • Many applications require data be stored in more then one dimension.

Two-dimensional array • Many applications require data be stored in more then one dimension. • A two-dimensional array consists of both rows and columns of elements. It is essentially a matrix. • To declare a two-dimensional array, we merely use two sets of square brackets. • The first contains the number of rows • The second contains the number of columns //Creates a 2 D array with 3 rows and 4 columns int vals[3][4];

Two-dimensional array

Two-dimensional array

Indices in 2 D arrays • Assume that the two dimensional array called val

Indices in 2 D arrays • Assume that the two dimensional array called val is declared and looks like the following: Val C 1 C 2 C 3 C 4 R 1 8 16 9 52 R 2 3 5 8 9 R 3 21 17 13 12 • To access the cell containing 6, we reference val[1][3], that is, row 1, column 3.

Using 2 D arrays • Just like 1 D arrays, once you have specified

Using 2 D arrays • Just like 1 D arrays, once you have specified the index, you are just working with a single variable of the given data type. • Assignments and usage is still the same: sum. Row 0 = val[0][0] + val[0][1] + val[0][2] + val[0][3]; //assigns 72 to cell at row 2, column 3 val[2][3] = 72;

Initializing 2 D arrays • You can use additional braces to indicate when rows

Initializing 2 D arrays • You can use additional braces to indicate when rows start and end, but you don’t have to do that. int val[3][4] = { {8, 16, 9, 52}, {3, 15, 27, 6}, {14, 25, 2, 10} }; • Or int val[3][4] = {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10}; • Or (correct, but not as clear as the first two): int val[3][4] = {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10};

Dry Run row Column table[row][column] 0 0 Table[0][0] = 0 0 1 Table[0][1] =

Dry Run row Column table[row][column] 0 0 Table[0][0] = 0 0 1 Table[0][1] = 1 0 2 Table[0][2] = 1 0 3 Table[0][3] = 1 0 4 Table[0][4] = 1 0 5 Table[0][5] = 1 1 0 Table[1][0] = -1 1 1 Table[1][1] = 0 1 2 Table[1][2] = 1 1 3 Table[1][3] = 1 1 4 Table[1][4] = 1 1 5 Table[1][5] = 1 … … ……………. . 5 5 Table[5][5] = 0

EXAMPLE PROGRAM. . . int main() { const int NUM_ROW(3); const int NUM_COL(4); int

EXAMPLE PROGRAM. . . int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }. . .

. . . int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23,

. . . int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }. . . > array 2 DExample. exe 11 12 13 14 21 22 23 24 31 32 33 34

EXAMPLE(2). . . int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL]

EXAMPLE(2). . . int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the transpose of the array for (int col = 0; col < NUM_COL; col++) { for (int row = 0; row < NUM_ROW; row++) { cout << vals[row][col] << " "; } cout << endl; }. . .

Matrix Addition Algorithm • Add two n x m matrices A, B; • for

Matrix Addition Algorithm • Add two n x m matrices A, B; • for each row i of A do • for each column j of A do ° C[i][j] = A[i][j] + B[i][j]; • Output matrices A, B and C.

. . . int main() { const int NUM_ROWS(3); const int NUM_COLS(2); Matrix. cpp

. . . int main() { const int NUM_ROWS(3); const int NUM_COLS(2); Matrix. cpp // number of matrix rows // number of matrix columns // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; // C = A + B for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { C[i][j] = A[i][j] + B[i][j]; } }. . .

matrix. cpp . . . // display A cout << "A =" << endl;

matrix. cpp . . . // display A cout << "A =" << endl; for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { cout << " } cout << endl; . . . " << A[i][j] << " ";

Array of arrays

Array of arrays

Memory layout

Memory layout

A three-dimensional array (3 x 5 x 4)

A three-dimensional array (3 x 5 x 4)

C++ view of three-dimensional array

C++ view of three-dimensional array

Multi-Dimensional Arrays • Arrays can have higher dimensions. • Definitions, intialization, indexing, function parameters

Multi-Dimensional Arrays • Arrays can have higher dimensions. • Definitions, intialization, indexing, function parameters are similar to 2 D arrays. • Note multidimensional arrays can have quite a few elements so you must be mindful of your memory capacity: int big[1000][1000]; //a billion ints

Initializing a three-dimensional array

Initializing a three-dimensional array

COMMON PROGRAMMING ERRORS • Addressing indices that are out of bounds of the array

COMMON PROGRAMMING ERRORS • Addressing indices that are out of bounds of the array range. This will run-time crash or at the very least a logical error. Be careful especially when using expressions to compute the indices. • Remember, indexing starts with 0, not 1!!! • Forgetting to declare the array (either altogether or forgetting the []) • Assuming the array is initialized (to zero. )

22

22