Jumat, 24 Juni 2011

Program C++ Perkalian Dua Buah Matriks


Once we learn about the sum of two matrices then we will learn to create two matrix multiplication program. Hopefully with this we can know things that were not previously known and useful for all my friends. Okeee,, just algorithms and programs clay. Check it out .... :)

Algoritma :
Procedure kali_matriks(input matriks1,matriks2 : matriks; baris, kolom, barkol :integer; output mat_kali : matriks)
Deklarasi
i, j, k             : integer

Deskripsi
for i <= 1 to baris do
for j <= 1 to kolom do
mat_kali[i,j] ß 0
for k <= 1 to barkol do
mat_kali[i,j] ß mat_kali[i,j] + matriks1[i,k]*matriks2[k,j]
endfor
endfor
endfor

Berikut kode programnya :

#include <iostream.h>

void baca_matriks(int mat[10][10], int baris, int kolom)
{  int i,j;
for (i=0; i<baris; i++)
for (j=0; j<kolom; j++)
{ cout<<"Data ["<<i+1<<","<<j+1<<"] :";
  cin>>mat[i][j];
}
}

void kali_matriks(const int matriks1[10][10], const int matriks2[10][10], int baris, int kolom, int barkol, int mat_kali[10][10])
{  int i,j,k;
for (i=0; i<baris; i++)
for (j=0; j<kolom; j++)
{ mat_kali[i][j]=0;
for (k=0; k<barkol; k++)
  mat_kali[i][j] = mat_kali[i][j]+matriks1[i][k]*matriks1[k][j];
}
}
void cetak_matriks(const int A[10][10], int baris, int kolom)
{  int i,j;
for (i=0; i<baris; i++)
{ for (j=0; j<kolom; j++)
cout<<A[i][j]
cout<<endl;
}
}

void main(){
int m,n,p;
int matriks1[10][10], matriks2[10][10];
int hasil[10][10];
cout<<"Baris matriks ke-1 : ";
cin>>m;
cout<<"Kolom matriks ke-2 : ";
cin>>n;
cout<<"Data matriks ke-1\n";
baca_matriks(matriks1,m,p);
cetak_matriks(matriks1,m,p);
cout<<"Data matriks ke-2\n";
baca_matriks(matriks2,p,n);
cetak_matriks(matriks2,p,n);
kali_matriks(matriks1,matriks2,m,n,p,hasil);
cout<<"Hasil perkalian : \n";
cetak_matriks(hasil,m,n);
}


explanation:
The above program is the program for multiplying two matrices that provided the two matrices can be multiplied is the first matrix column size must be equal to the size of the second matrix row. When Amxp and Bpxn, will produce Cmxn. Suppose the matrix A 2 x 3 matrix and B 3 x 1 and will produce a matrix C 1 x 2. Suppose also Am Bp xp and xn, then m = 2, p = 3, and n = 1.A2x3, B3x1, C2x1
The matrix C is obtained by:

C11 = a11 * b11 + a12 * B21 + A13 * B31
when:
i runs from 1 to m (multiple lines)
k runs from 1 to p (a lot of elements in the multiplication)
j runs from 1 to n (lots of columns)
Then obtained three loops:
  for i <= 1 to rows do
          for j <= 1 to the columns do
               mat_kali [i, j]
ß 0
               for k <= 1 to barkol do
mat_kali [i, j] expressed the matrix element to the result in (i, j). Therefore, a cumulative sum of the starting value to 0 (and only influenced by the variables i and j).
 

0 komentar:

Posting Komentar

Template by:

Free Blog Templates