给你找个例子,模仿一下。
#include <stdio.h>
#define SIZE 100
void mean(const int answer[]);
void median(int answer[]);
void mode(int freq[], const int answer[]);
void bubbleSort(int answer[]);
void printArray(const int answer[]);
int main(void)
{
int response[ SIZE ] = { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8, 7, 1,
6, 7, 8, 9, 3, 9, 8, 7, 1, 7,
7, 8, 9, 8, 9, 8, 9, 7, 1, 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, 1,
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
4, 5, 6, 1, 6, 5, 7, 8, 7, 9};
int frequency[10] = {0};
mean(response);
median(response);
mode(frequency, response);
return 0;
}
void mean(const int answer[])
{
int counter;
int total = 0;
printf( "%s\n%s\n%s\n", "**********", "*
Mean
*", "**********" );
for(counter = 0; counter < SIZE; counter++){
total += answer[counter];
}
printf( "Mean is the average value of all datas\n" );
printf( "Mean value for this run is: " );
printf("%d / %d = %.2lf\n\n", total, SIZE, (double)total/SIZE);
}
void median(int answer[])
{
printf( "\n%s\n%s\n%s\n", "*************", "*
Median
*", "*************" );
printf("Unsortted array of response: \n");
printArray(answer);
printf("\n");
printf("\nSorted array of response: \n");
bubbleSort(answer);
printArray(answer);
printf("\n\n");
if(SIZE%2 == 0){
printf("Median is average of elements %d and %d\n", SIZE/2, SIZE/2+1);
printf("in the sorted %d element array.\n", SIZE);
printf( "For this run median: %.2lf\n", (double)(answer[SIZE/2]+answer[SIZE/2+1])/2);
}
else{
printf("Media is average of elements %d" , (SIZE+1)/2);
printf("in the sorted %d element array.\n", SIZE);
printf( "For this run median: %d", answer[(SIZE+1)/2]);
}
}
void mode(int freq[], const int answer[])
{
int rate;
//count for elements 1~~9
int loop;
//count for all elements 0~SIZE
int largest = 0;
//largest frequency
int counter = 0;
//flag to count number of modes
int modeValue[10] = {0};
//most frequency response
printf("\n%s\n%s\n%s\n", "**********", "*
Mode
*", "**********");
for(rate = 1; rate <= 9; rate++){
freq[rate] = 0;
}
// inti frequencies as 0
for(loop = 0; loop < SIZE; loop++){
++freq[answer[loop]];
}
//traverse array and increment corresponding frequency
printf("%s%11s%13s\n\n", "Response", "Frequency", "Histogram");
for(rate = 1; rate <=9 ;rate++){
printf("%3d%10d
", rate, freq[rate]);
if(freq[rate]>largest){
largest = freq[rate];
for(loop = 0; loop < 10; loop++){
modeValue[loop] = 0;
}
modeValue[rate] = largest;
++counter;
}
else if(freq[rate] == largest){
modeValue[rate] = largest;
++counter;
}
for(loop = 1; loop <= freq[rate]; loop++){
printf("*");
}
printf("\n");
}
printf("\n");
if(counter > 1){
printf("Modes are: ");
}
else{
printf("Mode is: ");
}
for(loop = 1; loop <= 9; loop++){
if(modeValue[loop] != 0){
printf("%d with a frequency of %d\n\t\t", loop, modeValue[loop]);
}
}
printf("\n");
}
void bubbleSort(int answer[])
{
int loop;
int i;
int tmp;
for(loop = 0; loop < SIZE; loop++){
for(i = 0; i < SIZE-i; i++){
if(answer[i]>answer[i+1]){
tmp = answer[i];
answer[i] = answer[i+1];
answer[i+1] = tmp;
}
}
}
}
void printArray(const int answer[])
{
int loop;
for(loop = 0; loop < SIZE; loop++){
if(loop%20 == 0){
printf("\n");
}
printf("%2d", answer[loop]);
}
}