+ 2

I need help for fixing a problem

I created a program for the Mars rover challenge, and created a second version for random input. The problem is in the second program, and i dont know where it actually is. I dont want a complete solution for this, i want to fix it myself, but i need help by finding the problem and maybe an approach for fixing it. The description for the problem itself is in the program in the space where i think this bug is created (line 29, where the commands are given a random value). Thanks in advantage. https://code.sololearn.com/caMkH3Pk6L5t/?ref=app

20th Dec 2018, 10:25 AM
Karl G.
Karl G. - avatar
6 Antworten
+ 3
Hi Karl G. - Apologies for not seeing this sooner. I've noticed a few issues in the code. 1. The variable `x` in the `for` loop on line 51 will be reset to `9` in the `for` loop on line 64 when the input contains "MOV". The reason is both `for` loops share the same variable `x`. The `for` loop on line 64 changes `x` to `4` and increments until `x < input.Length`. When input is something like "MOV F 12", `x` will increment from `4` to `8` and then increment to `9` on the next loop on line 51. Fix: Declare a different variable like `y` on line 64 and replace all references of `x` to `y` in the for loop block. 2. Line 123: When rockStash is 5, an Index Out of Range Exception will occur on Rock[rockStash, 1] because the array Rock was initialized as Rock[5, 3]. Fix: Modify line 115 to use the expression: (rockStash + 1 > 4) instead of (rockStash + 1 > 5) I've applied these fixes to this code in case you need something to reference: - https://code.sololearn.com/cbqQQ05tMDX3
21st Dec 2018, 8:52 AM
David Carroll
David Carroll - avatar
+ 2
Your code works well except for MOV and TRN The problem is with directionRandom[0,random.Next(1)] directionRandom[1, random.Next(1)] Write a test code on how to address the elements in nested array, and then you can know how to fix it. 😉 (by the way, i like the POS command and the RLS A option)
20th Dec 2018, 12:19 PM
Gordon
Gordon - avatar
+ 2
David Carroll is good at C# He is kind and will help you when he is free and comes into this Q&A 😉
20th Dec 2018, 12:41 PM
Gordon
Gordon - avatar
+ 1
Thanks a lot, i will rework my Code to fix those problems
21st Dec 2018, 8:55 AM
Karl G.
Karl G. - avatar
0
Well, thanks a lot, ill try it
20th Dec 2018, 12:37 PM
Karl G.
Karl G. - avatar
0
Wait, I just tried, I was wrong😅 Sorry. https://code.sololearn.com/cqN63P7veoK7/?ref=app
20th Dec 2018, 12:38 PM
Gordon
Gordon - avatar