array counter problem

Back to General discussions forum

leeUNAGI     2021-08-02 23:09:14

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.

TimeToMoveOn     2021-08-03 11:09:33

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

leeUNAGI     2021-08-04 03:00:55

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!

leeUNAGI     2021-08-04 03:37:18

I FOUND IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

leeUNAGI     2021-08-04 03:38:44

eh kind of... i see that my loop just might be missing the last number and not counting it.

leeUNAGI     2021-08-04 03:43:07

ok actually it was the opposite of that. my loop was counting 1 too many... somehow...

qwerty     2021-08-04 16:16:22

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.

leeUNAGI     2021-08-11 05:07:41

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 ;)

Please login and solve 5 problems to be able to post at forum