Home / Education / How to Multiply Two Matrices In c++, matrix program in C Computer science

How to Multiply Two Matrices In c++, matrix program in C Computer science

How to Multiply Two Matrices In c++, matrix program in C Computer Science

 

 

Here you can see the ways by which How to Multiply Two Matrices In c++, matrix program in c++ Computer science..

The following code fragment will multiply two (3 x 3) matrices A*B and write the result in matrix C.

 int i,j,k;
 for (i=1; i<=3; i++)
   for (j=1; j<=3; j++)
     {
     sum = 0;
     for (k=1; k<=3; k++)
       sum = sum + A[i][k]*B[k][j];
     }
     C[i][j] = sum;

Note: This scheme assumes that the matrices were declared as A[4][4], B[4][4], C[4][4]. Some elements, A[0][], A[][0], etc. will not be used.

 

———————————————————————

 

The 2nd one method..

 

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int a[10][10];int b[10][10];int c[10][10];
void main()
{
int i,j,k,l;
clrscr();
cout<<“Enter the value of the matrix a:”;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<“\n Enter the value of the matrix b:”;
or(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}

cout<<“\n The multiplied value is:”;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j];
}
cout<<“\n”
}

getch();
}

————————————————————-

 

 

C Program for Matrix Multiplication

The ordinary matrix multiplication is the most often used and the most important way to multiply matrices. It is defined between two matrices only if the width of the first matrix equals the height of the second matrix. Multiplying an m×n matrix with an n×p matrix results in an m×p matrix.

 

How to Multiply Two Matrices In c++, matrix program in C Computer science
How to Multiply Two Matrices In c++, matrix program in C Computer science

This One is better from All above

 

 


# include “stdio.h”
main()
{
int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
printf(“Enter number of rows and columns of first matrix (less than 10)\n”);
scanf(“%d%d”,&r1,&c1);
printf(“Enter number of rows and columns of second matrix (less than 10)\n”);
scanf(“%d%d”,&r2,&c2);
if(r2==c1)
{
printf(“Enter rows and columns of First matrix \n”);
printf(“Row wise\n”);
for(i=0;i
for(j=0;j
scanf(“%d”,&m1[i][j]);
printf(“First Matrix is :\n”);
for(i=0;i
{
for(j=0;j
printf(“%d\t”,m1[i][j]);
printf(“\n”);
}
printf(“Enter rows and columns of Second matrix \n”);
printf(“Row wise\n”);
for(i=0;i
for(j=0;j
scanf(“%d”,&m2[i][j]);
printf(“Second Matrix is:\n”);
for(i=0;i
{
for(j=0;j
printf(“%d\t”,m2[i][j]);
printf(“\n”);
}
printf(“Multiplication of the Matrices:\n”);
for(i=0;i
{
for(j=0;j
{
mult[i][j]=0;
for(k=0;k
mult[i][j]+=m1[i][k]*m2[k][j];
printf(“%d\t”,mult[i][j]);
}
printf(“\n”);
}
}
else
{
printf(“Matrix multiplication cannot be done”);
}
return 0;
}

Check Also

UCP Spring Admission 2011 Are Open

UCP Spring Admission 2011 Are Open UCP Spring Admission 2011 Are Open University of Central …

One comment

  1. #include<stdio.h>

    #include<conio.h>

    int A[3][4],B[3][4],C[3][4],i,j;

    void main()

    {

    clrscr();

    printf("Enter the Number for Row n col for matrix1:\n");

    for(i=0;i<=2;i++)

    for(j=0;j<=3;j++)

    scanf("%d",&A[i][j]);

    printf("First matrix is:\n");

    for(i=0;i<=2;i++)

    {

    for(j=0;j<=3;j++)

    printf("%d\t",A[i][j]);

    printf("\n");

    }

    printf("Enter the Number for Rowz n col for matrix2:\n");

    for(i=0;i<=2;i++)

    for(j=0;j<=3;j++)

    scanf("%d",&B[i][j]);

    printf("Second matrix is:\n");

    for(i=0;i<=2;i++)

    {

    for(j=0;j<=3;j++)

    printf("%d\t",B[i][j]);

    printf("\n");

    }

    printf("Multiplication Of both metrixs\n");

    for(i=0;i<=2;i++)

    {

    for(j=0;j<=3;j++)

    {

    C[i][j]=0;

    C[i][j]=C[i][j]+A[i][j]*B[i][j];

    printf("%d\t",C[i][j]);

    }

    printf("\n");

    }

    getch();

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.