C++ Program – Matrix Multiplication

by Nideesh C on May 9, 2011 · 1 comment

in C++




//Downloaded From theonlinetutorials.com
#include<iostream.h>
#include<conio.h>
#define size 100
void dis(int *a,int r1,int c1)
 {
 for(int i=0;i<r1;i++)
 {
 for(int j=0;j<c1;j++)
 {
 cout<<*(a+i*size+j)<<"\t";
 }
 cout<<endl;
 }
 }
 void getd(int *a,int *r1,int *c1)
 {
 cout<<"Enter no of row";
 cin>>*r1;
 cout<<"Enter no of col";
 cin>>*c1;
 cout<<"Now enter the data";
 for(int i=0;i<*r1;i++)
 {
 for(int j=0;j<*c1;j++)
 {
 cin>>*(a+i*size+j);
 }
 cout<<endl;

}
 }

void mul(int *a,int *b,int *r,int r1,int c1,int r2,int c2)
 {
 if(c1!=r2)
 {
 cout<<"Matrix can't multiply"<<endl;
 return;}
 for(int i=0;i<r1;i++)
 {
 for(int j=0;j<c1;j++)
 {
 *(r+i*size+j)=0;
 for(int k=0;k<c2;k++)
 {
 *(r+i*size+j)+=(*(a+i*size+k) * *(b+k*size+j));
 }
 }
 }
 }

int main()
 {
 int a[size][size],b[size][size],c[size][size];
 int r1,c1,r2,c2;
 getd(&a[0][0],&r1,&c1);
 getd(&b[0][0],&r2,&c2);
 cout<<"First matrix is"<<endl;
 dis(&a[0][0],r1,c1);
 cout<<"Second matrix is"<<endl;
 dis(&b[0][0],r2,c2);
 mul(&a[0][0],&b[0][0],&c[0][0],r1,c1,r2,c2);
 cout<<"Result of the multiplication is"<<endl;
 dis(&c[0][0],r1,c2);
getch();
return 0;
 }



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ Program – Matrix Manipulation
  2. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  3. Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.
  4. C program to read A (MxN), find the transpose of a given matrix and output both the input matrix and the transposed matrix.
  5. C program to accept a matrix and determine whether it is a sparse matrix. A sparse matrix is matrix which has more zero elements than nonzero elements

Leave a Comment

{ 1 trackback }

Previous post:

Next post: