Author Topic: my "for" loop is acting wonky  (Read 1427 times)

0 Members and 1 Guest are viewing this topic.

Offline Doan Sephim

my "for" loop is acting wonky
« on: December 02, 2013, 08:53:21 PM »
I have an array gCollectible[5] = (0 0 0 0 0)

Each time I get one of the collectibles I change one of the array to 1.

I want to display this in a window, so I use a variable to count how many of these are 1's. The problem is they way it counts it counts the first one once, then the second twice, then the third three times, and so on. Here is my code. What is wrong with it?

Code: [Select]
(for (= i 0) (< i 5) (++i)
                    (if(> gCollectible[i] 0)
                        ++variable
                    )
               )
If the array is [1 1 1 1 1] shouldn't variable be 5? It gives me the value 15...


Artificial Intelligence Competition

Offline gumby

Re: my "for" loop is acting wonky
« Reply #1 on: December 03, 2013, 05:18:10 PM »
Agreed, that looks like it should report back a 5.  I'm guessing right before your loop that you've initialized variable?

Code: [Select]
   = variable 0
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Doan Sephim

Re: my "for" loop is acting wonky
« Reply #2 on: December 03, 2013, 08:51:19 PM »
Agreed, that looks like it should report back a 5.  I'm guessing right before your loop that you've initialized variable?

Code: [Select]
   = variable 0

LOL! I figured it out. There is nothing wrong with the for loop. Gumby you are close to being right. I actually do set it to 0 before the code...but I did not return it to zero after the code...so every time I checked, it just kept ticking up and up and up.

Sometimes the simplest things elude me!
« Last Edit: December 03, 2013, 09:46:25 PM by Doan Sephim »
Artificial Intelligence Competition

Offline Collector

Re: my "for" loop is acting wonky
« Reply #3 on: December 03, 2013, 10:31:46 PM »
I am sure most of us have some times missed something that should have been obvious. Those are exactly the kinds of errors that have you calling yourself an idiot when you finally track it down.
KQII Remake Pic


SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.112 seconds with 23 queries.