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

Problem set 2 question 3. Something is wrong with the "if" statement, but I can't figure out how to resolve it.

OpenStudy (anonymous):

http://pastebin.com/fdCzQyAE

OpenStudy (anonymous):

Unknown past ID for me, it didn't show the code :-/

OpenStudy (anonymous):

http://pastebin.com/QYB3iAGz

OpenStudy (anonymous):

Sorry!

OpenStudy (anonymous):

A couple of comments. In your else statement, the numValid figure is reset to 0 each time it goes through. The program runs through all the for loops before it returns to check your while condition. The way your program is currently set up, it checks through all the loops, the numValid is either 0 or 1 (as it keeps cancelling the addition in the else loop) and then because it is less than 6, goes through all the loops again, checks condition etc.

OpenStudy (anonymous):

Thought more on the problem. The above is not really the trouble. I reaslised you wanted 6 in a row... The numValid is reset, but not for every time a testno is not valid. it is reset every single time any permeation of testno is found not valid. So for the number 6, it is found not valid 7 times before it finds a correct format of nuggets. Then numValid is 1. Then number 9 is found valid, but several different types of nuggets find it not valid, so it goes back to 0...

OpenStudy (anonymous):

Eg, for 9, it finds that the combo 0 6packs and 1 9pack and 0 20packs makes 9. Then in the next loop, 0 6packs and 1 9pack and 1 20 pack does not make 9. numValid is reset to 0.

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!