0
Unable to find the mistake
I just tried to write a code to set values into a twodimensional array and output them. But it seems that I made a mistake and I am not able to find it. Could please someone help me? https://code.sololearn.com/cD1c5uxpajwD/?ref=app
10 odpowiedzi
+ 3
Change both of these:
if(y==4) {x++;y=0;}
to this:
if(y==3) {x++;y=-1;}
and y will immediately ++ to 0 when the loop restarts.
+ 11
+ 10
It should be: if(y==4)
+ 9
You'll have to use nested loop, I'm going to post the modified code soon.
+ 4
@Felix Felix ... Now that you've accepted @Shamima Yasmin's code...if you add (to yours) at line 14:
else {cout << x << ", " << y << ": " << arr[x][y] << endl;}
Your code skips elements (1,0) (2,0) and (3,0). The skipped elements retain garbage values and your count stops early at z=13...with 5-13 jumping positions. The problem is setting y=0 (vs -1).
There's nothing especially wrong with what you're trying to do; Shamima's answer is easier to maintain/follow though :)
+ 3
There is a mistake :
When write if(y=4), what you actually do is setting the value of y to 4 !
If you want to VERIFY you have to use '==' :
if(y==4)
If you have any question ask
+ 3
Looks like you're bleeding into 'for's' local variable stack (x=3, y=4 ... you're retrieving y) because you're overrunning your array with y=4.
You could probably fix it with: if (y!=4)... but the -1 really did work I thought.
* Anyone checking me can flip x=0,y=0 around; you'll get 3 (then 4, stack order).
+ 1
Oke... Thank you...
+ 1
Looks much easyer jet. Thank you!
0
Just editted my code, now it is working properly exept for a adsitional 4 at the end of it
https://code.sololearn.com/cX0fGKMp7lXh/?ref=app