Title: TwoDimensional Arrays
1Two-Dimensional Arrays
2Two-Dimensional Arrays
s
Col 0
Col 1
Col 2
Row 0
Row 1
Row 2
Row 3
3Declaration
- The number of rows is specified before the number
of columns - int s43
Number of columns
Number of rows
4How is it stored in memory?
s
This indicates row 1. Since there are 3 columns
row 1 begins at the fourth box (the first three
boxes contain elements in row 0)
This indicates an offset of 2 (column 2) from the
beginning of the row.
5Initialization
- The following declaration with initialization are
equivalent - int s43 50,70,60,48,75,62,51,59,60,52,78,63
- int s43 50,70,60,
- 48,75,62,
- 51,59,60,
- 52,78,63
- int s43 50,70,60,48,75,62,51,59,60,5
2,78,63 - int s3 50,70,60,48,75,62,51,59,60,52,78,63
- int s3 50,70,60,
- 48,75,62,
- 51,59,60,
- 52,78,63
- int s3 50,70,60,48,75,62,51,59,60,52
,78,63
You can leave the row dimension empty but the
column dimension must be specified otherwise, a
compile error will result.
6for Loops
- When accessing or performing operations on
multidimensional arrays, it is often necessary to
use nested for loops. Consider the problem of
adding arrays x and y, and placing the result in
z. - const int NROW 4
- const int NCOL 2
- int xNROWNCOL 3, 25,
- 9, 16,
- -4, 4,
- 44, 2
- int yNROWNCOL 8, 5,
- 12, -3,
- 43, 8,
- -7, 4
- int zNROWNCOL
7for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
?
?
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
?
2
2
2
iRow
44
2
-7
4
?
?
?
3
3
3
iColumn
8for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
For each row, all column values are computed and
filled before the next row is reached
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
?
?
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
0
2
2
2
iRow
44
2
-7
4
?
?
?
3
3
3
iColumn
9for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
?
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
0
2
2
2
iRow
44
2
-7
4
?
?
0
3
3
3
iColumn
10for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
?
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
0
2
2
2
iRow
44
2
-7
4
?
?
1
3
3
3
iColumn
11for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
30
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
0
2
2
2
iRow
44
2
-7
4
?
?
1
3
3
3
iColumn
12for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
30
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
0
2
2
2
iRow
44
2
-7
4
?
?
1
3
3
3
iColumn
13for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
30
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
1
2
2
2
iRow
44
2
-7
4
?
?
2
3
3
3
iColumn
14for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
30
0
9
16
12
-3
?
?
1
1
1
-4
4
43
8
?
?
1
2
2
2
iRow
44
2
-7
4
?
?
0
3
3
3
iColumn
15for Loops
- int iRow
- int iColumn
- for (iRow 0 iRow lt NROW iRow)
- for (iColumn 0 iColumn lt NCOL iColumn)
- ziRowiColumn xiRowiColumn
yiRowiColumn -
-
0
1
0
1
0
1
x
y
z
3
25
0
8
5
0
11
30
0
9
16
12
-3
21
?
1
1
1
-4
4
43
8
?
?
1
2
2
2
iRow
44
2
-7
4
?
?
0
3
3
3
iColumn
16Display
- include ltiostreamgt
- include ltiomanipgt
- using namespace std
- const int NROW 4
- const int NCOL 3
- int main()
-
- int sNROWNCOL 50,70,60,
- 48,75,62,
- 51,59,60,
- 52,78,63
- int i, j
-
- for (i 0 i lt NROW i)
- for (j 0 j lt NCOL j)
- cout ltlt setw(5) ltlt sij
17Row average
- Write a program that loads data (type double)
from a file input2d.txt into a 58 matrix. The
program should compute the average of each row.
The program should then display the matrix, with
the average at the end of each row.
18Column average
- Write a program that loads data (type double)
from a file input2d.txt into a 58 matrix. The
program should compute the average of each
column. The program should then display the
matrix, with the average at the end of each
column.
19Multidimensional Arrays and Functions
- Recall, a function using a 1-D array uses pass-by
reference as follows - a_function(int x)
- With multidimensional arrays, the array is still
passed using pass-by reference, but there is a
bit less flexibility. With the 1-D array we do
not have to explicitly state the size of the
array. With multidimensional arrays, we do not
have to explicitly state the size of the first
dimensional, but must do so for remaining
dimensions. - a_function(int xN)
- There is a method around this but we not be
covering this. In most cases (and in all cases in
this course), the size of the array will be known
beforehand and can always be explicitly stated.
20Terrain Navigation
- Imagine you have an array that quantifies the
land terrain (each element in the array specifies
the elevation at particular location in the
terrain).
21Terrain Navigation
- 5039 5127 5238 5259 5248 5310 5299
- 5150 5392 5410 5401 5320 5820 5321
- 5290 5560 5490 5421 5530 5831 5210
- 5110 5429 5430 5411 5459 5630 5319
- 4920 5129 4921 5821 4722 4921 5129
- 5023 5129 4822 4872 4794 4862 4245
22Terrain Navigation
- To quantify the difficulty in traversing the
terrain, we can compute the number of peaks in
the grid. - A peak is considered when the adjacent elevations
are lower than the current point. The points at
the edges of the array are never considered peaks
(no enough adjacent points). - In the previous slide the peaks are in bold and
are highlighted with the adjacent elevations.
23Terrain Navigation
- Write a function that given an array, and the
number of rows and columns (maximum size is
2525), it computes the number peaks. - Write a program that reads a data file
terrain.txt. The first two entries are the row
and column sizes, respectively. The remaining
entries are the elevations (type double). The
program displays the elevation data and
determines the number of peaks and displays this.