Perkalian Matriks Bahasa C (menggunakan variable)


Contoh perkalian matriks dengan jumlah baris dan jumlah kolom matriks dibuat variable (diinput).

/*
perkalian matriks A dengan B ditampung di matriks C, kemudian
matriks C ditambahkan dengan matriks D ditampung di matriks E
*/
#include<stdio.h>
main()
{

int i,j,k, nilai_i, nilai_j, nilai_k;
int matrik_A[10][10], matrik_B[10][10], matrik_C[10][10], matrik_D[10][10], matrik_E[10][10];

clrscr();
printf(“\n”);
printf(“Masukkan Jumlah Baris dan Kolom matrik\n”);
printf(“Jml Baris Matriks A : “); scanf(“%d”, &nilai_i);
printf(“Jml Kolom Matriks A : “); scanf(“%d”, &nilai_j);
printf(“Jml Baris Matriks B : “); scanf(“%d”, &nilai_j);
printf(“Jml Kolom Matriks B : “); scanf(“%d”, &nilai_k);

printf(“\n”);
printf(“—————————\n”);
printf(“Input Nilai Matrik A\n”);
printf(“—————————\n”);
for(i=0;i<nilai_i;i++)
{
for(j=0;j<nilai_j;j++)
{
printf(“Baris ke-%d, kolom ke-%d = “,i+1,j+1);
scanf(“%d”,&matrik_A[i][j]);
}
}
printf(“\n”);
printf(“—————————\n”);
printf(“Input Nilai Matrik B\n”);
printf(“—————————\n”);
for(j=0;j<nilai_j;j++)
{
for(k=0;k<nilai_k;k++)
{
printf(“Baris ke-%d, kolom ke-%d = “,j+1,k+1);
scanf(“%d”,&matrik_B[j][k]);
}
}

printf(“\n”);
printf(“—————————\n”);
printf(“Input Nilai Matrik D\n”);
printf(“—————————\n”);
for(i=0;i<nilai_i;i++)
{
for(k=0;k<nilai_k;k++)
{
printf(“Baris ke-%d, kolom ke-%d =”,i+1,k+1);
scanf(“%d”,&matrik_D[i][k]);
}
}

printf(“\n”);
for(i=0;i<nilai_i;i++)
{
for(k=0;k<nilai_k;k++)
{
matrik_C[i][k] = 0;
for(j=0;j<nilai_j;j++)
matrik_C[i][k]+=matrik_A[i][j]*matrik_B[j][k];
matrik_E[i][k]=matrik_C[i][k]+matrik_D[i][k];
}
}
printf(“————————————-\n”);
printf(“Nilai Matrik C = A x B adalah \n”);
printf(“————————————-\n”);
for(i=0;i<nilai_i;i++)
{
for(k=0;k<nilai_k;k++)
printf(“%7d”,matrik_C[i][k]);
printf(“\n”);
}
printf(“\n”);
printf(“————————————-\n”);
printf(“Nilai matrik E = C + D adalah \n”);
printf(“————————————-\n”);
for(i=0;i<nilai_i;i++)
{
for(k=0;k<nilai_k;k++)
printf(“%7d”,matrik_E[i][k]);
printf(“\n”);
}
getch();
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s