c++ program to add two matrices

BOOKC, this time share about c++ program to add two matrices
Write a c++ program to add two matrix using 2-D arrays use any c++ compiler, turbo c++, visual studio or codeblocks.
2D arrays are just like tables which consist of rows and colums and each cell contains a value.
Matrix addition as

1     2         1     2           1+1     2+2                       2         4
            +                = >                           Result=
3     4         3     6          3+3      4+6                       6        10

Concept used:
  • const int  (Important concept here for array size)
  • 2D arrays
  • Nested for loops

Program explanation:
  • For 2D array size there must be constant value in square brackets like
    array[constant value][constant value]
  • Two const variables row and col are used to define size
    if we do not make both const then error found because without const reserve word they are
    behaving as variable.
  • Before placing both variable in square brackets they must initialized else error will be found
  • three nested for loops are used two for taking input in matrix 2D arrays and one for resultant matrix

c++ code
codeblocks c++ compiler

using namespace std;

int main(){

//Using const int for array size
    const int row=2,col=2;
// if not use const error found

cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;

int first[row][col], second[row][col];

int i,j;

    cout<<"Enter value for row number: "<<i+1<<endl;


cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;

    cout<<"Enter value for row number: "<<i+1<<endl;




// Resultant Matrix




cout<<"\n\n\t\tResultant Matrix:"<<endl;
    cout<< endl;
        cout<<"\t\t"<<first[i][j]<<"    ";


return 0;

sample input out:

This program is helpful for c++ beginners to understand the basic working of 2D arrays. It is recommended to change the code and examine the output.

Hopefully the information we provide can be useful.

Press ESC to close