TK1914: C++ Programming

Download Report

Transcript TK1914: C++ Programming

TK1914: C++ Programming
Array II
Objective
In this chapter you will explore how to manipulate
data in a two-dimensional array.
FTSM :: TK1914, 20112012
2
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
3
• Example:
int mark[4][5];
[0]
[1]
[2]
[3]
[4]
[0]
[1]
[2]
[3]
mark
FTSM :: TK1914, 20112012
4
Initialization
• Like one-dimensional arrays
–
Two-dimensional arrays can be initialized when they
are declared
inner braces can
be omitted
• Example:
– int mark [][] = { {87,
{64,
{91,
{77,
[0]
[1]
[2]
[3]
[0]
87
64
91
77
[1]
86
69
90
74
[2]
88
60
94
75
mark
[3]
82
63
98
72
86,
69,
90,
74,
88,
60,
94,
75,
82,
63,
98,
72,
89},
66},
93},
70} };
[4]
89
66
93
70
FTSM :: TK1914, 20112012
5
Accessing Array Components
• The syntax to access a component of a twodimensional 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
FTSM :: TK1914, 20112012
6
Accessing Array Components
• Example:
temp = mark[2][4]
[0]
[1]
[2]
[3]
[0]
87
64
91
77
[1]
86
69
90
74
[2]
88
60
94
75
[3]
82
63
98
72
[4]
89
66
93
70
mark
• Example: find sum of elements in second row.
row = 1;
sumRow = 0;
for (int col = 0; col < 5; col++)
sumRow = sumRow + mark[row][col];
FTSM :: TK1914, 20112012
7
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
FTSM :: TK1914, 20112012
8
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 onedimensional arrays
FTSM :: TK1914, 20112012
9
prog09.9.cpp
FTSM :: TK1914, 20112012
10
FTSM :: TK1914, 20112012
11
FTSM :: TK1914, 20112012
12
FTSM :: TK1914, 20112012
13
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
14
Example 9-10 (pg 528)
#include <iostream>
#include <iomanip>
prog09.10.cpp
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);
FTSM :: TK1914, 20112012
15
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);
cout << endl;
sumRows(board, NUMBER_OF_ROWS);
cout << endl;
largestInRows(board, NUMBER_OF_ROWS);
//Line
//Line
//Line
//Line
//Line
2
3
4
5
6
}
FTSM :: TK1914, 20112012
16
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
17
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
18
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
19
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
FTSM :: TK1914, 20112012
20
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
FTSM :: TK1914, 20112012
21