/****************************************************** ** Name: ** Filename: modemedian.cpp ** Project #: Deitel & Deitel 4.14 ** Project Description: Modify the program of Fig. 4.17 so function mode is capable of handling a tie for the mode value. Also modify function median so the two middle elements ar averaged in an array with an even number of elements. ** Output: Mean, Median and Mode of 100 element array. ** Input: None ** Algorithm: Make an 100 element array Calculate the mean: total of 100 elements / 100 Print Result Calculate the median: median is 100 /2 -1 and 100 / 2 then average of the two elements Print Result Calculate mode: check array[] > largest Determine if there is a tie: check if another array[] == to previous largest Print Result: End Program ******************************************************/ // Include files #include // used for cin, cout #include #include using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); void mean( const int [], int ); void median( int [], int ); void mode( int [], int [], int ); void bubbleSort( int[], int ); void printArray( const int[], int ); //Global Variables - should not be used without good reason. int main () { const int responseSize = 100; int frequency[ 10 ] = { 0 }, response[ responseSize ] = { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 4, 5, 6, 1, 6, 7, 7, 7, 7, 7 }; mean( response, responseSize ); median( response, responseSize ); mode( frequency, response, responseSize ); pause (); return 0; } void mean( const int answer[], int arraySize ) { int total = 0; cout << "********\n Mean\n********\n"; for ( int j = 0; j < arraySize; j++ ) total += answer[ j ]; cout << "The mean is the average value of the data\n" << "items. The mean is equal to the total of\n" << "all the data items divided by the number\n" << "of data items (" << arraySize << "). The mean value for\nthis run is: " << total << " / " << arraySize << " = " << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 4 ) << static_cast< double >( total ) / arraySize << "\n\n"; pause (); } void median( int answer[], int size ) { cout << "\n********\n Median\n********\n" << "The unsorted array of responses is"; printArray( answer, size ); bubbleSort( answer, size ); cout << "\n\nThe sorted array is"; printArray( answer, size ); cout << "\n\nThe median is element " << size / 2 - 1 << " and " << size / 2 << " of\nthe sorted " << size << " element array.\nFor this run the median is " << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 ) << static_cast< double >(answer[size/2] + answer[size/2 - 1])/2 << "\n\n" << endl; pause (); } void mode( int freq[], int answer[], int size ) { int rating, rating1, largest = 0, modeValue = 0, tie = 0; cout << "\n********\n Mode\n********\n"; for ( rating = 1; rating <= 9; rating++ ) freq[ rating ] = 0; for ( int j = 0; j < size; j++ ) ++freq[ answer[ j ] ]; cout << "Response"<< setw( 11 ) << "Frequency" << setw( 19 ) << "Histogram\n\n" << setw( 55 ) << "1 1 2 2\n" << setw( 56 ) << "5 0 5 0 5\n\n"; for ( rating = 1; rating <= 9; rating++ ) { cout << setw( 8 ) << rating << setw( 11 ) << freq[ rating ] << " "; //Finds the largest frequency if( freq[ rating ] > largest ){ largest = freq[ rating ]; modeValue = rating; } //Checks to see if another frequency is equal to the previous found for ( rating1 = 1; rating1 <= 9; rating1++ ){ if( freq[ rating1 ] == largest ){ tie = rating1; } } for ( int h = 1; h <= freq[ rating ]; h++ ) cout << '*'; cout << '\n'; } //Checks to see if the largest is in the same response or not if ( tie != modeValue ){ cout << "The mode is the most frequent value.\n" << "For this run the mode is " << modeValue << " and " << tie << " which both occurred " << largest << " times." << endl; } else cout << "The mode is the most frequent value.\n" << "For this run the mode is " << modeValue << " which occurred " << largest << " times."; } void bubbleSort( int a[], int size ) { int hold; for ( int pass = 1; pass < size; pass++ ) for ( int j = 0; j < size - 1; j++ ) if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } } void printArray( const int a[], int size ) { for ( int j = 0; j < size; j++ ) { if ( j % 20 == 0 ) cout << endl; cout << setw( 2 ) << a[ j ]; } } void instruct (void) { // Declaration section // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output ******** Mean ******** The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (100). The mean value for this run is: 689 / 100 = 6.8900 Press any key to continue... ******** Median ******** The unsorted array of responses is 6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3 5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8 7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 7 7 7 7 7 The sorted array is 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 The median is element 50 and 49 of the sorted 100 element array. For this run the median is 4.50 Press any key to continue... ******** Mode ******** Response Frequency Histogram 1 1 2 2 5 0 5 0 5 1 1 * 2 3 *** 3 4 **** 4 5 ***** 5 7 ******* 6 9 ********* 7 26 ************************** 8 26 ************************** 9 19 ******************* The mode is the most frequent value. For this run the mode is 7 and 8 which both occurred 26 times. Press any key to continue... */