Which is better as far as functionality? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

23rd May 2021, 5:49 PM
Lexx
Lexx - avatar
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.
23rd May 2021, 6:12 PM
A͢J
A͢J - avatar
+ 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]; } }
23rd May 2021, 6:22 PM
A͢J
A͢J - avatar
+ 2
Lexx Welcome my friend.
23rd May 2021, 6:29 PM
A͢J
A͢J - avatar
+ 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.
23rd May 2021, 6:28 PM
Lexx
Lexx - avatar
0
Lexx Do you want to add 1 or assign 1 to matrix?
23rd May 2021, 6:05 PM
A͢J
A͢J - avatar
0
it outputs the same so you are going to have to explain the diffrence.
23rd May 2021, 6:09 PM
Lexx
Lexx - avatar
0
all matrix elements are 0 I should have clarified that and the objective was to make all elements = 1.
23rd May 2021, 6:18 PM
Lexx
Lexx - avatar