0
Which is better as far as functionality?
for (rows = 0; rows < 6; rows++) { for (cols = 0; cols < 6; cols++) { if (matrix[rows][cols] == 0) { matrix[rows][cols] = 1; } cout << matrix[rows][cols]; } or for (rows=0; rows < 6; rows++){ for (cols=0; cols < 6; cols++){ cout << matrix[rows][cols] + 1; } cout << endl; }
7 Answers
+ 3
Lexx
In second case suppose you have matrix value 1 so it would be added in 1 and result would be 2
But in 1st case if matrix value is 1 then output would be 1.
So you cannot say both outputs are same.
+ 3
Lexx
If you want to make all elements 1 then just simply do this no need to check anything.
for (rows = 0; rows < 6; rows++) {
for (cols = 0; cols < 6; cols++) {
matrix[rows][cols] = 1;
cout << matrix[rows][cols];
}
}
+ 2
Lexx Welcome my friend.
+ 1
interestingly enough that was my first solution but I put the
matrix[rows][cols] = 1;
before the for statements so that was likely my issue in the first place. Thanks for the reply and help AJ.
0
Lexx
Do you want to add 1 or assign 1 to matrix?
0
it outputs the same so you are going to have to explain the diffrence.
0
all matrix elements are 0 I should have clarified that and the objective was to make all elements = 1.