View
215
Download
0
Tags:
Embed Size (px)
Citation preview
TK1914: C++ Programming
Array II
FTSM :: TK1914, 20112012 2
Objective
In this chapter you will explore how to manipulate data in a two-dimensional array.
FTSM :: TK1914, 20112012 3
Two-Dimensional Arrays
• A collection of a fixed number of components arranged in two dimensions– All components are of the same type
• Declaration syntax:
dataType arrayName[intexp1][intexp2];
where intexp1 and intexp2 are expressions yielding positive integer values, which specify number of rows and number of columns respectively
• Sometimes called matrices or tables
FTSM :: TK1914, 20112012 4
mark
[0]
[1]
[2]
[3]
[0] [1] [2] [3] [4]
• Example:
int mark[4][5];
FTSM :: TK1914, 20112012 5
Initialization
• Like one-dimensional arrays– Two-dimensional arrays can be initialized when they
are declared
• Example:– int mark [][] = { {87, 86, 88, 82, 89},
{64, 69, 60, 63, 66},{91, 90, 94, 98, 93},{77, 74, 75, 72, 70} };
mark
[0][1][2][3]
[0] [1] [2] [3] [4]88 89828687
94 9398909175 70727477
60 66636964
inner braces can be omitted
FTSM :: TK1914, 20112012
Accessing Array Components
• The syntax to access a component of a two-dimensional array is:
arrayName[indexexp1][indexexp2]where indexexp1 and indexexp2 are expressions yielding nonnegative integer values
• indexexp1 specifies the row position and indexexp2 specifies the column position
6
FTSM :: TK1914, 20112012 7
Accessing Array Components
• Example:temp = mark[2][4]
• Example: find sum of elements in second row.row = 1;sumRow = 0;for (int col = 0; col < 5; col++)
sumRow = sumRow + mark[row][col];
mark
[0][1][2][3]
[0] [1] [2] [3] [4]
88 89828687
94 9398909175 70727477
60 66636964
FTSM :: TK1914, 20112012
Processing Two-Dimensional Arrays
• A two-dimensional array can be processed in three different ways:
1. Process the entire array
2. Process a particular row of the array, called row processing
3. Process a particular column of the array, called column processing
8
FTSM :: TK1914, 20112012
Processing Two-Dimensional Arrays (continued)
• Each row and each column of a two-dimensional array is a one-dimensional array
• When processing a particular row or column of a two-dimensional array
– we use algorithms similar to processing one-dimensional arrays
9
FTSM :: TK1914, 20112012 10
prog09.9.cpp
FTSM :: TK1914, 20112012 11
FTSM :: TK1914, 20112012 12
FTSM :: TK1914, 20112012 13
FTSM :: TK1914, 20112012 14
Passing Two-Dimensional Arrays as Parameters to Functions
• Two-dimensional arrays can be passed as parameters to a function
• By default, arrays are passed by reference• The base address, which is the address of the first
component of the actual parameter, is passed to the formal parameter
• When declaring a two-dimensional array as a formal parameter– can omit size of first dimension, but not the second
• Number of columns must be specified
FTSM :: TK1914, 20112012 15
#include <iostream> #include <iomanip>
using namespace std;
const int NUMBER_OF_ROWS = 6;const int NUMBER_OF_COLUMNS = 5;
void printMatrix(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);void sumRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);void largestInRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);
Example 9-10 (pg 528)
prog09.10.cpp
FTSM :: TK1914, 20112012 16
int main() { int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] = {{23, 5, 6, 15, 18}, {4, 16, 24, 67, 10}, {12, 54, 23, 76, 11}, {1, 12, 34, 22, 8}, {81, 54, 32, 67, 33}, {12, 34, 76, 78, 9}}; //Line 1
printMatrix(board, NUMBER_OF_ROWS); //Line 2 cout << endl; //Line 3 sumRows(board, NUMBER_OF_ROWS); //Line 4 cout << endl; //Line 5 largestInRows(board, NUMBER_OF_ROWS); //Line 6}
FTSM :: TK1914, 20112012 17
Example: Matrix manipulation#include <iostream>using namespace std;const int N = 3;
void inputMatrix(int mat[][N], int n);void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N],
int n);void outputMatrix(int mat[][N], int n);
void main() {int matrix1[N][N], matrix2[N][N], output[N][N];cout << "Input matrix 1: " << endl;inputMatrix(matrix1, N);cout << "Input matrix 2: " << endl;inputMatrix(matrix2, N);addMatrix(matrix1, matrix2, output, N);cout << "Answer for matrix1 add matrix2: " << endl;outputMatrix(output, N);
}
FTSM :: TK1914, 20112012 18
void inputMatrix(int mat[][N], int size) {int i, j;for (i = 0; i < size; i++)
for (j = 0; j < size; j++) cin >> mat[i][j];
}
void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int size) {int i, j;for (i = 0; i < size; i++)
for (j = 0; j < size; j++) mat3[i][j] = mat1[i][j] + mat2[i]
[j];}
void outputMatrix(int mat[][N], int size) {int i, j;for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) cout << mat[i][j];
cout << endl;}
}
FTSM :: TK1914, 20112012 19
Exercise
• Write matrix manipulation functions as listed below:– substract two matrices– multiply two matrices– to calculate transpose of a matrix
• Write function calls in the main program to test your matrix manipulation functions
FTSM :: TK1914, 20112012
Summary
• In a two-dimensional array, the elements are arranged in a table form
• To access an element of a two-dimensional array, you need a pair of indices: one for the row position and one for the column position
• In row processing, a two-dimensional array is processed one row at a time
• In column processing, a two-dimensional array is processed one column at a time
20
FTSM :: TK1914, 20112012
Summary (continued)
• In row processing, a two-dimensional array is processed one row at a time
• In column processing, a two-dimensional array is processed one column at a time
21