class with multidimensional array in C++

 

class with multidimensional array in C++ – This program has also been given in examples of array, in which some basic tasks have been done with elements of multi-dimensional array.

Here in this program, using the class and switch statement has been tried to improve the program. Two user defined functions have been used in the program. Whose definition is defined within the class in a way inline definition.

Also the appropriate comments in the program Given, so that the implementation of the program can be understood.

class with multi dimensional array

#include<iostream>
#include<stdib> //exit function
using namespace std;

class array // class name array
{
  int x[2][3],y[2][3],i,j;
  public:

 void getdata()
  {
    cout<<"Enter the element in the 1st array\n";
    for(i=0;i<2;i++) 
     {
      for(j=0;j<3;j++)
      {
        cout<<"a["<<i<<"]["<<j<<"] = "; cin>>x[i][j];     // storing value into variable x[][]
      }
    }
 cout<<endl;

 cout<<"Enter the element in the 2nd array\n";
 for(i=0;i<2;i++)  
  {
    for(j=0;j<3;j++)
    {
      cout<<"a["<<i<<"]["<<j<<"] = "; cin>>y[i][j];      // storing value into variable y[][]
    }
  }
}

void putdata()
{
  cout<<"1st array:\n";
  for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
     {
       cout<<x[i][j]<<" ";  // displaying value of variable x[][]
     }
   }
  cout<<endl;

  cout<<"2nd array:\n";
  for(i=0;i<2;i++)
  {
    for(j=0;j<3;j++)
   {
    cout<<y[i][j]<<" "; //displaying value of variable y[][]
   }
 }
 cout<<endl;
}

void sum()
{
  cout<<"adition of element in the array\n";
  for(i=0;i<2;i++)
  {
    for(j=0;j<3;j++)
   {
     cout<<x[i][j]+y[i][j]<<" ";  //sum of array-variable
   }
 }
}

void substract()
{
   cout<<"subtraction of element in the array\n";
   for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
     {
       cout<<x[i][j]-y[i][j]<<" ";    //subtraction of array-variable
     }
   }
}
void multi()
{
   cout<<"multiplicaton of element in the array\n";
   for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
    {
      cout<<x[i][j]*y[i][j]<<" ";       //multiply of array-variable
    }
  }
}
void div()
{
  cout<<"division of element in the array\n";
  for(i=0;i<2;i++)
  {
    for(j=0;j<3;j++)
    {
      cout<<x[i][j]/y[i][j]<<" ";    //devision of array-variable
    }
 }
}
};  // class terminate here

int main()
{
   
   int ch,i=0,a;

   array obj; // declared object of array class
   obj.getdata(); // calling function using object obj

   
   cout<<"Your entered list are\n";
   obj.putdata();// calling function using object obj

// creating a menu wizard using do-while loop
  do { 
        cout<<endl<<endl;
        cout<<"***OPERATION MENU***\n";
        cout<<"enter your choice\n";
        cout<<"1.ADITION\n";
        cout<<"2.SUBSTRACTION \n";
        cout<<"3.MULITPLY\n";
        cout<<"4.DIVISION\n";
        cout<<"0.EXIT\n"; cin>>ch;

//calling each function using case
        switch(ch)
       {
          case 1: {
                       
                       obj.sum();
                       cout<<endl;
                       obj.putdata();
                       break;
                     }
          case 2: {
                       
                       obj.substract();
                       cout<<endl;
                       obj.putdata();
                       break;
                      }
          case 3: {
                      
                       obj.multi();
                       cout<<endl;
                       obj.putdata();
                        break;
                      }
          case 4: {
                      
                      obj.div();
                      cout<<endl;
                      obj.putdata();
                      break;
                      }
           case 0: {
                      exit(0);
                      break;
                      }
           default : cout<<"please choose valid option\n";
                        break;
         }
   } while(ch!=0);

return 0;
}

OUTPUT

Enter the element in the 1st array
a[0][0] = 3
a[0][1] = 2
a[0][2] = 1
a[1][0] = 5
a[1][1] = 4
a[1][2] = 6

Enter the element in the 2nd array
a[0][0] = 7
a[0][1] = 6
a[0][2] = 4
a[1][0] = 3
a[1][1] = 2
a[1][2] = 5

Your entered list are
1st array:
3 2 1 5 4 6 
2nd array:
7 6 4 3 2 5

***OPERATION MENU***
enter your choice
1.ADITION
2.SUBSTRACTION 
3.MULITPLY
4.DIVISION
0.EXIT

Explanation :- In the program we add two different array elements with each other. here is,

x[2][3],y[2][3]

where getdata() function used to input elements by the user while putdata() will display them.

After both these Two function, we used switch statement to create a menu where end-user will select choice and program will Perform the related task only by executing the function( every case statement will perform different task ).

Here is the following function we used,

sum() // for addition
substract() // for substraction
multi()  / for multiply 
div()  // for division

Thus Program will Successfully executed..

Note: Program is simple Only code is large,

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version