Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Case Study Dale Roberts, Lecturer IUPUI [email protected] Dale Roberts.
Download ReportTranscript Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Case Study Dale Roberts, Lecturer IUPUI [email protected] Dale Roberts.
Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Case Study Dale Roberts, Lecturer IUPUI [email protected] Dale Roberts Sorting Arrays Sorting data Important computing application Virtually every organization must sort some data Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical ), no change If decreasing order, elements exchanged Repeat Example: original: 3 4 2 6 7 pass 1: 3 2 4 6 7 pass 2: 2 3 4 6 7 Small elements "bubble" to the top Dale Roberts Searching Arrays: Linear Search vs Binary Search Search an array for a key value Linear search Simple Compare each element of array with key value Useful for small and unsorted arrays Binary search For sorted arrays Compares middle element with key If equal, match found If key < middle, looks in first half of array If key > middle, looks in last half Repeat Very fast; at most n steps, where 2n > number of elements 30 element array takes at most 5 steps 25 > 30 so at most 5 steps Dale Roberts Case Study: Computing Mean, Median and Mode Using Arrays Mean – average Median – number in middle of sorted list Example: 1, 2, 3, 4, 5 3 is the median Mode – number that occurs most often Example: 1, 1, 1, 2, 3, 3, 4, 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 is the mode /* Fig. 6.16: fig06_16.c This program introduces the topic of survey data analysis. It computes the mean, median, and mode of the data */ #include <stdio.h> #define SIZE 99 void void void void void mean( const int [] ); median( int [] ); mode( int [], const int [] ) ; bubbleSort( int [] ); printArray( const int [] ); Function prototypes int main() { int frequency[ 10 ] = { 0 }; Dale Roberts 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 int response[ { 6, 7, 8, 7, 8, 9, 6, 7, 8, 7, 8, 9, 6, 7, 8, 7, 8, 9, 5, 6, 7, 7, 8, 9, 7, 4, 4, 4, 5, 6, SIZE ] = 9, 8, 7, 5, 9, 8, 9, 3, 9, 8, 9, 8, 7, 8, 7, 8, 9, 8, 2, 5, 3, 6, 8, 7, 2, 5, 3, 1, 6, 5, 8, 7, 8, 9, 9, 9, 9, 8, 8, 7, 9, 8, 7, 7, 8, 7, 4, 9, 7, 8, 8, 9, 7, 8, 8, 7, 8, 9, 9, 2, 5, 3, 6, 4, 7, 8, 5, 6, 7 }; mean( response ); median( response ); mode( frequency, response ); return 0; Initialize array Call functions mean, median, and mode } void mean( const int answer[] ) { int j, total = 0; printf( "%s\n%s\n%s\n", "********", " Define function mean Mean", "********" ); for ( j = 0; j <= SIZE - 1; j++ ) total += answer[ j ]; printf( "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 ( %d ). The mean value for\n" "this run is: %d / %d = %.4f\n\n", SIZE, total, SIZE, ( double ) total / SIZE ); } Dale Roberts 50 51 void median( int answer[] ) 52 { 53 54 55 printf( "\n%s\n%s\n%s\n%s", Define function median "********", " Median", "********", "The unsorted array of responses is" ); 56 57 printArray( answer ); 58 59 bubbleSort( answer ); printf( "\n\nThe sorted array is" ); 60 61 printArray( answer ); printf( "\n\nThe median is element %d of\n" 62 63 "the sorted %d element array.\n" "For this run the median is %d\n\n", 64 SIZE / 2, SIZE, answer[ SIZE / 2 ] ); 65 } 66 67 void mode( int freq[], const int answer[] ) 68 { 69 int rating, j, h, largest = 0, modeValue = 0; 70 71 printf( "\n%s\n%s\n%s\n", 72 "********", " Mode", "********" ); 73 74 for ( rating = 1; rating <= 9; rating++ ) 75 freq[ rating ] = 0; 76 77 for ( j = 0; j <= SIZE - 1; j++ ) 78 ++freq[ answer[ j ] ]; 79 Sort Array Print middle element Is this computation of the median element correct? Notice that SIZE must be odd for this to be correct. Is SIZE odd? Define function mode Notice how the subscript in freq[] is the value of an element in response[] (answer[]) Increase frequency[] depending on response[] Dale Roberts Does this code implement the definition of mode that you learned in math class? 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 } 102 printf( "%s%11s%19s\n\n%54s\n%54s\n\n", "Response", "Frequency", "Histogram", "1 1 2 2", "5 0 5 0 5" ); for ( rating = 1; rating <= 9; rating++ ) { printf( "%8d%11d ", rating, freq[ rating ] ); if ( freq[ rating ] > largest ) { largest = freq[ rating ]; modeValue = rating; } for ( h = 1; h <= freq[ rating ]; h++ ) printf( "*" ); printf( "\n" ); Print stars depending on value of frequency[] } printf( "The mode is the most frequent value.\n" "For this run the mode is %d which occurred" " %d times.\n", modeValue, largest ); Dale Roberts 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 void bubbleSort( int a[] ) { int pass, j, hold; for ( pass = 1; pass <= SIZE - 1; pass++ ) for ( j = 0; j <= SIZE - 2; j++ ) if ( a[ hold a[ j a[ j } j = ] + Define bubbleSort Bubble sort can make the smallest values “sink” by scanning top to bottom using < or make the largest values “float” by scanning bottom to top using >. ] > a[ j + 1 ] ) { a[ j ]; Bubble sort: if elements = a[ j + 1 ]; out of order, swap them. 1 ] = hold; } void printArray( const int a[] ) { int j; for ( j = 0; j <= SIZE - 1; j++ ) { if ( j % 20 == 0 ) printf( "\n" ); 127 128 Define printArray printf( "%2d", a[ j ] ); } 129 } Dale Roberts 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 (99). The mean value for this run is: 681 / 99 = 6.8788 ******** Median ******** The unsorted array of responses is 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 5 7 8 7 The sorted 1 2 2 2 3 5 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 array 3 3 3 6 6 6 7 7 7 8 8 8 9 9 9 is 4 4 6 6 7 7 8 8 9 9 4 7 7 8 9 4 7 7 8 9 4 7 7 8 9 5 7 8 8 9 5 7 8 8 9 5 7 8 8 9 5 7 8 8 9 5 7 8 8 9 5 7 8 8 9 5 7 8 8 The median is element 49 of the sorted 99 element array. For this run the median is 7 ******** Mode ******** Response Frequency Histogram 5 1 0 1 5 2 0 2 5 1 1 * 2 3 *** 3 4 **** 4 5 ***** 5 8 ******** 6 9 ********* 7 23 *********************** 8 27 *************************** 9 19 ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times. Dale Roberts