Pemrograman C++ Menentukan Modus silahkan di praktikan
include
#include
#include
void
main()
{
inti,j,n,dt[10],dum,sisa,med;
cout<<"masukkanbanyaknya
data = ";
cin>>n;
for
(i=1;i<=n;i++)
{
cout<<"data["<
cin>>dt[i];
}
cout<
cout<<"urutan
data awal"<
for
(i=1;i<=n;i++)
{
cout<
}
cout<
for
(i=1;i<=n;i++)
{
for
(j=1;j<=i;j++)
{
if
(dt[i]
{
dum=dt[i];
dt[i]=dt[j];
dt[j]=dum;
}
}
}
cout<
cout<<"pengurutan
data"<
for
(i=1;i<=n;i++)
{
cout<
}
cout<
//median
sisa=n%2;
if
(sisa==0)
{
med=(dt[n/2]+dt[n/2+1])/2;//genap
}
else
{
med=dt[n/2+1];//gasal
}
cout<<"median
= "<
getch();
}
Javac.
importjava.util.Scanner;
classalloperation {
public static void main(String[] args) {
Scanner scan1=new Scanner(System.in);
System.out.print("masukanbanyaknilai yang diinput= ");
int x=scan1.nextInt();
System.out.println("1. menghitungnilai modus");
System.out.println("2. menghitungnilai min dan max");
System.out.println("3. menghitungnilai median");
System.out.println("4. menghitungnilai rata2");
System.out.print("masukanpiihan(1-4)=");intpilih=scan1.nextInt();
switch(pilih){
case 1:
int[]a=new int[x];
int c=a[0];
int b=0;
inthasil=0,besar=0;
for(int i=0;iSystem.out.print("masukannilaike-"+(i+1)+"=");a[i]=scan1.nextInt();}
//proses modus
System.out.println("\n");for (int i=0; iif(a[i]==c){System.out.print(a[i]+" ");System.out.println(++b);if(b>besar){besar=b;hasil=c;}
}
else{c=a[i];
b=1;
System.out.print(a[i]+" ");System.out.println(b);}
}System.out.println("Modus = "+hasil);break;case 2:int[]input=new int[x];//mencarinilai max dan min
for(int i=0;iSystem.out.print("masukannilaike-"+(i+1)+"=");input[i]=scan1.nextInt();}
int max=0;int min=input.length;for(int k=0;kif(maxmax=input[k];}
if(min>input[k]){min=input[k];}
}
System.out.println("nilai max = "+max);System.out.println("nilai min = "+min);break;case 3:int[]nilai=new int[x];//median
for(int i=0;i System.out.print("masukannilaike-"+(i+1)+"=");nilai[i]=scan1.nextInt();}
//proses median
float median=0.0f;intbil=nilai.length%2;if(bil==0){intposisi=x/2;System.out.println("bilangangenap");median=(nilai[posisi-1]+nilai[posisi])/2;System.out.printf("nilai median adalah %.2f",median);}else{intposisi=x/2;System.out.println("bilanganganjil");median=nilai[posisi];System.out.print("nilai median adalah "+median);}break;case 4:float[]za=new float[x];floatjumlah=0;for(int i=0;i System.out.print("masukannilaike-"+(i+1)+"=");za[i]=scan1.nextFloat();}
System.out.println("\ntampilannilai:");for(int k=0;k System.out.print(za[k]+" ;");jumlah+=za[k];}
//proses rata2
float rata2=jumlah/x;System.out.println("\nnilai rata2 = "+rata2);break;default:System.out.println("salahmemasukanpilihan");break;}}
}
/* StandarDeviasi */ 02 #include
03 #include 04
05 main() 06 {
07 intn,i; 08 floatJKR,jumlah,rata,KR,SD;
09 floatx[100]; 10
11 printf("Menghitungstandardeviasi\n"); 12 printf("Banyak data : ");
13 scanf("%d",&n); 14
15 jumlah=0; 16 JKR=0;
17 18 for(i=1;i
19 { 20 printf("Data ke-%d : ",i+1);
21 scanf("%d",&x[i]); 22 jumlah=jumlah + x[i];
23 } 24
25 rata=jumlah/n; 26
27 for(i=0;i 28 {
29 KR=pow(x[i]-rata),2); 30 JKR=JKR+KR;
31 } 32
33 SD=sqrt(JKR/n); 34 printf("Standardeviasi : %f \n",SD);
35 }
/* rata-rata bilanganpositif */ 02 #include
03 main() 04 {
05 intcacah,i,n; 06 floatjumlah,rata;
07 floatx[100]; 08
09 jumlah=0; 10 cacah=0;
11 printf("rata-rata bilanganpositif \n"); 12 printf("banyak data: ");
13 scanf("%d",&n); 14 for(i=0; i
15 { 16 printf("data ke-%d : ",i+1);
17 scanf("%f",&x[i]); 18 if(x[i]>0)
19 { 20 jumlajumlah + x[i];
21 cacah=cacah + 1; 22 }
23 } 24 rata=jumah/cacah;
25 printf("hasilpenjumlahan : %.2f \n",jumlah); 26 printf("rata-rata : %.2f \n",rata);
27 }Tampilan program:
- /*
- Judul Program : Penjumlahan Matriks Ordo 2X2
- Pemrogram : Hendro Pramana Sinaga
- Tanggal : 10 Mei 2012
- Medan, Sumatera Utara, Indonesia
- */
- #include
- #include
- using namespace std;
- main()
- {
- int matA[2][2], matB[2][2], matC[2][2];
- //pemberian data matriks A dimulai disini
- for(int i=0; i<2 i="" nbsp="" span="">2>
- {
- for(int h=0; h<2 h="" nbsp="" span="">2>
- {
- cout<<"Data Matriks A ["<
- cin>>matA[i][h];
- }
- } // pemberian data matriks A berakhir disini
- endl(cout);
- //pemberian data matriks B dimulai disini
- for(int i=0; i<2 i="" nbsp="" span="">2>
- {
- for(int h=0; h<2 h="" nbsp="" span="">2>
- {
- cout<<"Data Matriks B ["<
- cin>>matB[i][h];
- }
- } // pemberian data matriks B berakhir disini
- endl(cout);
- cout<<"Matriks A + Matriks B :"<
- //perulangan menampilkan penjumlahan matriks
- for(int i=0; i<2 i="" nbsp="" span="">2>
- {
- for(int h=0; h<2 h="" nbsp="" span="">2>
- {
- matC[i][h] = matA[i][h]+matB[i][h];
- cout<
- }
- cout<
- }
- system("pause");
- }
#include#include#includeusingnamespacestd;intmain() {intpilihan;intA[3][3], B[3][3], X[3][3], i, j, k;while(pilihan != 6) {cout<cout<< "-------------------------------\n";cout<< "Menu operasimatrik 3x3\n";cout<< "1. Input matrik\n";cout<< "2. Tampilmatrik\n";cout<< "3. Penjumlahanmatrik\n";cout<< "4. Penguranganmatrik\n";cout<< "5. Perkalianmatrik\n";cout<< "6. Keluar\n";cout<< "Input pilihan ?\n";cout<< "-------------------------------";cout<cin>>pilihan;if(pilihan == 1) {/******* Masukkanmatriks A *******/for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++) {cout<< "input data matrik A["<< i + 1 << "]["<< j + 1 << "] : ";cin>> A[i][j];}}/******** Masukkanmatriks B ********/for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++) {cout<< "input data matrik B["<< i + 1 << "]["<< j + 1 << "] : ";cin>> B[i][j];}}} elseif(pilihan == 2) {/******** Cetakisimatriks A ********/cout<< "\n matrik A\n";for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++)// printf("%6i", A[i][j]);cout<cout<}cout</******** Cetakisimatriks B *******/cout<< "\n matrik B\n";for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++)// printf("%6i", B[i][j]);cout<cout<}} elseif(pilihan == 3) {/******** Proses penjumlahanmatriks A dan B ********/for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++) {X[i][j] = A[i][j] + B[i][j];}}/******** Cetakhasilperkalianmatriks A dan B *******/cout<< "\n matrikPenjumlahan A+B\n";for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++)cout<cout<}} elseif(pilihan == 4) {/******** Proses penjumlahanmatriks A dan B ********/for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++) {X[i][j] = A[i][j] - B[i][j];}}/******** Cetakhasilperkalianmatriks A dan B *******/cout<< "\n matrikPengurangan A-B\n";for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++)cout<cout<}} elseif(pilihan == 5) {/******** Proses perkalianmatriks A dan B ********//******** Cetakhasilperkalianmatriks A dan B *******/cout<< "\n matrikPerkalianAxB\n";for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++) {X[i][j] = 0;for(k = 0; k < 3; k++) {X[i][j] += A[i][k] * B[k][j];}}}for(i = 0; i < 3; i++) {for(j = 0; j < 3; j++)cout<cout<}}}cout<return0;}[ Pemrograman C++ ] Menentukan ModusKali inisayainginmencobamembuatkan program untukmenentukan modus darisederetanbilangan yang diinput, sayajugamenuliskankegunaandaritiaptahapan, sehinggabisamemudahkanpembaca, silahkandicobadansemogabermanfaat#include#include int x; void cari_modus(float bil[], int n, float modus[]) { intbanyak[100]; int k=1; x=0; //untukmengurutkansecara ascending for(int c=0;c { for(int i=(n-1);i>=0;i--) { if(bil[i] { int temp; temp=bil[i]; bil[i]=bil[i-1]; bil[i-1]=temp; } } } //menghitungberapa kali muncultiapangka for(int c=0;c { banyak[c]=0; for(int i=0;i { if(bil[c]==bil[i]) { banyak[c]++; } } } //menentukannilai yang paling seringmuncul for(int c=0;c { if(banyak[c]>k) { k=banyak[c]; } } //jika modus lebihdarisatu for(int c=0;c { if(x==0) modus[x]=0; else modus[x]=modus[x-1]; if(banyak[c]==k) { if(bil[c]!=modus[x]) { modus[x]=bil[c]; x++; } } } //JikaSemuaangkamunculsamabanyak int z=0; for(int c=0;c { if(banyak[c]==k) { z++; } } if(z==n) { x=0; } } void main() { int n; float bil[100]; float modus[100]; cout<<"Banyak N : ";cin>>n; for(int c=0;c { cout<<"Nilai "<<(c+1)<<" : ";cin>>bil[c]; } cout< cari_modus(bil,n,modus); if (x==0) cout<<"Tidak Ada Modus!"< else { cout<<"Modus : "; for(int c=0;c { cout< } } getch(); }
No comments:
Post a Comment