TwoDimensional Arrays - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

TwoDimensional Arrays

Description:

int z[NROW][NCOL]; Updated 08/11/12. Adrian Chan. ECOR1606 Problems solving ... for (iRow = 0; iRow NROW; iRow ) { for (iColumn = 0; iColumn NCOL; iColumn ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 24
Provided by: adria9
Category:

less

Transcript and Presenter's Notes

Title: TwoDimensional Arrays


1
Two-Dimensional Arrays
2
Two-Dimensional Arrays
s
Col 0
Col 1
Col 2
Row 0
Row 1
Row 2
Row 3
3
Declaration
  • The number of rows is specified before the number
    of columns
  • int s43

Number of columns
Number of rows
4
How 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.
5
Initialization
  • 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.
6
for 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

7
for 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
8
for 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
9
for 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
10
for 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
11
for 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
12
for 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
13
for 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
14
for 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
15
for 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
16
Display
  • 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

17
Row 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.

18
Column 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.

19
Multidimensional 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.

20
Terrain 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).

21
Terrain 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

22
Terrain 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.

23
Terrain 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.
Write a Comment
User Comments (0)
About PowerShow.com