Back to General discussions forum
Hello everyone. i'm fairly new to programming and have been solving a couple problems a day for more practice. i've encountered an odd error that i keep getting with my algorithm. i'm always missing the answer by 1. the last 3 trys this is what my code came up with and this is the correct answers:
arr[1] off by 1
mine 72 83 86 66 76
ans. 72 82 86 66 76
arr[6] 29 instead of 28.
mine 20 22 19 25 22 17 29 19 23 27 23 24
ans. 20 22 19 25 22 17 28 19 23 27 23 24
arr[2] 54 instead of 53.
mine 57 55 54 57 73 40
ans. 57 55 53 57 73 40
i'm a little familiar with debugging but i'm not sure an easy way to get to the bottom of a problem that has over 300 cycles in a for loop.
Any suggestions?
i was thinking about switching to Visual studio 2019 so i can learn the debugger since i hear it's pretty nice. i'm currently using vs code just because it is easy to use.
Hey leeUNAGI, welcome! I had a quick look at your code, but I'm too inexperienced in the C/C++ environment to really go through it. So from me only a few general tips that have helped me a lot over time on this site: Test your solution on smaller problems that you can solve by hand if necessary. Increase the size of the problem a little bit at a time until you find the error. This way you can see when things start to get difficult in your code. Make console outputs for each step, for example you can read out where you are in the large number array, which number is at the position and what the counter for the current number is. This way you can step through the code and check the console outputs. As soon as you notice something strange, ask why there is something strange ;) Debugging is a task that will always accompany you on this site...an experienced developer recently said to me "learning programming is not without pain" ;) I hope I could help you a little bit, try your way through and feel free to contact me if you got further - or not!
Many greetings
Thank you so much for the reply. And yeah i definitely print/cout out throughout my codes repeatedly. it really helps. for me at least. But i can try cutting the for loop down significantly and see if i can catch the error. i dont think i have any other choice at this point :)
i was kinda just hoping a vet c++ programmer would be able to give me some bjarne wisdom lol. ok here i go!
I FOUND IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
eh kind of... i see that my loop just might be missing the last number and not counting it.
ok actually it was the opposite of that. my loop was counting 1 too many... somehow...
Sorry for saying that, but I do not understand how your solution even pass the tests.
You constantly corrupting the memory by changing data in addresses not allocated by you program. May even damage your operating system.
Please, be more careful when handling pointers or choose some other language, Java or Python for example. These languages use references instead of pointers, which is more safe.
I think you saw a very early version of my solution. before i knew how large a number would be generated. i was checking the second input variable and adjusting my code to make sure that didnt happen. Dont worry, I'm not that new ;)