#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 99
void mean(int b[]);
void median(int b[]);
void mode(int b[]);
void bubbleSort(int b[]);
int findFrequentValue(int b[]);
int findFrequentIndex(int b[]);
void printHistogram(int b[]);
int * fillResponseArray(int response[]);
int main(void) {
srand(time(NULL));
int response[SIZE]={0}; //first fill with 0's
fillResponseArray(response);
mean(response);
median(response);
mode(response);
return 0;
}
int * fillResponseArray(int response[]){
int i;
for(i=0;i<SIZE;i++)
response[i]=rand()%10;
return response;
}
/*finds mean*/
void mean(int b[]) {
int sum=0;
int i;
printf( "********\n"
"MEAN\n"
"********\n\n"
"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.\n" );
for(i=0;i<SIZE;i++)
sum+=b[i];
printf ( "\n%d given responses SUM: %d MEAN: %.2f\n", SIZE, sum, (double)sum/SIZE );
printf("\n");
}
/*finds median*/
void median(int b[]) {
int i;
printf( "********\n"
"MEDIAN\n"
"********\n");
printf("\nUnsorted array\n");
for(i=0;i<SIZE;i++){
if(i!=0&&i%10==0)
printf("\n");
printf("%d ",b[i]);
}
printf("\n");
bubbleSort(b);
printf("Sorted array\n");
for(i=0;i<SIZE;i++){
if(i!=0&&i%10==0)
printf("\n");
printf("%d ",b[i]);
}
printf("\n\nMEDIAN is the %d th element: %d",SIZE/2,b[SIZE/2]);
printf("\n");
}
/*finds mode*/
void mode(int b[]) {
int frequency[10]={0},i;
printf( "\n********\n"
"MODE\n"
"********\n");
printf("Mode is the most frequent value.\n");
/* Fill the frequency array with occurrences of indexes*/
for(i=0;i<SIZE;i++)
frequency[ b[i] ]++;
/*print frequency array*/
for(i=0;i<10;i++)
printf("frequency[%d]= %2d\n",i,frequency[ i ]);
/* print result */
printHistogram(frequency);
printf("\n");
printf("\nMode is %d and appeared %d times", findFrequentIndex(frequency), findFrequentValue(frequency) );
}
/* Returns the index of most frequent one in frequency array*/
int findFrequentIndex(int b[]){
int mostFrequent=b[0],i;
int rating=0;
for(i=1;i<10;i++){
if(b[i]>mostFrequent){
mostFrequent=b[i];
rating=i; //keep index of mostFrequent one!
}
}
return rating;
}
/* Returns the value of most frequent occurrence in frequency array*/
int findFrequentValue(int b[]){
int mostFrequent=b[0],i;
for(i=0;i<10;i++){
if(b[i+1]>mostFrequent) //Achtung !!!!!
mostFrequent=b[i+1];
}
return mostFrequent;
}
/*prints the histogram*/
void printHistogram(int b[]){
int i,j;
for(i=0;i<10;i++){
printf("\nfrequency[%d] ",i);
for(j=1;j<=b[i];j++){
printf("*");
}
}
}
/*Sort function*/
void bubbleSort(int b[]) { //Passed by reference
int passes=SIZE-1;
int i,j,temp;
for(i=1;i<=passes;i++){
for(j=0;j<SIZE-1;j++){
if(b[j]>b[j+1]){
temp=b[j+1];
b[j+1]=b[j];
b[j]=temp;
}
}
}
}
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(30) : 警告 C4024:
'fillResponseArray' : 形参和实参的不同类型
1 1>c:\ users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(32):错误 C2065:
“响应”:未声明的标识符 1>c:\users\turan\documents\visual
studio 2008\projects \project1\search\search\dsa.c(32) : 警告 C4047:
'function' : 'int *' 与 'int'
1>c:\users\turan\documents\visual studio
2008\projects的间接级别不同\project1\search\search\dsa.c(32) : 警告 C4024: 'mean'
: 形式参数和实际参数 1 的不同类型
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(33):错误 C2065:
“响应”:未声明的标识符 1>c:\users\turan\documents\ visual
studio 2008\projects\project1\search\search\dsa.c(33) : 警告 C4047:
'function' : 'int *' 与 'int'
1>c:\users\turan\documents\的间接级别不同visual studio
2008\projects\project1\search\search\dsa.c(33) : 警告 C4024:
'median' : 形式参数和实际参数 1
1>c:\users\turan\documents\visual studio
2008\projects的不同类型\project1\search\search\dsa.c(34):错误 C2065:
“响应”:未声明的标识符 1>c:\users\turan\documents\visual
studio 2008\projects\project1\search\search\dsa.c(34) : 警告 C4047:
'function' : 'int *' 与 'int'
1>c:\users\turan\documents\visual的间接级别不同studio
2008\projects\project1\search\search\dsa.c(34) : 警告 C4024: 'mode'
: 形参和实参的不同类型 1 1>构建日志
保存在“file://c:\Users\ Turan\Documents\Visual Studio
2008\Projects\Project1\Search\Search\Debug\BuildLog.htm" 1>搜索 - 5 个
错误,9 个警告
========== 构建:0成功,1 失败,0 最新,0 跳过 ==========