4.7 Ví dụ: sử dụng mảng để tính Mean, Median Mode 48 • Mean – Giá trị trung bình (tổng/số phần tử) • Median – Giá trị dãy xếp – 1, 2, 3, 4, (3 median) – Nếu số phần tử số chẵn, lấy trung bình số • Mode – Giá trị xuất nhiều – 1, 1, 1, 2, 3, 3, 4, (1 mode) © 2004 Trần Minh Châu FOTECH VNU Chương 1 // Fig 4.17: fig04_17.cpp // This program introduces the topic of survey data analysis // It computes the mean, median, and mode of the data #include using using using using 10 11 #include 12 13 14 using std::setw; using std::setprecision; 15 16 17 18 19 20 void void void void void 21 22 23 24 int main() { const int responseSize = 99; 25 49 fig04_17.cpp (1 of 8) std::cout; std::endl; std::fixed; std::showpoint; mean( const int [], int ); median( int [], int ); mode( int [], int [], int ); bubbleSort( int[], int ); printArray( const int[], int ); // size of array responses ©2004 Trần Minh Châu FOTECH VNU 26 int frequency[ 10 ] = { }; 27 28 29 30 31 32 33 34 35 36 37 38 39 // initialize array responses int response[ responseSize ] = { 6, 7, 8, 9, 8, 7, 8, 9, 7, 8, 9, 5, 9, 8, 7, 8, 6, 7, 8, 9, 3, 9, 8, 7, 7, 8, 9, 8, 9, 8, 9, 7, 6, 7, 8, 7, 8, 7, 9, 8, 7, 8, 9, 8, 9, 8, 9, 7, 5, 6, 7, 2, 5, 3, 9, 4, 7, 8, 9, 6, 8, 7, 8, 9, 7, 4, 4, 2, 5, 3, 8, 7, 4, 5, 6, 1, 6, 5, 7, 8, 40 41 42 43 44 // process responses mean( response, responseSize ); median( response, responseSize ); mode( frequency, response, responseSize ); 45 46 return 0; 47 48 50 // initialize array frequency 8, 9, 7, 8, 8, 7, 8, 9, 9, 2, 5, 3, 6, 4, 7, 8, 5, 6, }; fig04_17.cpp (2 of 8) // indicates successful termination } // end main 49 ©2004 Trần Minh Châu FOTECH VNU 50 51 52 53 54 55 cout