training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

fig06_16_2.c (5864B)


      1 /* Exercise 6.14 */
      2 
      3 #include <stdio.h>
      4 #define SIZE 100
      5 
      6 /* function prototypes */
      7 void mean( const int answer[] );
      8 void median( int answer[] );
      9 void mode( int freq[], const int answer[] ) ;
     10 void bubbleSort( int a[] );
     11 void printArray( const int a[] );
     12 
     13 /* function main begins program execution */
     14 int main()
     15 {
     16    int frequency[ 10 ] = { 0 }; /* initialize array frequency */
     17    
     18    /* initialize array response */
     19    int response[ SIZE ] =             
     20       { 0, 8, 8, 9, 8, 9, 8, 9, 8, 9,
     21         7, 8, 9, 9, 9, 8, 9, 8, 9, 8,
     22         6, 9, 8, 9, 3, 9, 8, 9, 8, 9,
     23         7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
     24         6, 7, 8, 9, 8, 9, 9, 8, 9, 2,
     25         7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
     26         5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
     27         7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
     28         7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
     29         4, 5, 6, 1, 6, 5, 7, 8, 7 };
     30 
     31    /* process responses */
     32    mean( response );
     33    median( response );
     34    mode( frequency, response );
     35 
     36    return 0; /* indicates successful termination */
     37 
     38 } /* end main */
     39 
     40 /* calculate average of all response values */
     41 void mean( const int answer[] )
     42 {
     43    int j; /* counter for totaling array elements */
     44    int total = 0; /* variable to hold sum of array elements */
     45 
     46    printf( "%s\n%s\n%s\n", "********", "  Mean", "********" );
     47 
     48    /* total response values */
     49    for ( j = 0; j < SIZE; j++ ) {
     50       total += answer[ j ];
     51    } /* end for */
     52 
     53    printf( "The mean is the average value of the data\n"
     54            "items. The mean is equal to the total of\n"
     55            "all the data items divided by the number\n"
     56            "of data items ( %d ). The mean value for\n"
     57            "this run is: %d / %d = %.4f\n\n",
     58            SIZE, total, SIZE, (  double  ) total / SIZE );
     59 } /* end function mean */
     60 
     61 /* sort array and determine median element's value */
     62 void median( int answer[] )
     63 {
     64    printf( "\n%s\n%s\n%s\n%s", 
     65            "********", " Median", "********", 
     66            "The unsorted array of responses is" );
     67 
     68    printArray( answer ); /* output unsorted array */
     69 
     70    bubbleSort( answer ); /* sort array */
     71 
     72    printf( "\n\nThe sorted array is" );
     73    printArray( answer ); /* output sorted array */
     74 
     75    /* display median element */
     76 
     77    if( !(SIZE % 2) )
     78       /* if the number is pair */
     79       printf("\n\nThe median is the mean of the two middle elements\n"
     80 	     "%d and %d of the sorted %d element array.\n"
     81 	     "For this run the median is %d\n\n",
     82                 answer[SIZE / 2], answer[SIZE / 2 + 1], SIZE,
     83                 (answer[SIZE / 2] + answer[SIZE / 2 + 1]) / 2 );
     84    else
     85       printf( "\n\nThe median is element %d "
     86               "of the sorted %d element array.\n"
     87               "For this run the median is %d\n\n",
     88               SIZE / 2, SIZE, answer[ SIZE / 2 ] );
     89 
     90 } /* end function median */
     91 
     92 /* determine most frequent response */
     93 void mode( int freq[], const int answer[] )
     94 {
     95    int rating; /* counter for accessing elements 1-9 of array freq */
     96    int j; /* counter for summarizing elements 0-98 of array answer */
     97    int h; /* counter for diplaying histograms of elements in array freq */
     98    int largest = 0; /* represents largest frequency */
     99    int p_mode = 0; /* represents the mode clone */
    100    int modeValue = 0; /* respesents most frequent response */
    101 
    102    printf( "\n%s\n%s\n%s\n", 
    103            "********", "  Mode", "********" );
    104 
    105    /* initialize frequencies to 0 */
    106    for ( rating = 1; rating <= 9; rating++ ) {
    107       freq[ rating ] = 0;
    108    } /* end for */
    109 
    110    /* summarize frequencies */
    111    for ( j = 0; j < SIZE; j++ ) {
    112       ++freq[ answer[ j ] ];
    113    } /* end for */
    114 
    115    /* output headers for result columns */
    116    printf( "%s%11s%19s\n\n%54s\n%54s\n\n",
    117            "Response", "Frequency", "Histogram",
    118            "1    1    2    2", "5    0    5    0    5" );
    119 
    120    /* output results */
    121    for ( rating = 1; rating <= 9; rating++ ) {
    122       printf( "%8d%11d          ", rating, freq[ rating ] );
    123 
    124       /* keep track of mode value and largest frequency value */
    125       if ( freq[ rating ] > largest ) {
    126          largest = freq[ rating ];
    127          modeValue = rating;
    128       } /* end if */
    129       else if(freq[ rating ] == largest) {
    130 	 /* There is a pair of modes */
    131 	 p_mode = rating;
    132       }
    133 
    134       /* output histogram bar representing frequency value */
    135       for ( h = 1; h <= freq[ rating ]; h++ ) {
    136          printf( "*" );
    137       } /* end inner for */
    138 
    139       printf( "\n" ); /* being new line of output */
    140    } /* end outer for */
    141 
    142    /* display the mode value */
    143    printf("The mode is the most frequent value.\n");
    144    if(freq[p_mode] == freq[modeValue])
    145       printf("For this run the modes are %d and %d which"
    146 	     " occurred %d times.\n", modeValue, p_mode, largest );
    147    else
    148       printf("For this run the mode is %d which occurred"
    149       " %d times.\n", modeValue, largest );
    150 
    151 } /* end function mode */
    152 
    153 /* function that sorts an array with bubble sort algorithm */
    154 void bubbleSort( int a[] )
    155 {
    156    int pass; /* pass counter */
    157    int j;    /* comparison counter */
    158    int hold; /* temporary location used to swap elements */
    159 
    160    /* loop to control number of passes */
    161    for ( pass = 1; pass < SIZE; pass++ ) {
    162 
    163       /* loop to control number of comparisons per pass */
    164       for ( j = 0; j < SIZE - 1; j++ ) {
    165 
    166          /* swap elements if out of order */
    167          if ( a[ j ] > a[ j + 1 ] ) {
    168             hold = a[ j ];
    169             a[ j ] = a[ j + 1 ];
    170             a[ j + 1 ] = hold;
    171          } /* end if */
    172 
    173       } /* end inner for */
    174 
    175    } /* end outer for */
    176 
    177 } /* end function bubbleSort */
    178 
    179 /* output array contents (20 values per row) */
    180 void printArray( const int a[] )
    181 {
    182    int j; /* counter */
    183 
    184    /* output array contents */
    185    for ( j = 0; j < SIZE; j++ ) {
    186 
    187       if ( j % 20 == 0 ) { /* begin new line every 20 values */
    188          printf( "\n" );
    189       } /* end if */
    190 
    191       printf( "%2d", a[ j ] );
    192    } /* end for */
    193 
    194 } /* end function printArray */
    195