Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Flexible Arrays
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Wed Aug 23, 2006 10:54 pm   Post subject: Flexible Arrays

Using a flexible array how would I eliminate an element that is NOT the last element in the array?
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Wed Aug 23, 2006 10:58 pm   Post subject: (No subject)

Swap it with the last element in the array, and then remove the last element.
Mr. T




PostPosted: Wed Aug 23, 2006 11:04 pm   Post subject: Alex's Opinion

Is this what you mean by swapping?
thing (3):= thing (upper (thing))
If not, how do you swap?
[Gandalf]




PostPosted: Wed Aug 23, 2006 11:11 pm   Post subject: (No subject)

That's one part of swapping... But the whole process normally looks more like this:
code:
tempThing = thingB
thingB = thingA
thingA = tempThing


For your purposes, though, what you have is enough, since you don't really care about what the last element is when you delete it.
Mr. T




PostPosted: Wed Aug 23, 2006 11:42 pm   Post subject: Alex's Opinion

I did what you suggested, but I'm still getting an array subscript out of range error. I don't want to post my entire code just yet, so can I PM it to you?
[Gandalf]




PostPosted: Thu Aug 24, 2006 12:21 am   Post subject: Re: Alex's Opinion

Mr. T wrote:
I don't want to post my entire code just yet

Why not?
Mr. T




PostPosted: Thu Aug 24, 2006 12:23 am   Post subject: Alex's Opinion

"theft" Rolling Eyes
[Gandalf]




PostPosted: Thu Aug 24, 2006 12:31 am   Post subject: (No subject)

Fine, if you really believe that is a problem...

Other people manage, even without posting their entire code. It's probably better that way too, makes it easier to help you, though occassionally it comes in handy to see the whole thing. Post only the code relevant to your problem. That way, you don't have to be worried about someone stealing your program, and we can still try and help you find the problem.

Private messages are simply not the place for help discussion, especially when you spam the same message to multiple people. That's what the help sections are for.
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Thu Aug 24, 2006 12:37 am   Post subject: Alex's Opinion

If I only post the relevant code, you won't be able to run the program for visual aid. Anyways, heres the section:
(the error "subscript array is out of range" occurs at this line --> item (i).check := allowableBoundries (i)
code:
type items :
    record
        x, y : real
        size : real
        vy : real %%how fast it will fall
        check : boolean %%%%%oooooooooo!!!!!
        %%can have more, like color, etc:
    end record

var item : flexible array 1 .. 15 of items
var itemX, itemY : array 1 .. 15 * 30 of int
var positionInItemArray : int := 1
var randomItemLocation : int := 0
proc itemInitialization
    for a : 1 .. 15
        for b : 1 .. 30
            if grid (a, b) not= 0 then
                itemX (positionInItemArray) := a
                itemY (positionInItemArray) := b
                positionInItemArray += 1
            end if
        end for
    end for

    for i : 1 .. upper (item)
        item (i).size := 2
        randomItemLocation := Rand.Int (1, positionInItemArray - 1)
        item (i).x := itemX (randomItemLocation)
        item (i).y := itemY (randomItemLocation)
        item (i).vy := -0.25
    end for
end itemInitialization


fcn allowableBoundries (num : int) : boolean
    if grid (round (item (num).x), round (item (num).y)) not= white then
        result false
    else
        result true
    end if
end allowableBoundries

%purpose of this array = once item starts falling, it WON't stop (even if it hits another brick)
var continueMove : flexible array 1 .. upper (item) of boolean
for i : 1 .. upper (item)
    continueMove (i) := false
end for

var move : int := 0
proc moveItems
    for i : 1 .. upper (item)

        item (i).check := allowableBoundries (i)

        if item (i).check = true then             %%not collided
            continueMove (i) := true
        end if

        if continueMove (i) = true then
            item (i).y += item (i).vy             %%move it down
        end if

        %item hits the paddle
        if Math.DistancePointLine ((((item (i).x - 1) * 20) + 10) + item (i).size, (((item (i).y - 1) * 10) + 5) + item (i).size, (maxx div 2 - 20) + move, 8, (maxx
                div 2 + 27) + move, 8) <= item (i).size then
            moneySystem
            item (i).x := item (upper (item)).x
            item (i).y := item (upper (item)).y
            item (i).size := item (upper (item)).size
            item (i).vy := item (upper (item)).vy
            item (i).check := item (upper (item)).check
            new item, upper (item) - 1
        end if

        %item reaches the bottom of the screen
        if (((item (i).y - 1) * 10) + 5) + item (i).size <= 0 then
            item (i).x := item (upper (item)).x
            item (i).y := item (upper (item)).y
            item (i).size := item (upper (item)).size
            item (i).vy := item (upper (item)).vy
            item (i).check := item (upper (item)).check
            new item, upper (item) - 1
        end if

    end for
end moveItems

var picLargeItem : int := Pic.FileNew ("Large.GIF")
var picSmallItem : int := Pic.FileNew ("Small.GIF")
var randItem : flexible array 1 .. upper (item) of int
for i : 1 .. upper (item)
    randint (randItem (i), 1, 2)
end for
proc drawItems
    for i : 1 .. upper (item)
        if randItem (i) = 1 then
            %Draw.FillOval (((round (item (i).x) - 1) * 20) + 10, (((round (item (i).y) - 1) * 10) + 5), round (item (i).size), round (item (i).size), black)
            Pic.Draw (picLargeItem, ((round (item (i).x) - 1) * 20) + 6, ((round (item (i).y) - 1) * 10) + 1, picMerge)
        elsif randItem (i) = 2 then
            Pic.Draw (picSmallItem, ((round (item (i).x) - 1) * 20) + 6, ((round (item (i).y) - 1) * 10) + 1, picMerge)
        end if

    end for
end drawItems
[Gandalf]




PostPosted: Thu Aug 24, 2006 12:57 am   Post subject: Re: Alex's Opinion

Mr. T wrote:
If I only post the relevant code, you won't be able to run the program for visual aid.

Yes, but it also means we won't have to sift through hundreds of lines of who knows what kind of code. If you narrow down a problem enough, it'll be a lot easier for us (or you) to find it. Though, like I said, having the complete code is also useful in some situations. You should also post what you have done to try to fix the problem, and even what you think is causing it. We shouldn't be doing all the work for you.

As for your error: In your code you are iterating through each of the items; from 1 to the amount of items at the time of the first iteration of the for loop. The problem is that you are changing the upper bound of the items inside the for loop, so the original amount of items no longer applies. Let's say you delete one item in that loop, the amount of items in now one less than it originally was, but Turing still expects the original amount of items.

Problems like this are fixed by compartmentalizing your code further, into, say checkBoundaries() and deleteItems().
Mr. T




PostPosted: Thu Aug 24, 2006 1:07 am   Post subject: Alex's Opinion

I thought that maybe an exit statement right after the array update, would solve the problem. I know longer get the out of range error, but now, it seems that the entire array has shifted, and a new item is falling in the place of the item just removed from the array. Any other suggestions how can update the array?
code:
var move : int := 0
proc moveItems
    for i : 1 .. upper (item)

        item (i).check := allowableBoundries (i)

        if item (i).check = true then             %%not collided
            continueMove (i) := true
        end if

        if continueMove (i) = true then
            item (i).y += item (i).vy             %%move it down
        end if

        %item hits the paddle
        if Math.DistancePointLine ((((item (i).x - 1) * 20) + 10) + item (i).size, (((item (i).y - 1) * 10) + 5) + item (i).size, (maxx div 2 - 20) + move, 8, (maxx
                div 2 + 27) + move, 8) <= item (i).size then
            moneySystem
            item (i).x := item (upper (item)).x
            item (i).y := item (upper (item)).y
            item (i).size := item (upper (item)).size
            item (i).vy := item (upper (item)).vy
            item (i).check := item (upper (item)).check
            new item, upper (item) - 1
            exit
        end if

        %item reaches the bottom of the screen
        if (((item (i).y - 1) * 10) + 5) + item (i).size <= 0 then
            item (i).x := item (upper (item)).x
            item (i).y := item (upper (item)).y
            item (i).size := item (upper (item)).size
            item (i).vy := item (upper (item)).vy
            item (i).check := item (upper (item)).check
            new item, upper (item) - 1
            exit
        end if

    end for
end moveItems
Cervantes




PostPosted: Thu Aug 24, 2006 7:39 am   Post subject: (No subject)

Read!
Clayton




PostPosted: Thu Aug 24, 2006 9:55 am   Post subject: (No subject)

damn Cervantes i was going to hotlink that, but apparently you get up earlier than me Confused

now on topic:

theres one part in your code here that looks a bit weird:

Turing:

if item (i).check = true then             %%not collided
    continueMove (i) := true
end if

if continueMove (i) = true then
    item (i).y += item (i).vy             %%move it down
end if


why not just move the item (i).y += item (i).vy into the first if? all your doing is making continue move true in the first one, the next thing you do is if continueMove is true is move, so you can just move that line into the first if, and be done with the second one Wink
Mr. T




PostPosted: Thu Aug 24, 2006 3:29 pm   Post subject: Alex's Opinion

SuperFreak82, I know its hard to see the reason why I coded it like that. But in the game as a whole, it is neccessary. (ie. once a ball starts falling, it won't stop, even if something else is in the way)
Mr. T




PostPosted: Thu Aug 24, 2006 3:58 pm   Post subject: Alex's Opinion

Cervantes, I used that tutorial, and it helped eliminate the error. However, there's still a problem. It seems that another item simply takes the place of the deleted item. By this I mean that the new item falls down as if it were the original item. Can I PM someone my code so I can show what I mean.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: