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

Đề mục

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

 

ppt83 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1638 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng 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
g trình) Một vài loại mảng mảng dựa vào con trỏ (Pointer-based arrays) (C-like) mảng là đối tượng (Arrays as objects) (C++) 4.2	Mảng Mảng Tập hợp các vùng nhớ liên tiếp Cùng tên, cùng kiểu (int, char, ...) Truy nhập đến 1 phần tử Chỉ ra tên mảng và vị trí - position (chỉ số - index) Cú pháp: tên_mảng[ chỉ_số ] Phần tử đầu tiên ở vị trí 0 Mảng c có n phần tử c[ 0 ], c[ 1 ] … c[ n - 1 ] Phần tử thứ N ở vị trí thứ N-1 4.2	Mảng Phần tử của mảng cũng như các biến khác Gán giá trị và in mảng số nguyên c c[ 0 ] = 3; cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 int n[ 10 ]; // n is an array of 10 integers 15 16 // initialize elements of array n to 0 17 for ( int i = 0; i 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 // use initializer list to initialize array n 15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 16 17 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 // constant variable can be used to specify array size 15 const int arraySize = 10; 16 17 int s[ arraySize ]; // array s has 10 elements 18 19 for ( int i = 0; i 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 const int x = 7; // initialized constant variable 11 12 cout 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 const int arraySize = 10; 11 12 int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 13 14 int total = 0; 15 16 // sum contents of array a 17 for ( int i = 0; i 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 const int arraySize = 10; 15 int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 16 17 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 #include 13 #include 14 15 int main() 16 { 17 const int arraySize = 7; 18 int frequency[ arraySize ] = { 0 }; 19 20 srand( time( 0 ) ); // seed random-number generator 21 22 // roll die 6000 times 23 for ( int roll = 1; roll 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 // define array sizes 15 const int markSize = 40; // size of array of marks 16 const int frequencySize = 11; // size of array frequency 17 18 // place student marks in array of marks 19 int marks[ markSize ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 20 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 21 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; 22 23 // initialize frequency counters to 0 24 int frequency[ frequencySize ] = { 0 }; 25 fig04_11.cpp(2 of 2) 26 // for each student's mark, select value of an element of array 27 // responses and use that value as subscript in array 28 // frequency to determine element to increment 29 for ( int student = 0; student > string2; Ghi dữ liệu vào của người dùng vào xâu Dừng lại ở ký tự trắng đầu tiên (tab, newline, blank…) Thêm vào ký tự null Nếu nhập quá nhiều, dữ liệu sẽ tràn mảng Ta cần phải tránh điều này (mục 5.12 sẽ giải thích phương pháp) In xâu cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 char string1[ 20 ], // reserves 20 characters 12 char string2[] = "string literal"; // reserves 15 characters 13 14 // read string from user into array string2 15 cout > string1; // reads "hello" [space terminates input] 17 18 // output strings 19 cout > string1; // reads "there" 29 cout 4 5 using std::cout; 6 using std::endl; 7 8 void staticArrayInit( void ); // function prototype 9 void automaticArrayInit( void ); // function prototype 10 11 int main() 12 { 13 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 void modifyArray( int [], int ); // appears strange 13 void modifyElement( int ); 14 15 int main() 16 { 17 const int arraySize = 5; // size of array a 18 int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize a 19 20 cout 4 5 using std::cout; 6 using std::endl; 7 8 void tryToModifyArray( const int [] ); // function prototype 9 10 int main() 11 { 12 int a[] = { 10, 20, 30 }; 13 14 tryToModifyArray( a ); 15 16 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 int main() 13 { 14 const int arraySize = 10; // size of array a 15 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 16 int hold; // temporary location used to swap array elements 17 18 cout a[ j + 1 ] ) { 34 hold = a[ j ]; 35 a[ j ] = a[ j + 1 ]; 36 a[ j + 1 ] = hold; 37 38 } // end if 39 fig04_16.cpp(3 of 3)fig04_16.cppoutput (1 of 1) 40 cout 5 6 using std::cout; 7 using std::endl; 8 using std::fixed; 9 using std::showpoint; 10 11 #include 12 13 using std::setw; 14 using std::setprecision; 15 16 void mean( const int [], int ); 17 void median( int [], int ); 18 void mode( int [], int [], int ); 19 void bubbleSort( int[], int ); 20 void printArray( const int[], int ); 21 22 int main() 23 { 24 const int responseSize = 99; // size of array responses 25 fig04_17.cpp(2 of 8) 26 int frequency[ 10 ] = { 0 }; // initialize array frequency 27 28 // initialize array responses 29 int response[ responseSize ] = 30 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 31 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 32 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 33 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 34 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 35 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 36 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 37 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 38 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 39 4, 5, 6, 1, 6, 5, 7, 8, 7 }; 40 41 // process responses 42 mean( response, responseSize ); 43 median( response, responseSize ); 44 mode( frequency, response, responseSize ); 45 46 return 0; // indicates successful termination 47 48 } // end main 49 fig04_17.cpp(3 of 8) 50 // calculate average of all response values 51 void mean( const int answer[], int arraySize ) 52 { 53 int total = 0; 54 55 cout ( total ) / arraySize 71 largest ) { 125 largest = freq[ rating ]; 126 modeValue = rating; 127 128 } // end if 129 130 // output histogram bar representing frequency value 131 for ( int k = 1; k a[ j + 1 ] ) { 158 hold = a[ j ]; 159 a[ j ] = a[ j + 1 ]; 160 a[ j + 1 ] = hold; 161 162 } // end if 163 164 } // end function bubbleSort 165 fig04_17.cpp(8 of 8) 166 // output array contents (20 values per row) 167 void printArray( const int a[], int size ) 168 { 169 for ( int i = 0; i middle Lặp lại ở nửa cuối Rất nhanh Nhiều nhất là N bước với 2 > số phần tử của mảng mảng 30 phần tử cần nhiều nhất 5 bước 2 > 30 N 5 fig04_19.cpp(1 of 2) 1 // Fig. 4.19: fig04_19.cpp 2 // Linear search of an array. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int linearSearch( const int [], int, int ); // prototype 10 11 int main() 12 { 13 const int arraySize = 100; // size of array a 14 int a[ arraySize ]; // create array a 15 int searchKey; // value to locate in a 16 17 for ( int i = 0; i > searchKey; 22 23 // attempt to locate searchKey in array a 24 int element = linearSearch( a, searchKey, arraySize ); 25 fig04_19.cpp(2 of 2) 26 // display results 27 if ( element != -1 ) 28 cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include 10 11 using std::setw; 12 13 // function prototypes 14 int binarySearch( const int [], int, int, int, int ); 15 void printHeader( int ); 16 void printRow( const int [], int, int, int, int ); 17 18 int main() 19 { 20 const int arraySize = 15; // size of array a 21 int a[ arraySize ]; // create array a 22 int key; // value to locate in a 23 24 for ( int i = 0; i > key; 29 30 printHeader( arraySize ); 31 32 // search for key in array a 33 int result = 34 binarySearch( a, key, 0, arraySize - 1, arraySize ); 35 36 // display results 37 if ( result != -1 ) 38 cout high ) 112 cout 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 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 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 highGrade ) 81 highGrade = grades[ i ][ j ]; 82 83 return highGrade; 84 85 } // end function maximum 86 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 ( total ) / tests; // average 97 98 } // end function maximum 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 fig04_23.cppoutput (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:

  • pptBài giảng Ngôn ngữ lập trình C++ - Chương 4 Mảng.ppt
Tài liệu liên quan