# I need help with this C++ program that adds matrices and edits the result.

This is a discussion on I need help with this C++ program that adds matrices and edits the result. within the C++ Programming forums, part of the General Programming Boards category; The program adds 2 matrices that are 3x3 using arrays and then stores them into another matrix (array) and then ...

1. ## I need help with this C++ program that adds matrices and edits the result.

The program adds 2 matrices that are 3x3 using arrays and then stores them into another matrix (array) and then it's edited to show a diagonal line of "0" through it, btw I'm pretty new to programming and this is my first prog.language to learn.

insert
Code:
```#include <iostream>
using namespace std;
int main()
{
int x[3][3],y[3][3],c[3][3],i,j;

for(i=0;i<=2;i++)       // Entry stage
for(j=0;j<=2;j++)
{
cin>>x[i][j];
cin>>y[i][j];
}

for(j=0;j<=2;j++)
c[i][j]=y[i][j]+x[i][j];

for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
if(i=j)
c[i][j]=0;
}

for(i=0;i<=2;i++)      // Plotting the zero line and displaying the result
{
cout<<endl;

for(j=0;j<=2;j++)
cout<<c[i][j]<<" ";

}
return 0;
}```
It works almost just fine lol, Except that the first portion of the diagonal line does not become zero and instead displays the normal addition result :/, please ,i would be grateful for any help.

You're using = where you should be using ==

3. Originally Posted by Salem
Check your if statement.You're using = where you should be using ==
Yes, it worked! Why didn't I notice that?! Thank you,but I AM a bit curious about the fact that it did work but made that small mistake even with just an assignment operator instead on an equality operator. Would you be kind enough to tell me why ?

4. It compiled because this:
Code:
```if(i=j)
c[i][j]=0;```
amounts to:
Code:
```i = j;
if (i)
c[i][j] = 0;```

5. Originally Posted by laserlight
It compiled because this:
Code:
```if(i=j)
c[i][j]=0;```
amounts to:
Code:
```i = j;
if (i)
c[i][j] = 0;```
Which subsequently is evaluated as
Code:
```i = j;
if (i != 0)
c[i][j] = 0;```
Not everyone may be aware of the implicit comparison against 0.

6. Originally Posted by sonjared
Which subsequently is evaluated as
Code:
```i = j;
if (i != 0)
c[i][j] = 0;```
Not everyone may be aware of the implicit comparison against 0.
Good clarification, though technically there is an implicit conversion to bool, not an implicit comparison against 0, though the net effect (and probably the translation performed by the compiler) is the same.