GameMaker Can't get for loop to execute at all

C

Chungsie

Guest
In my project, I have a room, called, room0 and an object called object0

Code:
object0 initializes
cards = array_create(0,0)
cards = [9,8,7,2,3,4]
i = 0
j = 0
key = 0
complete1 =0
complete2 = 0
and in step event it does
Code:
for (j = 2; j >= array_length_1d(cards); j += 1)
    {
        if complete2 == 0 complete2 = .5
        key = cards[j]
        i = j - 1
        while (i > 0 and cards[i] > key)
        {
            cards[i - 1] = cards[i]
            i -= 1
            if complete1 == 0 complete1 = 1
        }
        cards[i + 1] = cards[i]
        if j == array_length_1d(cards) complete2 = 1
    }
the for loop never runs, as verified by the values of complete1 and complete2 and the fact the array is printed exactly as it was initialized.

I've tried making a script.

It does not work. How will I do my homework, if my assignment is to take pseudocode and write a insertion_sort function for a given array, as if dealing with cards?
 

Binsk

Member
You are telling it not to run so it doesn't run.

It is told to run while j is larger than the array which is never true. I assume that is not what you wanted.

Double check your for loop conditional logic.
 

obscene

Member
for (j = 2; j >= array_length_1d(cards); j += 1)

should be...

for (j = 2; j <= array_length_1d(cards); j += 1)

?
 
C

Chungsie

Guest
ok thanks guys. my issue seems to be it's not a working mock up of the pseudocode.
Code:
##Insertion sort
#for j = 2 to A.length
  #key = A[j]
  #insert A[j] into the sorted sequence A[1..j-1]
  #i = j -1
  #while i > 0 and A[i] > key
    #A[i+1] = A[i]
    #i = i -1
#A[i+1] = key
going to continue at it

Code:
while (j <= count)
    {
        key = cards[j]   
        i = j
        while (i > 0 and cards[i-1] > key)
            {
                cards[i] = cards[i-1]
                i -= 1
            }
        cards[i] = key
        j += 1
    }
it works now. thanksman.PNG where cards = [32,12,18,1,5]

thanks guys.
 
Last edited by a moderator:
Top