Computer Science Canada Counter Problem |
Author: | kaotickid [ Thu Apr 24, 2008 8:19 pm ] | ||
Post subject: | Counter Problem | ||
Well I have Compsci project and I've decided to make a game. The game involves multiplication and the user must choose which answer is the right one. However, whenever I generate the numbers and where to place the numbers on a grid, unless the answer is placed on the 9th box it tells me it is out of range. I've tried changing up the code but to no avail. If anyone can suggest anything that will solve my problem I thank you ahead of time. Here is my code so far, the problem is within the loop in the for loop:
|
Author: | gitoxa [ Thu Apr 24, 2008 11:45 pm ] | ||
Post subject: | Re: Counter Problem | ||
You were making the loop a lot more complicated than it was. ![]() |
Author: | kaotickid [ Fri Apr 25, 2008 7:59 pm ] |
Post subject: | Re: Counter Problem |
Thanks gitoxa ![]() ![]() |
Author: | gitoxa [ Sat Apr 26, 2008 9:54 am ] | ||
Post subject: | Re: Counter Problem | ||
Your old code
The easiest way to see what your problem is, is to do just pick a random number for "placement" and run through your loop. Lets start with 1. ~~~~~~~~ First run through the for loop, i = 1 Since placement is 1, the real answer gets placed into the first sqaure. i = 2 answerfake(i) is placed in the (i + placement) square. i + placement = 1 + 2 = 3 There's two problems here. One, your first fake answer was the second in the array; you skipped the first. Two, you completly skipped square 2. i=3 answerfake(i) is placed in the (i + placement) square. i + placement = 1 + 3 = 4 As you can see, this trend will continue until i + placement > 9, which in this case happens to be when i = 9 i=9 x = x + 1 = 0 + 1 = 1 answerfake(i) is placed in the x-th box. Two problems, x = 1, so you're going to be putting it in the 1st sqaure, which already happens to have a number in it. Secondly, you only have 8 fake answers, you're trying to use the 9th. ~~~~~~~~~~~ To fix the answerfake problem, there was two possible solutions. You could either just create 9 fake answers, and leave it as i, or use x to keep track of how many fake numbers you've placed already. I did the latter. The next part I fixed was your elsif in the if statement. I'm not really sure where you got "i + placement" from, but what happens when placement is 8. It'll put the frist number in the first square, then it'll put the 2nd number in the xth (1) square, again. (looking at the loop again, my elsif and else do the exact same thing, oops. ![]() Then came where you were placing the numbers. Using x as a counter to keep track of where to put the numbers isn't a good idea. Not when your for loop is keeping track of all nine squares in the first place. Just use i, it'll hit all nine square, and only hit each one once. numberwherex (i+placement) i + anything will eventually result in a number greater then 9, which will result in it trying to read from a non-existant element in the variable. It also doesn't make any sense. ~~~~~~~~~~~~ All in all, the rest of the program was well put together. Good luck with the rest of it. |