








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









1 //^ code^18 2 //^ Initializing
multidimensional
arrays. 3 #include^
int^ [][^3 ]^ ); 9 10 int^ main() 11 { 12 int^ array1[
13 int^ array2[
14 int^ array3[
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.
Quiz2 Quiz1 (^95 85) Student0 (^89 80) Student
1 //^ code^19 2 //^ Double-subscripted
array^ example. 3 #include^
=^ 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
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.
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
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
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
The^ array^ is:
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