2.Latihan : mencari mean, median,
modus, max, min dari banyak nilai
yang di inputkan
SUBPROGRAM C++:
#define SIZE 100
#include<iostream>
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
using namespace std;
void masuk(int data[100]){
int n,max,min,x;
max=data[1];
min=data[1];
for(int i=0;i<n;i++){
if(data[i]<min){
min=data[i];
}
else if(data[i]>max){
max=data[i];
}
}
cout<<"nilai maksimum="<<max<<endl;
cout<<"nilai minimum="<<min<<endl;
}
int main()
{
int i,y,choice;
int data[100],n,max,min,x;
cout<<"banyak data =";cin>>n;
for(int i=0;i<n;i++){
cout<<"banyak masukkan =";cin>>data[i];
}
masuk(data);
float array[SIZE],mean,median,mode;
char a;
a='b';
while(a=='b')
{
system ("cls");
cout<<"===========================";
cout<<"\nPROGRAM MEAN, MEDIAN, MODUS\n";
cout<<"===========================\n";
cout<<"Masukan jumlah data = ";
cin>>y;
cout<<"=======================\n";
cout<<"data\n";
cout<<"=======================\n";
for(i=0; i<y; 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,y);
cout<<"\nMean ="<<mean;
break;
case 2:
median=median_function(array,y);
cout<<"\n\tMedian = "<<median;
break;
case 3:
mode=mode_function(array,y);
cout<<"\n\tMode = "<<mode;
break;
case 4:
break;
default:
cout<<"salah kode";
break;
}
}while(choice!=4);
}
}
float mean_function(float array[],int y)
{
int i;
float sum=0;
for(i=0;i<y;i++)
sum=sum+array[i];
return (sum/y);
}
float median_function(float a[],int y)
{
float temp;
int i,j;
for(i=0;i<y;i++)
for(j=i+1;j<y;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(y%2==0)
return (a[y/2]+a[y/2-1])/2;
else
return a[y/2];
}
float mode_function(float a[],int y)
{
return (3*median_function(a,y)-2*mean_function(a,y));
}
untuk ke no 3 silahkan klik double pada link berikut
no 3
no 1
0 komentar:
Posting Komentar