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

Username:   Password: 
 RegisterRegister   
 Help with flexible arrays and procedures
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
battfel




PostPosted: Tue Jan 11, 2022 10:17 am   Post subject: Help with flexible arrays and procedures

What is it you are trying to achieve?
I am wondering If there is any way to use flexible arrays as a parameter in a procedure, so that I can modify the array within the procedure.


What is the problem you are having?
So far, it doesn't seem like this is possible. It might just be that Turing doesn't support it.


Describe what you have tried to solve this problem
I tried declaring the procedure like :
Turing:


procedure bulletmovements (var a : flexible array 1..* of bulletdata)



And I tried declaring the procedure with a normal array as a parameter, which does work, but then functions like 'upper()' won't work within it. I looked around the forums a bit and couldn't find much about this problem. My code has two long for loop sections that are practically the same other than different flexible array variables throughout, so it would have been nice to just condense it. As you'll see by the way, they are flexible arrays of a custom type, which I don't know if that matters, it probably shouldn't.
Turing:


for i : 1 .. upper (bluebullet)
        if bluebullet (i).x - bluebullet (i).xvel > maxx - 3 then
            bluebullet (i).xvel *= -1
            bluebullet (i).yvel *= -1
        elsif bluebullet (i).x - bluebullet (i).xvel < 3 then
            bluebullet (i).xvel *= -1
            bluebullet (i).yvel *= -1
        end if
        if bluebullet (i).y + bluebullet (i).yvel > maxy - 3 then
            bluebullet (i).xvel *= -1
            bluebullet (i).yvel *= -1
        elsif bluebullet (i).y + bluebullet (i).yvel < 3 then
            bluebullet (i).xvel *= -1
            bluebullet (i).yvel *= -1
        end if
        if bluebullet (i).timealive > 333 then
            for j : i .. upper (bluebullet) - 1
                bluebullet (j).x := bluebullet (j + 1).x
                bluebullet (j).y := bluebullet (j + 1).y
                bluebullet (j).xvel := bluebullet (j + 1).xvel
                bluebullet (j).yvel := bluebullet (j + 1).yvel
                bluebullet (j).timealive := bluebullet (j + 1).timealive
            end for
            new bluebullet, upper (bluebullet) - 1
            exit
        else
            bluebullet (i).x -= bluebullet (i).xvel
            bluebullet (i).y += bluebullet (i).yvel
            bluebullet (i).timealive += 1
        end if
        if Math.Distance (bluebullet(i).x, bluebullet(i).y, rx + 35, ry + 35) < 15 then
            reddeath := true
            gameover := true
            for j : i .. upper (bluebullet) - 1
                bluebullet (j).x := bluebullet (j + 1).x
                bluebullet (j).y := bluebullet (j + 1).y
                bluebullet (j).xvel := bluebullet (j + 1).xvel
                bluebullet (j).yvel := bluebullet (j + 1).yvel
                bluebullet (j).timealive := bluebullet (j + 1).timealive
            end for
            new bluebullet, upper (bluebullet) - 1
            exit
        end if
    end for
    for i : 1 .. upper (redbullet)
        if redbullet (i).x - redbullet (i).xvel > maxx - 3 then
            redbullet (i).xvel *= -1
            redbullet (i).yvel *= -1
        elsif redbullet (i).x - redbullet (i).xvel < 3 then
            redbullet (i).xvel *= -1
            redbullet (i).yvel *= -1
        end if
        if redbullet (i).y + redbullet (i).yvel > maxy - 3 then
            redbullet (i).xvel *= -1
            redbullet (i).yvel *= -1
        elsif redbullet (i).y + redbullet (i).yvel < 3 then
            redbullet (i).xvel *= -1
            redbullet (i).yvel *= -1
        end if
        if redbullet (i).timealive > 333 then
            for j : i .. upper (redbullet) - 1
                redbullet (j).x := redbullet (j + 1).x
                redbullet (j).y := redbullet (j + 1).y
                redbullet (j).xvel := redbullet (j + 1).xvel
                redbullet (j).yvel := redbullet (j + 1).yvel
                redbullet (j).timealive := redbullet (j + 1).timealive
            end for
            new redbullet, upper (redbullet) - 1
            exit
        else
            redbullet (i).x -= redbullet (i).xvel
            redbullet (i).y += redbullet (i).yvel
            redbullet (i).timealive += 1
        end if
        if Math.Distance (redbullet(i).x, redbullet(i).y, x + 35, y + 35) < 15 then
            bluedeath := true
            gameover := true
            for j : i .. upper (redbullet) - 1
                redbullet (j).x := redbullet (j + 1).x
                redbullet (j).y := redbullet (j + 1).y
                redbullet (j).xvel := redbullet (j + 1).xvel
                redbullet (j).yvel := redbullet (j + 1).yvel
                redbullet (j).timealive := redbullet (j + 1).timealive
            end for
            new redbullet, upper (redbullet) - 1
            exit
        end if
    end for


Maybe there is a workaround or something. It's not a huge deal either to just leave the code as is, I'm more curious as to if it is possible to do this
[size=14]Please specify what version of Turing you are using

4.1.1
Sponsor
Sponsor
Sponsor
sponsor
scholarlytutor




PostPosted: Fri Jan 14, 2022 5:33 pm   Post subject: RE:Help with flexible arrays and procedures

According to the documentation:

"There is no flexible array parameter type, although a flexible array can be passed to an array parameter with “*” as the upper bound."
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 1  [ 2 Posts ]
Jump to:   


Style:  
Search: