#define SIZE 100#include<iostream>#include<windows.h>using namespace std;float mean_function(float[],int);float median_function(float[],int);float mode_function(float[],int);
int main(){
int i,n,choice;
float array[SIZE],mean,median,mode;char a; a='b'; while(a=='b') {
cout<<"===========================";cout<<"\nPROGRAM MEAN, MEDIAN, MODUS\n";cout<<"===========================\n";cout<<"Masukan jumlah data = ";cin>>n;cout<<"=======================\n";cout<<"data\n";cout<<"=======================\n";for(i=0; i<n; i++)
cin>>array[i];
do{
cout<<"\n\tEnter pilihan\n\t1.Mean\n\t2.Median\n\t3.Mode\n\t4.Exit\n";cin>>choice;switch(choice){
case 1:
mean=mean_function(array,n);cout<<"\nMean ="<<mean;break;
case 2:
median=median_function(array,n);cout<<"\n\tMedian = "<<median;break;
case 3:
mode=mode_function(array,n);cout<<"\n\tMode = "<<mode;break;
case 4:
break;
default:
cout<<"salah kode";break;
}}while(choice!=4);
}}
float mean_function(float array[],int n){int i;float sum=0;for(i=0;i<n;i++)
sum=sum+array[i];
return (sum/n);
}
float median_function(float a[],int n){
float temp;int i,j;for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[j];a[j]=a[i];a[i]=temp;
}
}
if(n%2==0)return (a[n/2]+a[n/2-1])/2;elsereturn a[n/2];
}
float mode_function(float a[],int n){
return (3*median_function(a,n)-2*mean_function(a,n));
}
Next
« Prev Post
« Prev Post
Previous
Next Post »
Next Post »
Langganan:
Posting Komentar (Atom)