Program
mencari maksimum, minimum, mean dan median dari data dalam array dimensi satu,
syarat data array diurutkan terlebih dahulu.
Listing Program
:
/*Mencari
mean, median, maksimal dan minimal dengan randomize*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void
main()
{
int nilai[50];
int i,j, med, jml, tot=0, tamp;
randomize();
//input
cout<<"Jumlah data: ";
cin>>jml;
for (int i=0; i<jml; i++)
{
nilai [i]= random (10);
tot=tot+nilai[i];
}
for (int i=0; i<jml; i++)
cout<<nilai[i]<<"
";
for (int i=0; i<jml-1; i++)
//sorting
for (int j=i+1; j<jml; j++)
if(nilai[i]>nilai[j])
{
tamp = nilai[i];
nilai[i] = nilai[j];
nilai[j] = tamp;
}
cout<<"\n\nData setelah
diurutkan: "<<endl;
for(int i=0; i<jml; i++)
cout<<nilai[i]<<"
";
cout<<"\n\nNilai rata-rata:
"<<tot<<"/"<<jml<<"="<<tot/jml<<endl;
cout<<"\nNilai Maksimal :
"<<nilai[jml-1]<<endl;
cout<<"\nNilai Minimal : "<<nilai[0]<<endl;
if(jml%2==0)
cout<<"\nMedian
: "<<(nilai[jml/2]+nilai[jml/2-1])/2;
else
cout<<"\nMedian
: "<<(nilai[jml/2]);
getch();
}
Tidak ada komentar:
Posting Komentar