Addition , Multiplication and transpose of two dimensional matrix without using pointers in C++
Download Source Code here
C++ Code for addition ,multiplication and Transpose of 2-D matrices
Linked list implementation using C++
#include<iostream>
using namespace std;
int read(int[][5] ,int,int);
int display(int[][5],int ,int);
int mult(int [][5],int[][5],int[][5],int,int,int,int);
int add(int[][5],int[][5] ,int[][5] ,int,int,int,int);
int transpose(int [][5],int [][5],int,int);
int main()
{
int mat1[5][5],mat2[5][5],mat3[5][5],r1,c1,r2,c2;
cout<<"Enter size of 1st matrix : ";
cin>>r1>>c1;
cout<<"Size of 2nd matrix : ";
cin>>r2>>c2;
read(mat1,r1,c1);
read(mat2,r2,c2);
cout<<"Displaying 1st matrix :\n";
display(mat1,r1,c1);
cout<<"Displaying 2st matrix :\n";
display(mat2,r2,c2);
add(mat1,mat2,mat3,r1,c1,r2,c2);
if(r1==r2&&c2==c1)
{
cout<<"Displaying addition of matrix :\n";
display(mat3,r1,c1);
}
else
{
cout<<"No Addition Possible !\n";
}
if(c2==r2)
{
cout<<"Displaying Multipplication of matrix :\n";
mult(mat1,mat2,mat3,r1,c1,r2,c2);
display(mat3,r1,c2);
}
else{
cout<<"No Multiplication Possible !\n";
} cout<<"Displaying Transpose of matrix :\n"; transpose(mat1,mat3,r1,c1); display(mat3,c1,r1); } int read(int a[][5],int r,int c) { cout<<"Enter the elements of matrix :\n"; for(int i=0;i<r;i++) for(int j=0;j<c;j++) { cin>>a[i][j]; } return 0; } int display(int a[][5],int r,int c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<a[i][j]<<"\t"; } cout<<"\n"; } return 0; } int add(int a[][5],int b[][5],int c[][5],int r1,int c1,int r2,int c2) { for(int i=0;i<r1;i++) for(int j=0;j<c1;j++) { c[i][j] = a[i][j]+b[i][j]; } return 0; } int mult(int a[][5],int b[][5],int result[][5],int r1,int c1,int r2,int c2) { for(int i=0;i<r1;i++) for(int j=0;j<c2;j++) { result[i][j] =0; for(int k=0;k<c1;k++) { result[i][j] = result[i][j] + a[i][k]*b[k][j]; } } return 0; } int transpose(int m1[][5],int m2[][5],int r,int c) { for(int i=0;i<r;i++) for(int j=0;j<c;j++) { m2[j][i] = m1[i][j]; } return 0; }
Output:
Enter size of 1st matrix : 2 2
Size of 2nd matrix : 2 2
Enter the elements of matrix :
1 2 3 4
Enter the elements of matrix :
4 3 2 1
Displaying 1st matrix :
1 2
3 4
Displaying 2st matrix :
4 3
2 1
Displaying addition of matrix :
5 5
5 5
Displaying Multipplication of matrix :
8 5
20 13
Displaying Transpose of matrix :
1 3
2 4
Comments