Download - Chapter 8 - Two Dimensional Arrays
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 1/22
Copyright © 2012 Pearson Education, Inc.
Chapter 8
Two Dimensiona !rrays
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 2/22
2Copyright © 2012 Pearson Education, Inc.
"utine
"#$ecti%es1.Two Dimensiona !rrays
2.&atrices
De%eop pro#em soutions in C''containing(
) &atri* computations
) Input +rom Data ies) unctions to Compute -ums and !%erages
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 3/22
Two Dimensiona !rrays
) Decaration and Initiai/ation
) Computation and "utput) unction !rguments
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 4/22
Two Dimensiona !rrays) ! two dimensiona array stores data as a
ogica coection o+ rows and columns.
) Each eement o+ a twodimensiona array has a row
position and a coumn position.
) To access an eement in a twodimensiona array,
you must speci+y the name o+ the array +oowed #y(
a row offset
a column offset
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 5/22
3
Decaration and Initiai/ation) The decaration o+ a twodimensiona array re4uires a row
size and a column size.
) ! consecuti%e #oc5 o+ 6row size76column size7 memory
ocations are aocated.
) ! array eements must #e o+ the same type.
) Eements accessed #y two o++sets a row o++set and a
coumn o++set.
) The name o+ the array hods the address o+ the +irst #yte o+
memory
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 6/22
9
E*ampe//Declaration
int data[2][3];
Eements can #e accessed in the same way as accessing the eements 1D array
Copyright © 2012 Pearson Education, Inc.
Memory Snapshot
:
:
::
:
:
data
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 7/22;
E*ampe
//Declaration
int data[2][3];
Copyright © 2012 Pearson Education, Inc.
? ? ?
? ? ?
row 0
row 1
col 0 col 1 col 2
row/column form:
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 8/228
2D !rray De+inition -ynta*
Copyright © 2012 Pearson Education, Inc.
Syntax:data<type identi+ier [ =row<si/e> ][coumn<si/e] == initiai/ation<ist >;??row<si/e and coumn<si/e must #e integer constants
E*ampesint data[2][5]; //allocates consecutive memory for 10 integer valuesdouble t[2][2] = {{30!50"!{21!#2""; //allocates and initiali$es
@aid Ae+erencescout %% data[1][3];cout %% t[1][0];
In%aid Ae+erencescout %% data[2][5]; //invalid offset
cout %% t[&1][&1]; //invalid offset
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 9/22B
Initiai/ation E*ampes
int tem'[(][3] = {50! #0! )0! (*! #5!)2! 51! )+! )0! 52! #*! )3";
int tem'[(][3] = {{50! #0! )0"! {(*! #5! )2"!
{51! )+! )0"! {52! #*! )3"";
int t2[#][(] = {{50! #0! )0"! {(*! #5! )2"!
{51! )+! )0"! {52! #*! )3"";
int tem'[][3] = {{50! #0! )0"! {(*! #5! )2"!
{51! )+! )0"! {52! #*! )3"";int tem'[][3] = {50! #0! )0! (*! #5! )2! 51!
)+! )0! 52! #*! )3";
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 10/2210
E*ampe( Input sing cin) ested for oops are o+ten used when
inputting and assigning %aues to a twodimensiona array. ested oops are generay use+u +or getting around the 2D
arrays
Copyright © 2012 Pearson Education, Inc.
//Declarationdouble table[,-.][-.];
for int i=0; i%,-.; i4 //every ro for int 6=0; 6%-.; 6 4//every col cin 77 table[i][6];
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 11/22
11
E*ampe( !ssignment
Copyright © 2012 Pearson Education, Inc.
//Declarationconst int ,-.34!-.24;double v[,-.][-.];
for int i=0; i%,-.; i4 //every ro for int 6=0; 6%-.; 6 4//every col v[i][6] = i6;
0 1
1 2
2
V
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 12/22
12
E*ampe( Computations) Compute the a%erage %aue o+ an array
with n rows and m coumns.
Copyright © 2012 Pearson Education, Inc.
double sum04! average;for int i=0; i%n; i4 //every ro for int 6=0; 6%m; 6 4//every col sum = array[i][6];
average = sum / n8m4;
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 13/22
1
E*ampe( Computations
) Compute the a%erage %aue o+ the nth row
o+ a 2D array with r rows and c coumns.
Copyright © 2012 Pearson Education, Inc.
double sum04! ro9verage;for int 6=0; 6%c; 6 4 //every col sum = array[n][6];
average = sum / c;
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 14/22
1
&odi+yF
) &odi+y the C'' statements on the
pre%ious side to compute the a%erage o+
the mth coumn.
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 15/22
13
"utputting 2D !rrays
) Two dimensiona arrays are o+ten printed in
a row #y row +ormat, using nested for
statements.
) Ghen printing the row %aues o+ an array,
#e sure to print(
whitespace #etween the %aues in a row. a newine character at the end o+ each row.
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 16/22
19
E*ampe( Printing
Copyright © 2012 Pearson Education, Inc.
for int i=0; i%n; i4 {//every ro for int 6=0; 6%m; 6 4//every col
cout %% array[i][6] %% : :; cout %% endl; //add end&of&line eacro"
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 17/22
1;
2D !rrays as unction Parameters
) 2D arrays are aways passed #y re+erence.
) The coumn dimension must #e speci+ied. The e+tmost
dimension 6row7 may #e empty =>.
) unction prototype e*ampe(
int row!%erage6int Arr[][COLSIZE] int whichAow7H) !rray decaration in main(
int ta!le ["O#SIZE][COLSIZE];
) unction in%ocation e*ampe(
a%g row!%erage6ta!le, 7H
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 18/22
18
Documentation -tye) Gedocumented +unctions aways
incude preconditions and postconditions in the+unctionJs comment header #oc5.
Preconditions descri#e the conditions assumed to #e
true at the time the +unction is caed.) I+ preconditions are not met, there is no guarantee that the
+unction wi wor5 correcty.
Postconditions descri#e the changes that wi #e
made to the arguments during the e*ecution o+ the
+unction.
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 19/22
1B
&atri*) ! matri* is a set o+ num#ers arranged
in a rectanguar grid with rows and coumns.
) ! s4uare matri* has the same num#er o+ rows
as coumns.
) Aow and Coumn o++sets in &atrices are 1
#ased.
) 2D !rrays are use+u +or representing matrices. Aemem#er that C'' uses 0#ased o++setsF
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 20/22
20
&atri* Computations and "perations) The determinant o+ a matri* is a scaar %aue used
in computing matri* in%erses and so%ing systems o+
simutaneous e4uations.
) The transpose o+ a matri* is a new matri* in which the rows o+
the origina matri* are the coumns o+ the transpose.
) &atrices o+ the same si/e may #e added or su#tracted eement
#yeement.
) &atri* mutipication 6& 7 is de+ined ony when the num#er o+
coumns o+ & is e4ua to the num#er o+ rows in . Aesut has the si/e rows6&7 * cos67
Copyright © 2012 Pearson Education, Inc.
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 21/22
21
E*ampe sing 2D !rrays
Kincude LiostreamMKincude Lcstdi#Musing namespace stdHint main67N ?? decare the matrices(
int mat1=>=>, mat2=>=>, add=>=>H??generate the matrices(+or 6int i0H iLH i''7 +or 6int $0H $LH $''7 N mat1=i>=$>rand67O100H ??assume a eements ess than 100 mat2=i>=$>rand67O30H ??assume a eements ess than 30 ??add the matrices(+or 6int i0H iLH i''7 N +or 6int $0H $LH $''7N res=i>=$>mat1=i>=$>'mat1=i>=$>H
cout LL res=i>=$> LLQ QH cout LL endH
return 0H
Write C++ program to randomly generate two 4x4 matrices and addthem into a new matrix?
Possi#e output(
101 0 210 110 20 111 3;
1 2 110
80 3 220 8;
7/24/2019 Chapter 8 - Two Dimensional Arrays
http://slidepdf.com/reader/full/chapter-8-two-dimensional-arrays 22/22
22
E*ercise sing 2D !rrays
Write C++ program to generate two 4x4 matrices
randomly and multiply them?
REMEMBER: The result of multiplication will be a 4x4
matrix as well.