add elements in multidimensional array in C++

यहाँ पर हम दो programs के बारे में चर्चा करेंगे –

Addition, multiply and subtraction operation with two Array-Elements using Single Dimensional Array

#include<iostream.h>
#include<conio.h>

void main()
{
   clrscr();
   int x[5],y[5],i,j;

    cout<<"Enter the element in the 1st array:\n";
    for(i=0;i<5;i++)
     {
       cout<<"arr["<<i<<"] = "; 
       cin>>x[i]; // store 1st array element
     }

   cout<<"\nEnter the element in the 2nd array:\n";
   for(i=0;i<5;i++)
    { 
      cout<<"arr["<<i<<"] = "; 
      cin>>y[i]; // store 2nd array element
    }

   cout<<"\nElements are:\n";
   for(i=0;i<5;i++)
    {
      cout<<x[i]<<" + "<<y[i]<<" = "<<x[i]+y[i]<<"\t"; //addition
      cout<<x[i]<<" - "<<y[i]<<" = "<<x[i]-y[i]<<"\t"; //subtraction
      cout<<x[i]<<" x "<<y[i]<<" = "<<x[i]*y[i]<<endl; //multiply
    }

 getch();
}

OUTPUT

Enter the element in the 1st array:
arr[0] = 3
arr[1] = 4
arr[2] = 2
arr[3] = 1
arr[4] = 4

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

Elements are:
3   +   3  =   6	3   -   3  =   0	3   x   3  =   9
4   +   2  =   6	4   -   2  =   2	4   x   2  =   8
2   +   5  =   7	2   -   5  =   -3	2   x   5  =   10
1   +   6  =   7	1   -   6  =   -5	1   x   6  =   6
4   +   4  =   8	4   -   4  =   0	4   x   4  =   16

Addition, multiply, subtraction,division operation with Two Array-Elements using Two Dimensional Array

In this program, we have made two two-dimensional array declarations and entered elements from the user in both.

यहाँ पर line of code बढ़ जाने के कारण program को अलग अलग block में बांटा गया है ताकि इसे आसानी से समझा जा सके –

int x[2][3], y[2][3];

Here is the program,

#include<iostream.h>
#include<conio.h>
#include<dos.h> // for system("pause");

void main()
 {
   clrscr();
   int x[2][3],y[2][3],i,j;

enter the elements in the first array x[2][3],

cout<<"Enter the element in the 1st array:\n";
  for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
      {
        cout<<"arr["<<i<<"]["<<j<<"] = "; 
        cin>>x[i][j]; // store 1st array element
      }
   }

then,  enter the elements in the second array y[2][3],

 cout<<"\nEnter the element in the 2nd array:\n";
  for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
      {
        cout<<"arr["<<i<<"]["<<j<<"] = "; 
        cin>>y[i][j]; // store 2nd array element
      }
   }
cout<<endl;
  system("pause");
  clrscr();

After all the elements are entered, The elements of the array x[i][j] will be displayed,

cout<<"\n1st array:\n";
  for(i=0;i<2;i++) 
   {
     for(j=0;j<3;j++)
      {
        cout<<x[i][j]<<" "; // display 1st array element
      }
   }

just like that the elements of the array y[i][j] will be displayed,

cout<<"\n2nd array:\n";
  for(i=0;i<2;i++)
   {
     for(j=0;j<3;j++)
      {
        cout<<y[i][j]<<" "; // display 2nd array element
      }
   }

After this, the first two arrays elements will be added,

cout<<"\n\nAdition 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]<<" "; // addtion
     }
  }

just like that, other operations will be performed,

cout<<"\nSubstraction 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
      }
   }

 cout<<"\nMultiplicaton 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
     }
  }


 cout<<"\nDivision 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]<<" "; // division
     }
  }

 getch();
}

Thus program successfully executed,

OUTPUT

Enter the element in the 1st array:
arr[0][0] = 5
arr[0][1] = 8
arr[0][2] = 9
arr[1][0] = 7
arr[1][1] = 3
arr[1][2] = 4
Enter the element in the 2nd array:
arr[0][0] = 6
arr[0][1] = 5
arr[0][2] = 2
arr[1][0] = 3
arr[1][1] = 7
arr[1][2] = 8

1st array:
5 8 9 7 3 4 
2nd array:
6 5 2 3 7 8 

Addition of element in the array
11 13 11 10 10 12 

Subtraction of element in the array: 
-1 3 7 4 -4 -4 

Multiplication of element in the array: 
30 40 18 21 21 32 

Division of element in the array: 
0 1 4 2 0 0

class with multidimensional array in C++

Leave a Reply

Your email address will not be published.