Ad Code

Responsive Advertisement

Write a C++ program for Addition and Multiplication of two matrices by overloading + and * operators.










// ADDITION OF TWO MATRIX using matrix.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
class A{
 int a[3][3],c[3][3],i,j;
 int r1,c1;    /*  here we take two veriable for ROW and COLLUMN*/
 public:
 void set_rc(int r,int c){
  r1=r;
  c1=c;
 }
 void set_ADD(){
  cout<<"\n\nEnter a value of first matrix :\n";
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){
    cin>>*(*(a+i)+j);
   }
  }
 } 
 void set_MUL(){
  cout<<"\n\nEnter a value of second matrix :\n";
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){ /* IT IS SAME AS A &b[i][j] */ 
    cin>>*(*(a+i)+j);
   }
  }
 } 
 void get_ADD(){
  cout<<"\n\nFirst matrix value :\n\n";
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){
    cout<<"\t"<<*(*(a+i)+j);
   }
  cout<<"\n";
  }
 }
 void get_MUL(){
  cout<<"\n\nSecond matrix value :\n\n";
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){
    cout<<"\t"<<*(*(a+i)+j);
   }
  cout<<"\n";
  }
 }
 
 A operator +(A obj){
  cout<<"\n\nAddition of two matrix is :\n\n";
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){
    cout<<"\t"<<*(*(a+i)+j)+*(*(obj.a+i)+j);
   }
  cout<<"\n";
  }
 }
 A operator *(A obj){
  cout<<"\n\nMultification of two matrix is :\n\n";
  for(i=0; i<obj.r1; i++){
   for(j=0; j<c1; j++){
    c[i][j]=0;
    for(int k=0; k<c1; k++){
     c[i][j]+=a[i][k]*obj.a[k][j];
    }
   }
  }
  for(i=0; i<r1; i++){
   for(j=0; j<c1; j++){
    cout<<"\t"<<c[i][j];
   }
  cout<<"\n";
  }
 }
};
int main(){
 A obj,obj2;
 int r,c,r2,c2;
 cout<<"Enter first matric  row :";
 cin>>r;
 cout<<"Enter first matric column :";
 cin>>c;
 obj.set_rc(r,c);
 cout<<"Enter second matic  row :";
 cin>>r2;
 cout<<"Enter second matric  column :";
 cin>>c2;
 obj2.set_rc(r2,c2);
 if(r==r2 && c==c2){  /* Checking the condition for add two matric */
  obj.set_ADD();
  obj2.set_ADD();
  obj+obj2;
 }
 else{cout<<"\n\nEnter valid row and column \n\n";}
 cout<<"Enter first matric  row :";
 cin>>r;
 cout<<"Enter first matric column :";
 cin>>c;
 obj.set_rc(r,c);

 cout<<"Enter second matic  row :";
 cin>>r2;
 cout<<"Enter second matric  column :";
 cin>>c2;
 obj2.set_rc(r2,c2);
 if(c==r2){
  obj.set_MUL();
  obj2.set_MUL();
  obj.get_MUL();
  obj2.get_MUL();
  obj*obj2; 
 }
} 



out put:
Enter first matri row : 2
Enter first matric column : 2

Enter second matric row : 2
Enter second matric column : 2

Enter a value of first matric: 
1
2
3
4

Enter a value of second matric :
2
1
3
4

Addition of two matric :
     
     3      3
     7      7

Enter first matri row : 2
Enter first matric column : 2

Enter second matric row : 2
Enter second matric column : 2

Enter a value of first matric: 
1
2
3
4

Enter a value of second matric :
2
1
4
3

First matric value 

    1    2
    3    4

second matric value 
     1    2 
     4    3

multification of two matic is 

    7     10
    13    20




 



If you like code with vibhu  and would like to contribute, you can also write an article using   This link :

https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1


OR  mail your article to codewithvibhu@gmail.com.  See your article appearing on the code with vibhu main page and help other students. 

Post a Comment

0 Comments