-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_sort.c
146 lines (119 loc) · 3.85 KB
/
array_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Survey data analysis with arrays:
// computing the mean, median and mode of the data.
#include<stdio.h>
#define SIZE 99
//function prototypes
void mean(const unsigned int answer[]);
void median(unsigned int answer[]);
void mode(unsigned int freq[], unsigned const int answer[]);
void bubbleSort(unsigned int a[]);
void printArray(unsigned const int a[]);
// function main begins program execution
int main(void) {
unsigned int frequency[10] = { 0 }; // initialize array frequency
// initialize array response
unsigned int response[SIZE] = {
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,5,7,8,7 };
//process responses
mean(response);
median(response);
mode(frequency, response);
}
// calculate average of all response values
void mean(const unsigned int answer[]) {
printf_s("%s\n%s\n%s\n", "********", " Mean", "********");
unsigned int total = 0; // variable to hold sum of array element
// total response values
for (size_t j = 0; j < SIZE; ++j) {
total += answer[j];
}
printf_s("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 (%u). the mean value for\n"
"this run is: %u / %u = %.4f\n\n", SIZE, total, SIZE, (double)total / SIZE);
}
// sort array and determine median element's value
void median(unsigned int answer[]) {
printf_s("\n%s\n%s\n%s\n%s",
"********", " Median", "********",
"The unsorted array of responses is");
printArray(answer); // output unsorted array
bubbleSort(answer); // sort array
printf_s("%s", "\n\nThe sorted array is");
printArray(answer); // output sorted array
// display median element
printf_s("\n\nThe median is element %u of\n"
"the sorted %u element array.\n"
"For this run the median is %u\n\n",
SIZE / 2, SIZE, answer[SIZE / 2]);
}
// determine the most frequent response
void mode(unsigned int freq[], const unsigned int answer[]) {
printf_s("\n%s\n%s\n%s\n", "********", " Mode", "********");
// initialize frequencies to 0
for (size_t rating = 1; rating <= 9; ++rating) {
freq[rating] = 0;
}
// summarize frequencies
for (size_t j = 0; j < SIZE; ++j) {
++freq[answer[j]];
}
// output headers for result columns
printf_s("%s%11s%19s\n\n%54s%54s\n\n",
"Response", "Frequency", "Histogram",
"1 1 2 2", "5 0 5 0 5");
// output results
unsigned int largest = 0; // response largest frequency
unsigned int modeValue = 0; // response most frequent response
for (size_t rating = 1; rating <= 9; ++rating) {
printf_s("%8u%11u ", rating, freq[rating]);
// keep track of mode value and largest frequency value
if (freq[rating] > largest) {
largest = freq[rating];
modeValue = rating;
}
// output histogram bar representing frequency value
for (unsigned int h = 1; h <= freq[rating]; ++h) {
printf_s("%s", "*");
}
puts(""); // begin new line of output
}
// display the mode value
printf_s("\nThe mode is the most frequent value.\n"
"For this run the mode is %u which occurred"
" %u times.\n", modeValue, largest);
}
// function that sorts an array with bubble sort algorithm
void bubbleSort(unsigned int a[]) {
// loop to control number of passes
for (unsigned int pass = 1; pass < SIZE; ++pass) {
// loop control number of comparisons per pass
for (size_t j = 0; j < SIZE - 1; ++j) {
// swap elements if out of order
if (a[j] > a[j + 1]) {
unsigned int hold = a[j];
a[j] = a[j + 1];
a[j + 1] = hold;
}
}
}
}
//output array contents (20 values per row)
void printArray(const unsigned int a[]) {
// output array contents
for (size_t j = 0; j < SIZE; ++j) {
if (j % 20 == 0) { // begin new line every 20 values
puts("");
}
printf_s("%2u", a[j]);
}
}