Ngôn ngữ lập trình C++ - Chương 4: Mảng

4.1 Giới thiệu

4.2 Mảng

4.3 Khai báo mảng

4.4 Ví dụvềsửdụng mảng

4.5 Truyền tham sốcho hàm

4.6 Sắp xếp mảng

4.7 Ví dụ: Dùng mảng tính Mean, Median và Mode

4.8 Tìm kiếm trên mảng: Tìm kiếm Tuyến tính và tìm kiếm Nhịphân

4.9 Mảng nhiều chiều

pdf83 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1472 | Lượt tải: 3download
Tóm tắt nội dung Ngôn ngữ lập trình C++ - Chương 4: Mảng, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
greater than middle element, 
74 // set new low element
75 else
76 low = middle + 1; // search high end of array
77 }
78 
79 return -1; // searchKey not found
80 
81 } // end function binarySearch
Sử dụng tìm Nhị phân:
Nếu key bằng middle, tìm thấy
Nếu nhỏ hơn, tìm nửa thấp
Nếu lớn hơn, tìm nửa cao
Vòng lặp tạo low, middle và high tự động. Nếu tìm 
nửa cao, thì phần tử low mới sẽ cao hơn middle.
©2004 Trần Minh Châu.
FOTECH. VNU.
67
fig04_20.cpp
(5 of 6)
82 
83 // print header for output
84 void printHeader( int size )
85 {
86 cout << "\nSubscripts:\n";
87 
88 // output column heads
89 for ( int j = 0; j < size; j++ )
90 cout << setw( 3 ) << j << ' ';
91 
92 cout << '\n'; // start new line of output
93 
94 // output line of - characters
95 for ( int k = 1; k <= 4 * size; k++ )
96 cout << '-';
97 
98 cout << endl; // start new line of output
99 
100 } // end function printHeader
101 
©2004 Trần Minh Châu.
FOTECH. VNU.
68
fig04_20.cpp
(6 of 6)
102 // print one row of output showing the current
103 // part of the array being processed
104 void printRow( const int b[], int low, int mid, 
105 int high, int size )
106 {
107 // loop through entire array
108 for ( int m = 0; m < size; m++ )
109 
110 // display spaces if outside current subarray range
111 if ( m high )
112 cout << " ";
113 
114 // display middle element marked with a *
115 else
116 
117 if ( m == mid ) // mark middle value
118 cout << setw( 3 ) << b[ m ] << '*'; 
119 
120 // display other elements in subarray
121 else
122 cout << setw( 3 ) << b[ m ] << ' ';
123 
124 cout << endl; // start new line of output
125 
126 } // end function printRow
©2004 Trần Minh Châu.
FOTECH. VNU.
69
fig04_20.cpp
output (1 of 2)
Enter a number between 0 and 28: 6
Subscripts:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12
6 found in array element 3
Enter a number between 0 and 28: 25
Subscripts:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
16 18 20 22* 24 26 28
24 26* 28
24*
25 not found
©2004 Trần Minh Châu.
FOTECH. VNU.
70
fig04_20.cpp
output (2 of 2)
Enter a number between 0 and 28: 8
Subscripts:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12
8 10* 12
8*
8 found in array element 4
© 2004 Trần Minh Châu. FOTECH. VNU
71
Chương 4.
4.9 Mảng nhiều chiều
• Đa chỉ số
– int a[ 3 ][ 4 ];
– a[ i ][ j ]
– Các bảng có dòng và cột
– Dòng trước, cột sau
– “Mảng của mảng”
• a[0] là một mảng 4 phần tử
• a[0][0] là phần tử đầu tiên của mảng
Row 0
Row 1
Row 2
Column 0 Column 1 Column 2 Column 3
a[ 0 ][ 0 ]
a[ 1 ][ 0 ]
a[ 2 ][ 0 ]
a[ 0 ][ 1 ]
a[ 1 ][ 1 ]
a[ 2 ][ 1 ]
a[ 0 ][ 2 ]
a[ 1 ][ 2 ]
a[ 2 ][ 2 ]
a[ 0 ][ 3 ]
a[ 1 ][ 3 ]
a[ 2 ][ 3 ]
Row subscript 
(chỉ số dòng)
Array name
Column subscript
(chỉ số cột)
© 2004 Trần Minh Châu. FOTECH. VNU
72
Chương 4.
4.9 Mảng nhiều chiều
• Khởi tạo
– Mặc định là 0
– Khởi tạo, mỗi dòng trong 1 cặp ngoặc
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 
1 2
3 4
1 0
3 4
Row 0 Row 1
© 2004 Trần Minh Châu. FOTECH. VNU
73
Chương 4.
4.9 Mảng nhiều chiều
• Truy nhập đến như bình thường
cout << b[ 0 ][ 1 ];
– In ra 0
– Không sử dụng dấu phẩy (,)
cout << b[ 0, 1 ];
• Lỗi cú pháp
• Function prototype
– Phải chỉ rõ kích thước của các chỉ số
• Không đòi hỏi kích thước cho chỉ số đầu tiên, cũng như mảng 
1 chiều
– void printArray( int [][ 3 ] );
1 0
3 4
©2004 Trần Minh Châu.
FOTECH. VNU.
74
fig04_22.cpp
(1 of 2)
1 // Fig. 4.22: fig04_22.cpp
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 } }; 
15 
16 cout << "Values in array1 by row are:" << endl;
17 printArray( array1 );
18 
19 cout << "Values in array2 by row are:" << endl;
20 printArray( array2 );
21 
22 cout << "Values in array3 by row are:" << endl;
23 printArray( array3 );
24 
25 return 0; // indicates successful termination
26 } // end main
Chú ý nhiều cách khởi tạo.
Các phần tử trong array2
được gán từ dòng thứ nhất
rồi đến dòng thứ hai.
Chú ý cấu trúc của prototype.
©2004 Trần Minh Châu.
FOTECH. VNU.
75
fig04_22.cpp
(2 of 2)
fig04_22.cpp
output (1 of 1)
28 
29 // function to output array with two rows and three columns 
30 void printArray( int a[][ 3 ] ) 
31 { 
32 for ( int i = 0; i < 2; i++ ) { // for each row 
33 
34 for ( int j = 0; j < 3; j++ ) // output column values
35 cout << a[ i ][ j ] << ' '; 
36 
37 cout << endl; // start new line of output 
38 
39 } // end outer for structure 
40 
41 } // end function printArray 
Values in array1 by row are:
1 2 3
4 5 6
Values in array2 by row are:
1 2 3
4 5 0
Values in array3 by row are:
1 2 0
4 0 0
Vòng lặp for thường được 
dùng để quét qua mảng. Sử
dụng vòng lặp lồng nhau cho 
mảng nhiều chiều.
© 2004 Trần Minh Châu. FOTECH. VNU
76
Chương 4.
4.9 Mảng nhiều chiều
• Tiếp theo: chương trình ví dụ về khởi tạo mảng
– Chương trình lưu trữ điểm của sinh viên
– Mảng nhiều chiều (bảng)
– Dòng là sinh viên
– Cột là điểm
95 85
89 80
Quiz1 Quiz2
Student0
Student1
©2004 Trần Minh Châu.
FOTECH. VNU.
77
fig04_23.cpp
(1 of 6)
1 // Fig. 4.23: fig04_23.cpp
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 
11 
12 using std::setw;
13 using std::setprecision;
14 
15 const int students = 3; // number of students
16 const int exams = 4; // number of exams
17 
18 // 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 
©2004 Trần Minh Châu.
FOTECH. VNU.
78
fig04_23.cpp
(2 of 6)
24 int main()
25 {
26 // initialize student grades for three students (rows)
27 int studentGrades[ students ][ exams ] = 
28 { { 77, 68, 86, 73 },
29 { 96, 87, 89, 78 },
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 );
43 
©2004 Trần Minh Châu.
FOTECH. VNU.
79
fig04_23.cpp
(3 of 6)
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 ) 
49 << endl;
50 
51 return 0; // indicates successful termination
52 
53 } // end main
54 
55 // find minimum grade
56 int minimum( int grades[][ exams ], int pupils, int tests )
57 {
58 int lowGrade = 100; // initialize to highest possible grade
59 
60 for ( int i = 0; i < pupils; i++ ) 
61 
62 for ( int j = 0; j < tests; j++ ) 
63 
64 if ( grades[ i ][ j ] < lowGrade )
65 lowGrade = grades[ i ][ j ];
66 
67 return lowGrade;
68 
69 } // end function minimum
Tính điểm trung bình cho sinh 
viên. Ta truyền dòng chứa điểm 
của sinh viên vào hàm. Chú ý: 
studentGrades[0] cũng là
một mảng.
©2004 Trần Minh Châu.
FOTECH. VNU.
80
fig04_23.cpp
(4 of 6)
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
75 
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
86 
©2004 Trần Minh Châu.
FOTECH. VNU.
81
fig04_23.cpp
(5 of 6)
87 // determine average grade for particular student 
88 double average( int setOfGrades[], int tests ) 
89 { 
90 int total = 0; 
91 
92 // total all grades for one student 
93 for ( int i = 0; i < tests; i++ ) 
94 total += setOfGrades[ i ]; 
95 
96 return static_cast( total ) / tests; // average
97 
98 } // end function maximum
©2004 Trần Minh Châu.
FOTECH. VNU.
82
fig04_23.cpp
(6 of 6)
99 
100 // 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]";
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
©2004 Trần Minh Châu.
FOTECH. VNU.
83
fig04_23.cpp
output (1 of 1)
The array is:
[0] [1] [2] [3]
studentGrades[0] 77 68 86 73
studentGrades[1] 96 87 89 78
studentGrades[2] 70 90 86 81
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75

File đính kèm:

  • pdfNgôn ngữ lập trình C++ - Chương 4 Mảng.pdf