1. Identifikasi masalah
=> mencari mean, modus dan median.
2. Menentukan input dan output
mean : for(i=0;i<n;i++)
sum=sum+array[i];
median : for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
modus : 3*median_function(a,n)-2*mean_function(a,n)
3. Membuat Flowchart
4. Test menggunakan data
Median :
1 1 2 2 2 3 4 5 5
f[1]=2
f[2]=3
f[3]=1
f[4]=1
f[5]=2
5. Implementasi C++
source code :
#define SIZE 100
#include<iostream>
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')
{
system ("cls");
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;
else
return a[n/2];
}
float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}
6. Execusi
untuk rencana silahkan kunjungi latian no :
Tidak ada komentar:
Posting Komentar