Computer Science Canada Accumulators won't work |
Author: | dc116 [ Thu Mar 12, 2009 8:47 pm ] | ||
Post subject: | Accumulators won't work | ||
I'm making a quiz, in which it will only allow the user to have 3 guesses. If it took the user one guess to input the correct answer, the user will gain 3 marks. If it took the user two guesses to input the correct answer then the user will gain 2 marks. If the user took all 3 guesses to input the correct answer, he/she will only receive one mark. Otherwise, the user will receive no marks. The problem is, when the user is doing the quiz, the accumulators (variables mark and correct) are not working. In the end, no matter how many questions you answered correctly, the report card will display your mark as 0. You will see what I mean by running the program.
Any help would be greatly appreciated. |
Author: | Tony [ Thu Mar 12, 2009 8:53 pm ] |
Post subject: | RE:Accumulators won\'t work |
are you familiar with scope? http://en.wikipedia.org/wiki/Scope_(programming) |
Author: | dc116 [ Thu Mar 12, 2009 8:58 pm ] |
Post subject: | RE:Accumulators won\'t work |
No I really have no idea what scope is. How does scope relate to my program? |
Author: | Tony [ Thu Mar 12, 2009 9:03 pm ] |
Post subject: | RE:Accumulators won\'t work |
you have a problem with it. |
Author: | dc116 [ Thu Mar 12, 2009 9:28 pm ] |
Post subject: | RE:Accumulators won\'t work |
Can you give me more hints? because I have no idea what scope is in turing (there are no such examples on wikipedia) |
Author: | Tony [ Thu Mar 12, 2009 9:44 pm ] |
Post subject: | RE:Accumulators won\'t work |
it works the same way. Now that I've actually tried to run your program, Turing actually gives a warning before executing. Looks like there are more problems in there. Either way, keep track of how the value of mark variable changes, as the program is running. |
Author: | dc116 [ Thu Mar 12, 2009 9:56 pm ] |
Post subject: | Re: Accumulators won't work |
Is it because that every time the "ask" procedure is used, another "mark" variable is declared? |
Author: | Tony [ Thu Mar 12, 2009 10:05 pm ] |
Post subject: | RE:Accumulators won\'t work |
that is a partial answer. What else can you say about the "mark" variable? |
Author: | dc116 [ Thu Mar 12, 2009 10:08 pm ] |
Post subject: | RE:Accumulators won\'t work |
Perhaps it is used improperly? E.g. accumulators are not supposed to be used within procedures? |
Author: | Tony [ Thu Mar 12, 2009 10:20 pm ] |
Post subject: | RE:Accumulators won\'t work |
what value is it initialized to? How (and under what conditions) does it increment? |
Author: | dc116 [ Thu Mar 12, 2009 10:24 pm ] |
Post subject: | RE:Accumulators won\'t work |
Well, in the beginning I set "mark" to zero. For each "ask" procedure, mark increases from between 0-3 depending on the correctness of the answer. So is my procedure done improperly? |
Author: | Tony [ Thu Mar 12, 2009 10:30 pm ] |
Post subject: | Re: RE:Accumulators won\'t work |
dc116 @ Thu Mar 12, 2009 10:24 pm wrote: mark increases from between 0-3
which "mark"? dc116 @ Thu Mar 12, 2009 9:56 pm wrote: Is it because that every time the "ask" procedure is used, another "mark" variable is declared?
also the increase depends on more than just the correctness of the answer. What conditions must be true for that line to execute? |
Author: | dc116 [ Thu Mar 12, 2009 10:38 pm ] |
Post subject: | RE:Accumulators won\'t work |
Ohhhh I see. Since mark += (4-i) is the same as mark := mark + (4-i), but every time the ask procedure is used, another mark variable is declared and the previous values all disappear. Is that somewhat along the lines of my problem? |
Author: | Tony [ Thu Mar 12, 2009 10:41 pm ] |
Post subject: | RE:Accumulators won\'t work |
Yes, you are correct. Though you have another problem as well -- your program never gets to the mark += (4-i) line; there's something that makes it skip a chunk of code, and force the flow of execution to go elsewhere. |
Author: | dc116 [ Fri Mar 13, 2009 3:43 pm ] | ||||
Post subject: | RE:Accumulators won\'t work | ||||
So any ideas on how to use an accumulator to actually add the marks properly? How do I accumulate outside a procedure? EDIT: Never mind... I figured out that the "exit" has to go after the "mark += (4-i)",
... and also that I have to declare the variables "mark" and "correct" in a different way
Thanks for all those who helped. ![]() |