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

Username:   Password: 
 RegisterRegister   
 Flexible Arrays in Procedures
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Silinter




PostPosted: Sun May 25, 2008 1:30 pm   Post subject: Flexible Arrays in Procedures

Alright, here is my problem, nice and short.
I want a procedure to take in a flexible array and change it; i.e.
Turing:
procedure doThis (var arrayToUse : flexible array 1 .. * of int)
    new arrayToUse, 1
end doThis

however, it gives me a syntax error. Does Turing support taking in flexible arrays in procedures?
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sun May 25, 2008 3:13 pm   Post subject: Re: Flexible Arrays in Procedures

I don't believe so, but someone else may be able to confirm this as a fact.

You may already know that you can pass a flexible array as an argument if the parameter is a static array with an unknown upper bounds, but then during the procedure it will be treated as a static array.
code:
proc f (arr : array 1 .. * of int)
end f
var x : flexible array 1 .. 3 of int
f (x)

But again, I'm not sure of how to change bounds of an array during a procedure. You could accomplish this by creating your own array class.

What is the situation you need this for? There may be an alternative.
Silinter




PostPosted: Sun May 25, 2008 11:07 pm   Post subject: Re: Flexible Arrays in Procedures

I wanted to create a procedure to take out elements from a flexible array, so I just thought it'd be easier if I made it into a procedure.
Now what's all this talk about "array class"?
richcash




PostPosted: Mon May 26, 2008 3:22 am   Post subject: Re: Flexible Arrays in Procedures

Yeah, I don't think that can be done in turing. Unfortunately, you can't make a procedure to remove an element from an array.


But, you can make a class that represents a flexible array and solve most if not all limitations associated with flexible arrays in turing. Like this, for example :
code:
class Array
    export ini, add, remove_element, value_at
    var arr : flexible array 1 .. 0 of int
    proc ini (arr_ : array 1 .. * of int)
        new arr, upper (arr_)
        for i : 1 .. upper (arr)
            arr (i) := arr_ (i)
        end for
    end ini
    proc add (n : int)
        new arr, upper (arr) + 1
        arr (upper (arr)) := n
    end add
    proc remove_element (n : int)
        for i : n .. upper (arr) - 1
            arr (i) := arr (i + 1)
        end for
        new arr, upper (arr) - 1
    end remove_element
    fcn value_at (n : int) : int
        result arr (n)
    end value_at
end Array

You can pass an Array object as a parameter to a function, and with more methods than in my poor example, get the full functionality of a flexible array. Notice that now you don't need to make a procedure that removes an element because you can just make a method that does it for you.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: