Addition of Array, Array example in C++, Array program
Write a C++ program to add two array A and B of size n x m.

Program Code:
#include<iostream>
using namespace std;
int main()
{
int n, m;
int A[10][10], B[10][10];
int i, j, ADD[3][3];
cout<<"Enter total number of rows in matrix A and B : ";

cin>>n;
cout<<endl;
cout<<"Enter total number of columns in matrix A and B : ";
cin>>m;
cout<<endl;
cout<<"Enter first matrix elements : ";
cout<<endl;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cin>>A[i][j];
}
}
cout<<"Enter second matrix elements : ";
cout<<endl;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cin>>B[i][j];
}
}
// Adding  matrix A and B to form the ADD matrix .....
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
ADD[i][j] = A[i][j] + B[i][j];
}
}
cout<<"The new matrix ADD will be : ";
cout<<endl;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<ADD[i][j]<<" ";
}
cout<<endl;;
}
return 0;
}


The program output is tested on www.jdoodle.com
Output:
Enter total number of rows in matrix A and B : 3
Enter total number of columns in matrix A and B : 3
Enter first matrix elements : 
1 2 3 4 5 6 7 8 9 
Enter second matrix elements : 
9 8 7 6 5 4 3 2 1 
The new matrix ADD will be : 
10 10 10 
10 10 10 
10 10 10 


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

1 comments:

  1. what is the code if number of rows and number of columns are different of both arrays.

    ReplyDelete