Docsity

Multiple-Subscripted Arrays-Introduction to Programming-Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Multiple-Subscripted Arrays-Introduction to Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

C++ Programming^ • Multiple-Subscripted Arrays

docsity.com

Multiple-Subscripted Arrays^ ^ Multiple subscripts^ ◦^ a[ i ][ j ]^ ◦^ Tables with rows and columns^ ◦^ Specify row, then column^ ◦^ “Array of arrays”^ ^ a[0]

is an array of 4 elements  a[0][0]^ is the first element of that array

ColumnRow 0 Row 1 Row 2

0 Column^

1 Column^2

Column^3

a[^0 ][^0 ]a[ a[^1 ][^0 ] a[^2 ][^0 ]

0 ][^1 ]a[^ a[ 1 ][^1 ] a[ 2 ][^1 ]

0 ][^2 ]a[^0 a[ 1 ][^2 ] a[ 2 ][^2 ]

][^3 ] a[ 1 ][^3 ] a[ 2 ][^3 ]

Array^ nameRow^ subscript

Column^ subscript

docsity.com

Multiple-Subscripted Arrays^ ^ Referenced like normal^ cout

<<^ b[^0

][^1 ];

◦^ Outputs

◦^ Cannot reference using commas^ cout^ <<

b[^ 0,^1

];

^ Syntax error  Function prototypes ◦ Must specify sizes of subscripts ^ First subscript not necessary, as with single-scriptedarrays ◦ void printArray( int [][ 3 ] );

docsity.com

1 //^ code^18 2 //^ Initializing

multidimensional

arrays. 3 #include^ 4 5 using^ std::cout; 6 using^ std::endl; 7 8 void^ printArray(

int^ [][^3 ]^ ); 9 10 int^ main() 11 { 12 int^ array1[

2 ][^3 ]^ =^ {^ {^

1,^ 2,^3 },^ {^ 4,^

5,^6 }^ };

13 int^ array2[

2 ][^3 ]^ =^ {^ 1,

2,^ 3,^ 4,^5 };

14 int^ array3[

2 ][^3 ]^ =^ {^ {^

1,^2 },^ {^4 }^ };

1516 cout^ <<^ "Values

in^ array1^ by^ row

are:"^ <<^ endl; 17 printArray(

array1^ ); 1819 cout^ <<^ "Values

in^ array2^ by^ row

are:"^ <<^ endl; 20 printArray(

array2^ ); 2122 cout^ <<^ "Values

in^ array3^ by^ row

are:"^ <<^ endl; 23 printArray(

array3^ ); 2425 return^ 0;^

//^ indicates^ successful

termination 2627 }^ //^ end^ main

Note the variousinitialization styles. Theelements in^ array

are Note the format of theprototype. assigned to the first rowand then the second.

docsity.com

Multiple-Subscripted Arrays^ ^ Next: program showing initialization^ ◦^ After, program to keep track of studentsgrades^ ◦^ Multiple-subscripted array (table)^ ◦^ Rows are students^ ◦^ Columns are grades

Quiz2 Quiz1 (^95 85) Student0 (^89 80) Student

docsity.com

1 //^ code^19 2 //^ Double-subscripted

array^ example. 3 #include^ 4 5 using^ std::cout; 6 using^ std::endl; 7 using^ std::fixed; 8 using^ std::left; 9 10 #include^ 1112 using^ std::setw; 13 using^ std::setprecision; 1415 const^ int^ students

=^ 3;^ //^ number

of^ students 16 const^ int^ exams

=^ 4;^ //^ number

of^ exams 1718 //^ function^ prototypes 19 int^ minimum(^

int^ [][^ exams^ ],^

int,^ int^ ); 20 int^ maximum(^

int^ [][^ exams^ ],^

int,^ int^ ); 21 double^ average(

int^ [],^ int^ ); 22 void^ printArray(

int^ [][^ exams^ ],

int,^ int^ ); 23

docsity.com

44 //^ calculate

average^ grade^ for

each^ student

45 for^ (^ int^

person^ =^ 0;^ person

<^ students;^ person++

46 cout^ <<

"The^ average^ grade

for^ student^ "^ <<

person

47 <<

"^ is^ " 48 <<

average(^ studentGrades[

person^ ],^ exams

endl; 5051 return^ 0;^

//^ indicates^ successful

termination 5253 }^ //^ end^ main 5455 //^ find^ minimum

grade 56 int^ minimum(^

int^ grades[][^ exams

],^ int^ students,

int^ tests^ )

57 { 58 int^ lowGrade

=^ 100;^ //^ initialize

to^ highest^ possible

grade

5960 for^ (^ int^

i^ =^ 0;^ i^ <^ students;

i++^ ) 6162 for^ (^ int

j^ =^ 0;^ j^ <^ tests;

j++^ ) 6364 if^ (

grades[^ i^ ][^ j^ ]^ <^ lowGrade^ ) 65 lowGrade

=^ grades[^ i^ ][^ j^ ]; 6667 return^ lowGrade; 6869 }^ //^ end^ function

minimum

Determines the average forone student. We pass thearray/row containing thestudent’s grades. Note that studentGrades[0]

is itself an array.

docsity.com

7071 //^ find^ maximum

grade 72 int^ maximum(^

int^ grades[][^ exams

],^ int^ students,

int^ tests^ )

73 { 74 int^ highGrade

=^ 0;^ //^ initialize

to^ lowest^ possible

grade

7576 for^ (^ int^

i^ =^ 0;^ i^ <^ students;

i++^ ) 7778 for^ (^ int

j^ =^ 0;^ j^ <^ tests;

j++^ ) 7980 if^ (

grades[^ i^ ][^ j^ ]^ >^ highGrade^ ) 81 highGrade

=^ grades[^ i^ ][^

j^ ]; 8283 return^ highGrade; 8485 }^ //^ end^ function

maximum 86

docsity.com

99100 //^ Print^ the^

array 101 void^ printArray(

int^ grades[][^ exams

],^ int^ pupils,^ int^ tests^ )

102 { 103 //^ set^ left

justification^ and

output^ column^ heads 104 cout^ <<^ left

<<^ "^

[0]^ [1]^ [2]^ [3]";

105106 //^ output^

grades^ in^ tabular

format 107 for^ (^ int^

i^ =^ 0;^ i^ <^ pupils;

i++^ )^ { 108109 //^ output

label^ for^ row 110 cout^ <<

"\nstudentGrades["

<<^ i^ <<^ "]^ "; 111112 //^ output

one^ grades^ for^ one^ student 113 for^ (^ int

j^ =^ 0;^ j^ <^ tests;

j++^ ) 114 cout

<<^ setw(^5 )^ << grades[^ i^ ][^ j^

];

115116 }^ //^ end^ outer

for 117118 }^ //^ end^ function

printArray

docsity.com

The^ array^ is:

[0]^ [1]^ [2]^ [3]

studentGrades[0]

studentGrades[1]

studentGrades[2]

Lowest^ grade:^68 Highest^ grade:^96 The^ average^ grade

for^ student^0 is

The^ average^ grade

for^ student^1 is

The^ average^ grade

for^ student^2 is

docsity.com