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