#include <stdio.h>
#define SIZE 99

void mean(const int answer[]);
void median(int answer[]);
void mode(int freq[], const int answer[]);
void bubbleSort(int a[]);
void printArray(const int a[]);

int main(void) {
    int frequency[10] = {0};
    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
    };

    mean(response);
    median(response);
    mode(frequency, response);
    
    return 0;
}

void mean(const int answer[]) {
    int total = 0;
    for (size_t j = 0; j < SIZE; ++j) {
        total += answer[j];
    }
    printf("Mean: %.4f\n", (double) total / SIZE);
}

void median(int answer[]) {
    bubbleSort(answer);
    if (SIZE % 2 == 0) {
        double med = (answer[SIZE / 2 - 1] + answer[SIZE / 2]) / 2.0;
        printf("Median: %.1f\n", med);
    } else {
        printf("Median: %d\n", answer[SIZE / 2]);
    }
}

void mode(int freq[], const int answer[]) {
    for (size_t rating = 1; rating <= 9; ++rating) {
        freq[rating] = 0;
    }
    for (size_t j = 0; j < SIZE; ++j) {
        ++freq[answer[j]];
    }
    
    int largest = 0;
    for (size_t rating = 1; rating <= 9; ++rating) {
        if (freq[rating] > largest) {
            largest = freq[rating];
        }
    }
    
    printf("Mode(s) appearing %d times: ", largest);
    for (size_t rating = 1; rating <= 9; ++rating) {
        if (freq[rating] == largest) {
            printf("%zu ", rating);
        }
    }
    printf("\n");
}

void bubbleSort(int a[]) {
    for (int pass = 1; pass < SIZE; ++pass) {
        for (size_t j = 0; j < SIZE - 1; ++j) {
            if (a[j] > a[j + 1]) {
                int hold = a[j];
                a[j] = a[j + 1];
                a[j + 1] = hold;
            }
        }
    }
}

void printArray(const int a[]) {
    for (size_t j = 0; j < SIZE; ++j) {
        if (j % 20 == 0) {
            printf("\n");
        }
        printf("%2d", a[j]);
    }
    printf("\n");
}