Kali ini saya akan memposting program pada rencana kuliah bulan 3 latihan 2, kali ini saya menggunakan bahasa c++ dan berikut adalah kodingannya:
#define SIZE 100
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main(int argc, char** argv) {
int i,n,choice;
float array[SIZE],mean,median,mode;
char a;
a='b';
cout<<"===========================";
cout<<"\nPROGRAM MEAN, MEDIAN, MODUS\n";
cout<<"===========================\n";
cout<<"Masukan jumlah data = ";
cin>>n;
cout<<"=======================\n";
cout<<"data\n";
for(i=0; i<n; i++)
cin>>array[i];
mean=mean_function(array,n);
cout<<"\nMean ="<<mean;
median=median_function(array,n);
cout<<"\nMedian = "<<median;
mode=mode_function(array,n);
cout<<"\nModus = "<<mode;
}
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));
}
Dan berikut adalah hasil setelah di compile..