contoh aplikasi array 2 dimensi sedikit contoh aplikasi array 2 dimensi
Contoh-1 Magic Square (bujur sangkar ajaib)
8 1 6 3 5 7 4 9 2 Magic square 15 15 15 15 15 15 Square Matrix 3x3 n = 3 15 15 8 1 6 3 5 7 4 9 2 15 15 15 15
8 1 6 3 5 7 4 9 2 Magic square 15 15 15 15 15 15 Square Matrix 3x3 n = 3 15 8 1 6 ( n3 + n ) / 2 = 3 5 7 ( 33 + 3 ) / 2 = ( 27 + 3 ) / 2 = 15 4 9 2 15 15 15 15 (nxn/2) x (nxn +1) / n = (3x3/2) x (3x3 + 1) / 3 = ( 4.5 ) x ( 10 ) / 3 = 15
Magic square Square Matrix 5x5 65 n = 5 ( n3 + n ) / 2 = ( 53 + 5 ) / 2 = ( 125 + 5 ) / 2 = 65 65 65 65 65 65 65 (nxn/2) x (nxn +1) / n = (5x5/2) x (5x5 + 1) / 5 = ( 12.5 ) x ( 26 ) / 5 = 65
Magic square Square Matrix 11x11 n = 11 ( n3 + n ) / 2 = 671 Square Matrix 11x11 n = 11 ( n3 + n ) / 2 = ( 113 + 11 ) / 2 = ( 1331 + 11 ) / 2 = 671 671
#include<stdio.h> void main() { int A[21][21]={0}; int n,i,j,r,c,key; printf("input n, positip, ganjil, minimum 3, maksimum 11\n"); scanf("%i", &n); key=1;i=0; j=(n-1)/2; A[i][j]=key; key++; while(key<=n*n) { r=(i+n-1)%n; c=(j+n-1)%n; if(A[r][c] != 0 ) { i=(i+1)%n; } else { i=r; j=c; } A[i][j]=key; key++; } for(i=0;i<=n-1;i++) { for(j=0;j<=n-1;j++) { printf("%4i",A[i][j]); } printf("\n\n");
Magic square Square Matrix 11x11 n = 11
eliminasi Gauss Jordan
Diketahui Sistem Persamaan Linear sebagai berikut : Susun algoritma untuk menghitung nilai : X1 = …… X2 = …… X3 = …… X4 = ……
Hasil yang tercetak oleh program :
Periksa : 2x3 + 3x2 + 1x5 + 3x2 = 6 + 6 + 5 + 6 = 12 + 11 = 23 6 + 6 + 5 + 6 = 12 + 11 = 23 3x3 + 2x2 + 2x5 + 2x2 = 9 + 4 + 10 + 4 = 13 + 14 = 27
Program : //gaus-01.cpp #include<stdio.h> float A[4][5] = {2,3,1,3,23, 3,2,2,2,27, 1,3,3,2,28, 4,5,2,3,38}; float B[5]; int i,j,k; float x,y; void CETAK() { for(i=0; i<=3; i++ ) { printf("\n X%i = %2.1f", i+1, A[i][4] ); } void main() { for(j=0; j<=2; j++) {for(i=j+1; i<=3; i++ ) { x=A[i][j]; y=A[j][j]; for(k=0; k<=4; k++) { A[i][k] = y*A[i][k] - x*A[j][k]; } A[3][4]=A[3][4] / A[3][3]; A[3][3]=1; for(j=3; j>=1; j--) {for(i=j-1; i>=0; i-- ) { A[i][k] = y*A[i][k] - x*A[j][k]; for(i=0;i<=3;i++) { A[i][4]=A[i][4]/A[i][i]; A[i][i] = 1; } CETAK();
void main() { for(j=0; j<=2; j++) {for(i=j+1; i<=3; i++ ) { x=A[i][j]; y=A[j][j]; for(k=0; k<=4; k++) { A[i][k] = y*A[i][k] - x*A[j][k];} } A[3][4]=A[3][4] / A[3][3]; A[3][3]=1; for(j=3; j>=1; j--) {for(i=j-1; i>=0; i-- ) { A[i][k] = y*A[i][k] - x*A[j][k]; for(i=0;i<=3;i++) { A[i][4]=A[i][4]/A[i][i]; A[i][i] = 1; } CETAK();
void main() { for(j=0; j<=2; j++) {for(i=j+1; i<=3; i++ ) { x=A[i][j]; y=A[j][j]; for(k=0; k<=4; k++) { A[i][k] = y*A[i][k] - x*A[j][k];} } A[3][4]=A[3][4] / A[3][3]; A[3][3]=1; for(j=3; j>=1; j--) {for(i=j-1; i>=0; i-- ) { A[i][k] = y*A[i][k] - x*A[j][k]; for(i=0;i<=3;i++) { A[i][4]=A[i][4]/A[i][i]; A[i][i] = 1; } CETAK();
Program : for(j=3; j>=1; j--) {for(i=j-1; i>=0; i-- ) { x=A[i][j]; y=A[j][j]; for(k=0; k<=4; k++) { A[i][k] = y*A[i][k] - x*A[j][k]; } for(i=0;i<=3;i++) { A[i][4]=A[i][4]/A[i][i]; A[i][i] = 1; } CETAK();
#include<stdio> #include<conio.h> void main() { int A[3][5]; //menyiapkan array int I,J, N; clrscr(); // mengisi array // mencetak isi array // agar dapat dilihat apakah // mengisi array sudah benar getch(); }
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N; clrscr(); // mengisi array // mencetak isi array for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; } getch(); 0 1 2 3 4 1 2 3 4 5 1 2 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N; clrscr(); // mengisi array N = 1; for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { xxxxxxxxx; xxxxxxxxx; } // mencetak isi array { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 1 2 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N; clrscr(); // mengisi array N = 1; for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { A[I][J] = N; N = N+1; } // mencetak isi array { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 1 2 6 7 8 9 10 11 12 13 14 15
tidak menggunakan variabel N
? #include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J; clrscr(); // mengisi array N = 1; for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { A[I][J] =. . . . .; } // mencetak isi array { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 1 2 6 7 8 9 10 11 12 13 14 15 ?
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J; clrscr(); // mengisi array N = 1; for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { A[I][J] = I*5 + J+1; } // mencetak isi array { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 1 2 6 7 8 9 10 11 12 13 14 15
diisi kolom per kolom
{ for(I=0; I<=2; I=I+1) { } 0 1 2 3 4 1 2 3 4 5 diisi baris per baris 1 2 6 7 8 9 10 11 12 13 14 15 for(J=0; J<=4; J=J+1) { for(I=0; I<=2; I=I+1) { } J --> loop luar I --> loop dalam
#include<stdio> #include<conio.h> void main() { int A[3][5]; //menyiapkan array int I,J, N, K; clrscr(); // mengisi array // mencetak isi array // agar dapat dilihat apakah // mengisi array sudah benar getch(); } 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N, K; clrscr(); // mengisi array // mencetak isi array for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; } getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N,K; clrscr(); // mengisi array N = 1; for(J=0; J<=4; J=J+1) { K=N; for(I=0; I<=2; I=I+1) { xxxxxxxxx; xxxxxxxxx; } N=N+1; // mencetak isi array { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N,K; clrscr(); // mengisi array N = 1; for(J=0; J<=4; J=J+1) { K=N; for(I=0; I<=2; I=I+1) { A[I,J] = K; K = K + . . . : } N=N+1; // mencetak isi array { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J, N,K; clrscr(); // mengisi array N = 1; for(J=0; J<=4; J=J+1) { K=N; for(I=0; I<=2; I=I+1) { A[I,J] = K; K = K + 5; } N=N+1; // mencetak isi array { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
tidak menggunakan variabel N
? #include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J; clrscr(); // mengisi array for(J=0; J<=4; J=J+1) { for(I=0; I<=2; I=I+1) { A[I,J] = . . . . . ; } // mencetak isi array for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ?
#include<stdio> #include<conio.h> void main() { int A[3][5]; int I,J; clrscr(); // mengisi array for(J=0; J<=4; J=J+1) { for(I=0; I<=2; I=I+1) { A[I,J] = I*5 + J+1 ; } // mencetak isi array for(I=0; I<=2; I=I+1) { for(J=0; J<=4; J=J+1) { printf(“%4i”, A[I][J]; getch(); 0 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
SOAL-2
Soal-2. Sudah ada array Dua Dimensi yang dibuat dengan int A[3][5], belum ada isinya . Susun program (penggalan program) untuk mengisi array tersebut sehingga isinya menjadi sebagai berikut : 0 1 2 3 4 1 4 7 10 13 1 2 2 5 8 11 14 3 6 9 12 15
diisi baris per baris atau kolom per kolom ?