TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data...
-
date post
23-Jan-2016 -
Category
Documents
-
view
215 -
download
0
Transcript of TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data...
![Page 1: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/1.jpg)
TK1914: C++ Programming
Array II
![Page 2: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/2.jpg)
FTSM :: TK1914, 20112012 2
Objective
In this chapter you will explore how to manipulate data in a two-dimensional array.
![Page 3: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/3.jpg)
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
![Page 4: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/4.jpg)
FTSM :: TK1914, 20112012 4
mark
[0]
[1]
[2]
[3]
[0] [1] [2] [3] [4]
• Example:
int mark[4][5];
![Page 5: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/5.jpg)
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
![Page 6: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/6.jpg)
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
![Page 7: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/7.jpg)
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
![Page 8: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/8.jpg)
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
![Page 9: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/9.jpg)
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
![Page 10: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/10.jpg)
FTSM :: TK1914, 20112012 10
prog09.9.cpp
![Page 11: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/11.jpg)
FTSM :: TK1914, 20112012 11
![Page 12: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/12.jpg)
FTSM :: TK1914, 20112012 12
![Page 13: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/13.jpg)
FTSM :: TK1914, 20112012 13
![Page 14: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/14.jpg)
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
![Page 15: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/15.jpg)
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
![Page 16: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/16.jpg)
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}
![Page 17: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/17.jpg)
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);
}
![Page 18: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/18.jpg)
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;}
}
![Page 19: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/19.jpg)
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
![Page 20: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/20.jpg)
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
![Page 21: TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.](https://reader035.fdocuments.us/reader035/viewer/2022062807/56649d2f5503460f94a072d3/html5/thumbnails/21.jpg)
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