Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (anonymous):

Hey, I'm having problems debugging my PS2. I think my code should work but for some reason it goes into an infinite loop. I was wondering if anyone can help me locate the rpoblem. Thanks This is my code: http://dpaste.com/569869/

OpenStudy (anonymous):

What's your initial abc? Did you initialize them?

OpenStudy (anonymous):

I thought you didn't need to initialize a, b, c if you use them that way (in the for loops)

OpenStudy (anonymous):

Oh sorry. No, you don't. I should read that again... :P

OpenStudy (anonymous):

Ah, I see your problem now. Your code couldn't get out of that while loop cuz your count never increases. The reason? n is never equal to 6*a + 9*b + 20*c.

OpenStudy (anonymous):

Wouldn't n be equal to 6a+9b+20c when n is a feasible number of nuggets?

OpenStudy (anonymous):

Oh wait I think I know what you mean. My count resets back to 0 because I'm still going through the loops even after I get the feasible answer.

OpenStudy (anonymous):

Okay, so let's try this. In my simulation, 55 is a feasible number, since it gives you two 20s, one 6s, and one 9s pack. And it can't be no other way, right? But consider 58 for instance. You can get 58 by buying 2 20s packs, 3 6s packs, and none of the 9s packs. So that would be 2(20) + 3(6) + 0(9) = 58. However, if I say that you get 58 by buying 2 20s packs, 2 9s packs, and none of the 6s packs, I'm also right. In this case, your equation would be n == 6*3 + 2*9 + 2*20. n being 58, that test would be false. It is cases like these that makes (among others) your count never increases (since it would only reset it back to 0). THAT, and I suspect something else...

OpenStudy (anonymous):

Thanks, I figured out what was wrong

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!