C++ Multiplication of Two Multidimensional Matrix (CMTMM)

The following is an example of a C++ program that calculates the multiplication of two multidimensional matrices, the condition for the multiplication is that the number of rows in the second matrix must be the same as the number of columns in the first matrix.

output:

code:

#include <iostream.h>
#include <conio.h>

/**
*bundet.com
*Perkalian dua matrix
*/

void main()
{
 int a[20][20],b[20][20],c[20][20];
 int brsa, klmabrsb, klmb;

 cout<<"Baris matrik a : ";
 cin>>brsa;
 cout<<"Kolom matrik a Baris b : ";
 cin>>klmabrsb;
 cout<<"Kolom matrik b : ";
 cin>>klmb;

 //input matrik A
 cout<<"Masukkan matrik A"<<endl;
 for(int i=0;i<brsa;i++)
 for(int j=0;j<klmabrsb;j++)
 {
  cout<<"matrik a["<<i<<"]["<<j<<"] : ";
  cin>>a[i][j];
 }

 //input matrik B
 cout<<"Masukkan matrik B"<<endl;
 for(int i=0;i<klmabrsb;i++)
 for(int j=0;j<klmb;j++)
 {
  cout<<"matrik b["<<i<<"]["<<j<<"] : ";
  cin>>b[i][j];
 }

 //perkalian dua matrik
 for(int i=0;i<brsa;i++)
 for(int j=0;j<klmb;j++)
 {
  c[i][j]=0;
  for(int k=0;k<klmabrsb;k++)
  c[i][j]=c[i][j]+a[i][k]*b[k][j];
 }
 clrscr();

 //output matrik A
 cout<<"isi matrik A"<<endl;
 for(int i=0;i<brsa;i++)
 {
 for(int j=0;j<klmabrsb;j++)
  cout<<a[i][j]<<"  ";
  cout<<endl;
 }

 //output matrik B
 cout<<"isi matrik B"<<endl;
 for(int i=0;i<klmabrsb;i++)
 {
 for(int j=0;j<klmb;j++)
  cout<<b[i][j]<<"  ";
  cout<<endl;
 }

 //output matrik C
 cout<<"isi matrik C"<<endl;
 for(int i=0;i<brsa;i++)
 {
 for(int j=0;j<klmb;j++)
  cout<<c[i][j]<<"  ";
  cout<<endl;
 }
getch();
}

Hope this is useful & happy learning!

Question 1

IKA YUNIDA ANGGRAINI Oct 6, 2015, 12:33:00 Bro, in the matrix multiplication section, what is 'K'? Why did it suddenly appear there?

VENGEANCE TAMAKA Nov 25, 2015, 23:40:00 Hi sis. What do you mean by dr.

c[i][j]=c[i][j]+a[i][k]*b[k][j];
Cij=cij+a......

How do you explain Cij?

UCCHI ADIEL 29 Nov 2015, 13:00:00 Bro, there is an error in the input section of the B matrix if (i=0;i\
DWITIYA PRADIPTA ALWANI 8 Dec 2016, 00:07:00 Bro, how do I input variables?

ANDI MAYANG RIANI 4 Jan 2017, 11:41:00 Sis, what numbers were entered in one, why did the program produce such results?

RICKY GURNING Jun 7, 2017, 11:34:00 Bro.. have the c++ program:

> Deret taylor
> cari akar persamaan pangkat 4
> matrix 4x4

if there is, please help, bro..  rickygurning6@gmail.com  thanks

Response 1

Hello Ika, YES, for 'k' in the program above, it is a parameter/argument for looping 2 matrices a and b, in which the initialization of 'k' is given a value of 0, so it starts from 0, so that when a condition occurs where k...

meaning: it multiplies 2 matrices, because the concept of multiplying 2 matrices is, for example, matrix a and b, then the rows of matrix ax are the columns of matrix b, therefore the multiplication of 2 matrices has a condition, namely the number of rows of matrix a == the number of columns of matrix b.

that's why 1 variable "klmabrsb" is used by 2 matrices

c[i][j]=c[i][j]+a[i][k]*b[k][j];

meaning; Note!: the concept of arithmetic in programming is not the same as the study of mathematics which recognizes process priorities (for example a+bxc, then what must be prioritized is bxc then added to a), while computers do not recognize that, so:

  1. c[i][j]+a[i][k] (add the contents of matrix c with the contents of matrix a, the result is stored in matrix c)
  2. c[i][k]*b[k][j] (multiply the contents of matrix c by the contents of matrix b, the result is stored in matrix c)
  3. c[i][j]=c[x][x] (replace the contents of matrix c with the contents of matrix c resulting from addition and multiplication)..\
    How is it bro? is it getting more confusing  😃 hehehe you know, matrix multiplication requires continuous practice to understand and understand more.. good luck

Maybe there is a space or enter space that causes an error, usually that happens when the program code is copied from different media.

int brsa, klmabrsb, klmb; //<-- Pertama, deklarasikan dulu variabelnya, seperti contoh ini.
cout<<"Baris matrik a : "; //<-- kedua, buat dialognya, agar user tau ini diminta menginputkan apa.
cin>>brsa; //<-- ketiga, gunakan perintah cin>> untuk menginputkan nilai ke variabel, contoh cin>>nama_variabelnya;
cout<<"Kolom matrik a Baris b : ";
cin>>klmabrsb;
cout<<"Kolom matrik b : ";
cin>>klmb;

That's it bro,

Hi ANDI MAYANG RIANI, The numbers entered are free / up to you, what is clear is that the input procedure for the program above is:

  1. Enter the matrix format, whether 3x3, 4x4, 4x3, so many x so many, so it's up to you.
  2. Just input the values/contents of the matrix, for example the format 2x3 is filled with all values/numbers one, so that it becomes:
1 1 1
1 1 1

Like that.


Post a Comment

Previous Next

نموذج الاتصال