Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
using namespace std;
class matrix
{
 int dim1,dim2;
 int **mat;
 public:
 matrix(int x=2,int y=2)
 {
  dim1=x;
  dim2=y;
  mat=new int*[dim1];
  for(int i=0;i<dim1;i++)
   mat[i]=new int[dim2];
  for(int i=0;i<dim1;i++)
   for(int j=0;j<dim2;j++)
    mat[i][j]=0;
 }
 int returndim1()
 {
  return dim1;
 }
 int returndim2()
 { 
  return dim2;
 }
 void input()
 {
  cout<<"Enter the elements of matrix - ";
  for(int i=0;i<dim1;i++)
   for(int j=0;j<dim2;j++)
    cin>>mat[i][j];
 }
 void out()
 {
  for(int i=0;i<dim1;i++)
   {
    cout<<"\n";
    for(int j=0;j<dim2;j++)
 cout<<mat[i][j]<<" ";
   }
 } 
 matrix operator+(matrix x)
 {
  matrix c(dim1,dim2);
  if(dim1==x.returndim1() && dim2==x.returndim2())
   {    
    for(int i=0;i<dim1;i++)
      for(int j=0;j<dim2;j++)
    c.mat[i][j]=this->mat[i][j]+x.mat[i][j];
 return c;
   }
  else
  {
   cout<<"Matrix cant be added";  
   return c;  
  }
 }
 matrix operator-(matrix x)
 {
  matrix c(dim1,dim2);
  if(dim1==x.returndim1() && dim2==x.returndim2())
   { 
    for(int i=0;i<dim1;i++)
      for(int j=0;j<dim2;j++)
    c.mat[i][j]=this->mat[i][j]-x.mat[i][j];
 return c;
   }
  else
  {
   cout<<"Matrix cant be subtracted";  
   return c;  
  }
 }
 matrix operator*(matrix x)
 {
  matrix c(dim1,x.returndim2());
  if(dim2==x.returndim1())
   { 
    for(int i=0;i<dim1;i++)
      for(int j=0;j<x.returndim2();j++)
      for(int k=0;k<dim2;k++)
        c.mat[i][j]+=this->mat[i][k]*x.mat[k][j];
 return c;
   }
  else
  {
   cout<<"Matrix cant be multiplied";  
   return c;  
  }
 }
};
int main()
{
 int x,y;
 char ch;
 do{
      cout<<"\n WELCOME TO MATRIX COMPUTATION"<<endl;
      cout<<"\n THE ASSIGNMENT WAS DONE BY:"<<endl;
      cout<<"\n                               "<<endl;
      cout<<"\n                               "<<endl;
      cout<<"\n                              "<<endl;
      cout<<"\n THE ASSIGNMENT IS DONE  FOR:;<endl;
      cout<<"\n*********************************************************"<<endl;
      cout<<"\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
      cout<<"\n*********************************************************"<<endl;      
            
      cout<<"\nTHIS ASSIGNMENT IS TO COVER THE FOLLOWING: "<<endl;
      cout<<"\n                         > Find and displaying Determinant"<<endl;
      cout<<"\n                         > Find and displaying the Inverse"<<endl;
      cout<<"\n                           matrix"<<endl;
      cout<<"\n                         > Carrying out row transformation"<<endl;
      cout<<"\n                         > Addition & Subtraction"<<endl;
      cout<<"\n                         > Multiplicationn & division"<<endl;
      cout<<"\n"<<endl;
 cout<<"\nEnter elements, select the value then space-bar then input next"<<endl;
 cout<<"Enter the dimension of matrix 1 (rows x cols) - : "<<endl;
 cout<<"\n"<<endl;  
 cin>>x>>y;
 matrix a(x,y);
 a.input();
 cout<<"\nEnter elements, select the value then space-bar then input next"<<endl;
 cout<<"\nEnter the dimension of matrix 2 (rows x cols) - : "<<endl;
 cout<<"\n"<<endl; 
 cin>>x>>y;
 matrix b(x,y);
 b.input();
 cout<<"\n1.Add matrices\n2.Subtract matrices\n3.Multiplymatrices\n";
 cout<<"Select any one above , then hit ENTER"<<endl;
 cin>>x;
 if(x==1)
  (a+b).out();
 else if(x==2)
  (a-b).out();
 else if(x==3)
  (a*b).out();
 else
  cout<<"Wrong choice";
 cout<<"\nContinue(y/n)";
 cin>>ch;
 }while(ch=='y'||ch=='Y');
 cin.get();
 return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 30-Nov-11 1:13am    
Not a question!
--SA
Sergey Alexandrovich Kryukov 30-Nov-11 1:14am    
Not even clear what do you want.
--SA
enhzflep 30-Nov-11 4:21am    
WTF? Mixing "\n" and std::endl.... Why?
Why not throw a printf in there for good measure too?!

As for the Topic line, what the hell is "Wish to find the inverse , row transformation,, determinant of both matrix, if possible i want to call just one and matrix and a time and perform these operations, is that possible?" even supposed to mean?

Hint:
Subject line = "Matrix operations in C++"
Question = Er, Ahrm - the question you have!!

Hint #2:
cout<<"\n THE ASSIGNMENT IS DONE FOR:;<endl;
Is never going to compile!!

1 solution

Any matrix operations are possible and were comprehensively implemented in several languages including C++. Is your code which you published without any comments related to this. I wish I could understand your requirements so inaccurately formulated in the title of this question. Nevertheless, I hope I answered the main question: it is possible. :-) Any concerns?

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900