Add different array element with each other

In this page we will discuss two examples with array-elements where we use two array and perform the addition, multiply, subtraction task with following both type array,

 

addition, multiply and subtraction operation with two array-elements using single dimensional array

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

int x[5], y[5];

Here is the program,

#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.

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;

First of all, the user has been asked to enter the elements in the 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, the user has been asked to enter the elements in the 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 

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

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