Thursday, 29 November 2012

2 dimentional array (matrix) operations


/*A program to implement 2 dimentional array (matrix) operations.
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class array2d
{
 private:
 int a[10][10],b[10][10],m,n,i,j,k,p,choice;
 public:
 int  insertion();
 void traverse(int);
 void deletion();
 void searching();
 void merging();
 array2d();
};

int array2d::insertion()
{
 cout<<"Enter no. of rows for matrix:";
 cin>>n;
 cout<<"Enter no. of columns for matrix:";
 cin>>m;
 cout<<"Enter the elements for matrix:\n";
 for(int i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  {
   cout<<"Enter element for matrix location "<<i<<","<<j<<" :";
   cin>>a[i][j];
  }
 }
 traverse(n);
 return n;
}

void array2d::traverse(int n=0)
{
 if(n>0)
 {
  cout<<"\nEntered Elements are:-\n";
  for(int i=0;i<n;i++)
  {
   for(int j=0;j<m;j++)
   cout<<a[i][j]<<"\t";cout<<"\n";
  }
 }
 else  cout<<"matrix is empty";
}

void array2d::deletion()
{
 cout<<"Enter no. that you want to delete:";
 cin>>p;k=0;
 for(i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  if(a[i][j]==p)
  {
   k=1;a[i][j]=0;break;
  }
 }
 if(k==1)
 {
  cout<<"no.'"<<p<<"' is replaced by Zero";
  cout<<"\nAfter Deletion :";traverse(n);
 }
 else cout<<"element not present in matrix";
}

void array2d::searching()
{
 cout<<"Enter no. that you want to Search:";
 cin>>p;k=0;
 for(i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  if(a[i][j]==p)
  {
   k=1;
   cout<<p<<" Present in the matrix at location "<<i+1<<","<<j+1;break;
  }
 }
 if(k==1);
 else cout<<"element not present in matrix";
}

void array2d::merging()
{
 if(n==0)
 {
  cout<<"First must be enter 1st matrix elements\n";
  insertion();
 }
 cout<<"Enter the elements for 2nd matrix:\n";
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  {
   cout<<"Enter element for matrix location "<<i<<","<<j<<" ";;
   cin>>b[i][j];
  }
 }
 cout<<"First matrix ";traverse(n);
 cout<<"\nSecond Matrix is:-\n";
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  cout<<b[i][j]<<"\t";cout<<"\n";
 }
 do
 {
  cout<<"\n\t\t matrix merging oprations are:-\n";
  cout<<"1. Addition\n2. Substraction\n3. Multiplication\n";
  cout<<"4. Dividation\n5. Back Menu\n6. Exit";
  cout<<"\nEnter Your Choice";
  cin>>choice;
  switch(choice)
  {
   case 1:
 cout<<"\nAddition of both matrix:\n";
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  cout<<a[i][j]+b[i][j]<<"\t";cout<<"\n";
 }break;
   case 2:
 cout<<"\nSubstraction of both matrix:\n";
 for(i=0;i<n;i++)
 {
   for(j=0;j<m;j++)
   cout<<a[i][j]-b[i][j]<<"\t";cout<<"\n";
 }break;
   case 3:
 cout<<"\Multiplication of both matrix:\n";
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  cout<<a[i][j]*b[i][j]<<"\t";cout<<"\n";
 }break;
   case 4:
 cout<<"\nDividation of both matrix:\n";
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  cout<<a[i][j]/b[i][j]<<"\t";cout<<"\n";
 }
 break;
   case 5:
 return;
   case 6:
 exit(1);
  default:
 cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }while(1);
}

array2d::array2d()
{
 m=0,n=0,k=0;
 do
 {
  cout<<"\n\t\tMULTI DIMENTIONAL ARRAY\n";
  cout<<"\t\t1. To Insert elements in matrix\n";
  cout<<"\t\t2. To Delete an Element\n";
  cout<<"\t\t3. To Display the Elements\n";
  cout<<"\t\t4. To Search an item\n";
  cout<<"\t\t5. To Merge the multidimentinal arrays\n";
  cout<<"\t\t6. To Main page\n";
  cout<<"\t\t7. To Exit from Program\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
   n=insertion();break;
   case 2:
   deletion();break;
   case 3:
   traverse(n);break;
   case 4:
   searching();break;
   case 5:
   merging();break;
   case 6:
 return;
   case 7:
 exit(1);
   default:cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }while(1);
}
/*=========================2Nov.,2010=============================*/

No comments:

Post a Comment

You are welcome, your comment helps me to make this blog more better.