latest Post

The program calculates the median value, mode, and mean with validation using C ++



Buatlah program statistika untuk menghitung nilai median, modus dan mean dari n data yang diinputkan dengan menggunakan pointer !

#include<iostream>
#include <conio.h>
#include <cstdlib>
#include <cstring>
using namespace std;

float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);

int kata;
char val [100];

int validasi()
{
    do
    {
        kata=0;
        cin>>val;
        for(int i=0; i<strlen(val); i++)
        {
            if(isdigit(val[i])==0)
                kata++;
        }
        if(kata !=0)
            cout<<"Input salah !\n"<<endl;

    }
 while(kata!=0);
    return 0;
}


int main(){
int i,n,choice;
float *data;
float modus;
mulai :
cout<<"\t\t===========================";
cout<<"\n\t\tPROGRAM MEAN, MEDIAN, MODUS\n";
cout<<"\t\t===========================\n\n";
cout<<"Masukan jumlah data = ";
validasi(); n=atoi(val);
data = new float[n];
cout<<endl;
for(i=0; i<n; i++){
 cout<<"Masukkan data ke- "<<i+1<<" : ";
 validasi(); data[i]=atoi(val);
}
awal :
 cout<<"\nData yang anda masukkan :";
 for(i=0; i<n; i++){
  cout<<"\nData ke- "<<i+1<<" : "<<data[i];
 } 
 cout<<"\n\n\tMenu Program\n\t1.Mean\n\t2.Median\n\t3.Modus\n\t4.Input Data\n\t5.Exit\n\t";
 cout<<"\n\n\t\tMasukkan Pilihan : ";
 validasi(); choice=atoi(val);
 switch(choice){

 case 1:
 cout<<"\n\tMean   = "<<mean_function(data,n);
 break;

 case 2:
 cout<<"\n\tMedian = "<<median_function(data,n);
 break;
 
 case 3:{
 modus=mode_function(data,n);
 if (modus!=0)
  cout<<"\n\tModus  = "<<modus;
 else
  cout<<"Tidak ada data modus";
 break;
 } 
 
 case 4:{
  system("cls");
  goto mulai;
  break;
 }
 break;
 
 case 5:{
  cout<<"Bye..";
  return 0;
 }
 break;
 
 default:
 cout<<"Pilihan tidak tersedia !";
 break;
 }
 
 getch();
 int pilihan;
 cout << "\n\nApakah anda ingin mencoba lagi? jika ya tekan <1>, tidak tekan <2>." << endl;
 cout << "Pilihan anda : ";
 validasi(); pilihan=atoi(val);
 cout << endl;
 if(pilihan==1) {
  system("CLS");
  goto awal;
 }
 else
  cout << "\nTerima Kasih Telah Menggunakan Program ini." << endl;
}


float mean_function(float *data,int n){
int i;
float sum=0;
for(i=0;i<n;i++)

sum=sum+data[i];

return (sum/n);

}


float median_function(float *data,int n){

float temp;
int i,j;
for(i=0;i<n;i++)

for(j=i+1;j<n;j++){

 if(data[i]>data[j]){
 
 temp=data[j];
 data[j]=data[i];
 data[i]=temp;
 }
}

if(n%2==0)
return (data[n/2]+data[n/2-1])/2;
else
return data[n/2];
}

float mode_function(float *a,int n){
 double modus; 
 int Byk_sementara=1; 
 int y; 
 int byk[100]; 
 for(int i=0;i<10;i++) { 
  byk[i]=0; 
  for(int j=0;j<10;j++) { 
   if(a[i]==a[j]) { 
    byk[i]++; 
   } 
  } 
 } 
 for(int i=0;i<10;i++) { 
  if(byk[i]>Byk_sementara) { 
   Byk_sementara=byk[i]; 
   y=i; 
  } 
 } 
 modus=a[y]; 
 return modus;   
}


About Unknown

Unknown
Recommended Posts × +

0 comments:

Post a Comment