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

Username:   Password: 
 RegisterRegister   
 Making an Array fit into a Procedure Nice and Snug
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bacchus




PostPosted: Thu Jun 09, 2005 9:11 pm   Post subject: Making an Array fit into a Procedure Nice and Snug

Ok, my problem is I want to know if theres a way to make a procedure accept the lower bounds of an array.
Ex)
code:
proc bar (foo : array 2 .. * of int)
    for i : lower (foo) .. upper (foo)
        put i
    end for
end bar

var foo : array 2 .. 5 of int

bar (foo)
I don't want that procedure to be hard-coded in that way that it will only accept arrays that start with 2. I want it to accept any array no matter what the lower bounds are. Kind of how '*' marks the end of the array, is there a similar one for the beginning? I'm afraid I haven't looking in the Turing Reference yet (I accually did a quick little look), but I have never seen anyone try it before and I'm a bit tired at the current moment. Any advice would be apprieciated.
Sponsor
Sponsor
Sponsor
sponsor
lyam_kaskade




PostPosted: Thu Jun 09, 2005 9:48 pm   Post subject: (No subject)

Hmmm...you could try something like

code:

proc bar (foo : array 1 .. * of int, lower:int)
    var list:array lower..upper (foo)+lower of int

    for j:1..upper (foo)
       list (j+lower):= foo (j)
    end for

    for i : lower (list) .. upper (list)
        put i
    end for
end bar

var foo : array 2 .. 5 of int

bar (foo, 2)


Although to me that seems needlessly complicated somehow. Best I can think of for now though. Maybe something with a flexible array would work...

EDIT:
Furthermore, I found this in the reference:

Quote:

In the declaration of an array parameter, the upper bound can be given as an asterisk (*), as is done in the above example.


and

Quote:

Since the lower bound is necessarily known at compile time, lower is rarely used.


The first thing doesn't mention lower bound at all, and the second thing says "necessarily". Err...if that helps.
Bacchus




PostPosted: Thu Jun 09, 2005 10:05 pm   Post subject: (No subject)

Ya I know, but have you also tested your code there? I tried many things before including one similar to that. What I found was that it gives you an error when you try to put an array with the lower bounds that are different than the lower bounds in the parameter's array. Also with your code, I got the error: "'lower' cannot have subscripts"
lyam_kaskade




PostPosted: Thu Jun 09, 2005 10:13 pm   Post subject: (No subject)

Oops...yeah the "lower" parameter should have a different name, because that's a keyword. It gets rid of the subscript problem, but it still say compile time expression expected, so that's a bust. I guess there's no way that I know of to do it. Sorry.
Delos




PostPosted: Fri Jun 10, 2005 9:24 am   Post subject: (No subject)

The lower bound of an array must be known at compile time. There's nothing more to it.
I'm not sure why'd you want to have an array that starts and ends at variable positions - you could just use a flexible array...
This, IMHO, is actually a good thing, since it reduces the possibility of creating a situation where the bounds of the array get confused needlessly.
Bacchus




PostPosted: Fri Jun 10, 2005 11:37 pm   Post subject: (No subject)

The problem was if I wanted to Import the array into a procedure through the procedures parameters. But I guess I must hard-code the procedure and array. Sad
jamonathin




PostPosted: Sat Jun 11, 2005 6:58 am   Post subject: (No subject)

Why dont you cheat. Im not using turing right now so it may not work, but here

code:
proc bar (numb :int, foo : array 2 .. * of int)
    for i : numb .. upper (foo)
        put i
    end for
end bar

var foo : array 2 .. 5 of int
var hold :int := lower(foo) %or just 2
bar (hold, foo)

Just having something represnt the lower(foo)?
Cervantes




PostPosted: Sat Jun 11, 2005 8:00 am   Post subject: (No subject)

That does not work. It only works if the lower bounds of both the actual and the formal parameters are the same.
I was thinking you could do something like this, but it doesn't work. I'll post it anyways, in case it gives anyone a brainstorm:
code:

var foo : array 2 .. 5 of int
for i : lower (foo) .. upper (foo)
    foo (i) := i
end for

proc bar (foobar : array 0 .. * of int)
    for i : lower (foo) .. upper (foo)
        if i >= lower (foo) then
            put i
        end if
    end for
end bar

bar (foo)

This assumes that your array will never have a lower bounds less than zero. But, it also does not work. Hopefully someone can extend it, because I'm stuck. Confused
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Sat Jun 11, 2005 5:42 pm   Post subject: (No subject)

But what if we were to change it so that . . .
code:

proc bar (low, high :int, foo : array 0 .. * of int)
    for i : low .. high
        put i
    end for
end bar

var foo : array 2 .. 5 of int
var hold : array 1 .. 2 of int := init (lower (foo), upper (foo)) 
bar (hold (1), hold (2), foo)


Unless you can var an array into the negatives, such as.
code:
var foo : array -1 .. 1 of jamonathin

I've never tried it, and i can barely think of any scenerio's where it would make sense, if any.
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  [ 9 Posts ]
Jump to:   


Style:  
Search: