0
#include <iostream>

using namespace std;

int main()
{
   int a[3][3] = {1,1,1,1,1,1,1,1,1};
   int b[3][3] = {1,1,1,1,1,1,1,1,1};
   int c[3][3];
   for(int i=1;i<=3;i++)
   {
       for(int j=1;j<=3;j++)
       {
           c[i][j]=a[i][j]+b[i][j];
           cout<<"Element of C"<<i<<j<<"is\t"<<c[i][j]<<endl;
       
       }
  
   
   }

       return 0;

}

In the above code i a am getting correct output till A22 but for above it is throwing me a garbage value.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 3
    You need to learn about indices to arrays in C: indices start from ZERO, hence a three-item array has indices 0, 1 and 2, that is _less than_ three and not _less-or-equal_ three. – CiaPan Jul 18 '21 at 18:52

2 Answers2

4

You need to index your matrices starting from 0, not 1. So, you need to change you for loops to be like for(int i=0;i<3;i++).

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
saferif
  • 187
  • 10
2
  • Do not using namespace std

  • Two-dimensional arrays should better be initialized with double parenthesis

    int a[3][3] = {{1,1,1},{1,1,1},{1,1,1}};

    int b[3][3] = {{1,1,1},{1,1,1},{1,1,1}};

  • Array indices in C/C++ start from 0. Hence the loop should read

    for(int i=0;i<3;i++)

    and similar for the nested loop

  • Get a good book for C++.

francesco
  • 7,189
  • 7
  • 22
  • 49