Professional Documents
Culture Documents
08 2D Arrays
08 2D Arrays
2D Array
2
2003 Prentice Hall, Inc. All rights reserved.
3
3
2003 Prentice Hall, Inc. All rights reserved.
4
Sales[5][3]
4
2003 Prentice Hall, Inc. All rights reserved.
5
Two-Dimensional Array Initialization During
Declaration
• Two-dimensional arrays can be initialized
when they are declared:
– Elements of each row are enclosed within braces
and separated by commas
– All rows are enclosed within braces
– For number arrays, unspecified elements are set to 0
5
2003 Prentice Hall, Inc. All rights reserved.
6
6
2003 Prentice Hall, Inc. All rights reserved.
7
7
2003 Prentice Hall, Inc. All rights reserved.
8
Initialization
• Examples:
– To initialize row number 4 (fifth row) to 0:
8
2003 Prentice Hall, Inc. All rights reserved.
9
9
2003 Prentice Hall, Inc. All rights reserved.
10
Input
• Examples:
– To input into row number 4 (fifth row):
10
2003 Prentice Hall, Inc. All rights reserved.
Sum by Row
• Example:
– To find the sum of row number 4:
11
2003 Prentice Hall, Inc. All rights reserved.
12
Sum by Column
• Example:
– To find the sum of each individual column:
12
2003 Prentice Hall, Inc. All rights reserved.
13
Largest Element in Each Row
and Each Column
• Example:
– To find the largest element in each row:
13
2003 Prentice Hall, Inc. All rights reserved.
14
Passing Two-Dimensional Arrays as Parameters
to Functions
• Two-dimensional arrays are passed by reference as
parameters to a function
– Base address is passed to formal parameter
• Two-dimensional arrays are stored in row order
• When declaring a two-dimensional array as a
formal parameter, can omit size of first dimension,
but not the second
14
2003 Prentice Hall, Inc. All rights reserved.
Multiple-Subscripted Arrays (Example)
Student0 95 85
Student1 89 80
15
2003 Prentice Hall, Inc. All rights reserved.
Outline
24 int main()
25 {
26 // initialize student grades for three students (rows)
27 int studentGrades[ students ][ exams ] =
28 { { 77, 68, 86, 73 },
fig04_23.cpp
29 { 96, 87, 89, 78 }, (2 of 6)
30 { 70, 90, 86, 81 } };
31
32 // output array studentGrades
33 cout << "The array is:\n";
34 printArray( studentGrades, students, exams );
35
36 // determine smallest and largest grade values
37 cout << "\n\nLowest grade: "
38 << minimum( studentGrades, students, exams )
39 << "\nHighest grade: "
40 << maximum( studentGrades, students, exams ) << '\n';
41
42 cout << fixed << setprecision( 2 );
2003 Prentice Hall, Inc. 17
43 All rights reserved.
Outline
70
71 // find maximum grade
72 int maximum( int grades[][ exams ], int pupils, int tests )
73 {
74 int highGrade = 0; // initialize to lowest possible grade
fig04_23.cpp
75 (4 of 6)
76 for ( int i = 0; i < pupils; i++ )
77
78 for ( int j = 0; j < tests; j++ )
79
80 if ( grades[ i ][ j ] > highGrade )
81 highGrade = grades[ i ][ j ];
82
83 return highGrade;
84
85 } // end function maximum 2003 Prentice Hall, Inc. 19
86 All rights reserved.
Outline
99
100 // Print the array
101 void printArray( int grades[][ exams ], int pupils, int tests )
102 {
103 // set left justification and output column heads
fig04_23.cpp
104 cout << left << " [0] [1] [2] [3]"; (6 of 6)
105
106 // output grades in tabular format
107 for ( int i = 0; i < pupils; i++ ) {
108
109 // output label for row
110 cout << "\nstudentGrades[" << i << "] ";
111
112 // output one grades for one student
113 for ( int j = 0; j < tests; j++ )
114 cout << setw( 5 ) << grades[ i ][ j ];
115
116 } // end outer for
117
118 } // end function printArray 2003 Prentice Hall, Inc. 21
All rights reserved.
Outline