#include <iostream>
#include <iomanip>
using namespace std;
void mean (const int [],int);
void median (int [],int);
void mode (int [],int [],int);
void bubbleSort (int [],int);
void printArray(const int[],int);
int main()
{
const int responseSize=99;
int frequency[10]={0};
int response[responseSize]=
{6,7,8,9,8,7,8,9,8,9,
6,7,8,9,3,9,8,7,8,7,
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,responseSize);
median(response,responseSize);
mode(frequency,response,responseSize);
return 0;
}
void mean(const int answer[],int arraySize)
{
int total=0;
cout<<"********/n mean/n********/n";
for(int i=0;i<arraySize;i++)
total+=answer[i];
cout<<fixed<<setprecision(4);
cout<<"元素个数是:"<<arraySize<<endl;
cout<<"元素总和是:"<<total<<endl;
cout<<"平均值是:"<<total/arraySize<<endl;
}
void median(int answer[],int size)
{
cout<<"/n********/n Median/n********/n"
<<"未排序的数组是";
printArray(answer,size);
bubbleSort(answer,size);
cout<<"/n/n排序后的数组是:";
printArray(answer,size);
cout<<"/n/n在这"<<size<<"个元素中,中位数是第"<<size/2<<"个,值是:"<<answer[size/2];
}
void mode(int freq[],int answer[],int size)
{
int largest=0;
int modeValue=0;
cout<<"/n********/n Mode/n********/n";
for(int i=1;i<10;i++)
freq[i]=0;
for(int j=0;j<size;j++)
++freq[answer[j]];
cout<<"Response"<<setw(11)<<"Frequency"<<setw(19)
<<"Histogram/n/n"<<setw(55)<<"1 1 2 2/n"
<<setw(56)<<"5 0 5 0 5/n/n";
for(int rating=1;rating<10;rating++){
cout<<setw(9)<<rating<<setw(11)<<freq[rating]<<" ";
if(freq[rating]>largest){
largest=freq[rating];
modeValue=rating;}
for(int k=1;k<=freq[rating];k++)
cout<<"*";
cout<<"/n";
}
cout<<"数据处理直方图"<<endl;
cout<<"这组数据中,出现次数最多的是:"<<modeValue<<endl;
cout<<"一共出现了"<<largest<<"次"<<endl;
}
void bubbleSort(int a[],int size)
{
int hold;
for(int pass=1;pass<size;pass++){
for(int j=0;j<size;j++){
if(a[j]>a[j+1]){
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
}
}
void printArray(const int a[],int size)
{
for(int i=0;i<size;i++){
if(i%20==0)
cout<<endl;
cout<<setw(2)<<a[i];
}
}