Unable to find the mistake | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

3rd Jul 2017, 4:42 PM
Felix Felix
Felix Felix - avatar
10 Answers
+ 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.
4th Jul 2017, 4:54 AM
Kirk Schafer
Kirk Schafer - avatar
3rd Jul 2017, 4:57 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 10
It should be: if(y==4)
3rd Jul 2017, 4:48 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 9
You'll have to use nested loop, I'm going to post the modified code soon.
3rd Jul 2017, 4:53 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 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 :)
3rd Jul 2017, 10:31 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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
3rd Jul 2017, 4:48 PM
Jojo
+ 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).
4th Jul 2017, 4:38 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Oke... Thank you...
3rd Jul 2017, 4:49 PM
Felix Felix
Felix Felix - avatar
+ 1
Looks much easyer jet. Thank you!
3rd Jul 2017, 4:59 PM
Felix Felix
Felix Felix - avatar
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
4th Jul 2017, 4:00 AM
Felix Felix
Felix Felix - avatar