Arrays With Multiple Amount Values in Procedures or Functions
Author |
Message |
Linkxgl
|
Posted: Wed Dec 14, 2011 12:20 pm Post subject: Arrays With Multiple Amount Values in Procedures or Functions |
|
|
I'm trying to make function that you can input an array of any length and output an array of any length. I know the reason you have to specify the length of the array is because you to tell the computer how much memory the array will be using so it can make space for it, but if you're using an array in a function, you've probably already declared the array's length anyways so you should be able to use the array in the length.
I was trying to do this:
code: |
function do(ar:array 0..* of int):array 0..* of int
var ar:array 0..* of int:=ar
result ar
end do
|
I think I've seen this done before, but I don't know how to go about applying the usage, where * any integer.
Thanks!
Linkxgl |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Wed Dec 14, 2011 2:51 pm Post subject: Re: Arrays With Multiple Amount Values in Procedures or Functions |
|
|
I don't think that what you're trying to do actually works in Turing. Turing always wants to know how big its arrays are, even when using 0 .. * as the bounds. You can pass parameters of unknown size and that works because, as you said, the array is already declared. But you cannot create a function that returns an array of an unknown size since Turing demands to know the size of the array at compile time.
I'm not sure what the practical uses of this "array identity" function would be. It may be that whatever you want to use this for can be done differently. |
|
|
|
|
|
Linkxgl
|
Posted: Wed Dec 14, 2011 3:50 pm Post subject: RE:Arrays With Multiple Amount Values in Procedures or Functions |
|
|
Hmmm, yeah I didn't think I would be able to do that, but I was thinking something like this:
code: |
function do(x:array 0..* of int,i:int):array 0..i of int
%function body
end do
|
Basically what I'm trying to do is have the user specify two pixels on Turing, then I want to know the pixel co-ordinates between them (using linear systems), and putting them all in an array. Is there a better way to do this? |
|
|
|
|
|
Raknarg
|
Posted: Wed Dec 14, 2011 4:41 pm Post subject: RE:Arrays With Multiple Amount Values in Procedures or Functions |
|
|
For a functions and procedures the input and output can be unknown, so you can use array 1 .. * of int for both. However, you can't use it in a variable, like you did for for the first example.
What you want will work fine.
@Dreadnaught have you tried it before? If it didnt work for you, you may have just used it incorrectly. |
|
|
|
|
|
Linkxgl
|
Posted: Wed Dec 14, 2011 5:45 pm Post subject: RE:Arrays With Multiple Amount Values in Procedures or Functions |
|
|
How about you try it? It doesn't seem to work for me... Could you post an example code? |
|
|
|
|
|
Dreadnought
|
Posted: Wed Dec 14, 2011 8:26 pm Post subject: Re: Arrays With Multiple Amount Values in Procedures or Functions |
|
|
@Raknarg According to the Turing documentation, when you declare an array with an upper bound using * the upper bound is computed from the count of the initializing values and must have an initialization list. The result of evaluating a function wouldn't have such a list.
Just try running Turing: | fcn F () : array 1 .. * of int
end F |
As for the variable declaration you can use *, but again you need an initialization list (using init). There is however a way to do declare an array dynamically the way Linkxgl wanted (sort of). Declare with var y:array 1..upper(x) of int, then use a for loop to get all the values in the new array.
But as for Linkxgl, if you want to make array of any size on the fly, try looking up flexible arrays. You can re-size the upper bound at any time. Only drawback is that if you pass it as a parameter it will become a static array in the body of that specific function or procedure (but you can also use it globally). Flexible arrays might be exactly what you're looking for.
Also, if it is possible to make functions that produce arrays of dynamic size, please let me know. There might be some cool stuff to do with that. |
|
|
|
|
|
Tony
|
Posted: Wed Dec 14, 2011 8:54 pm Post subject: RE:Arrays With Multiple Amount Values in Procedures or Functions |
|
|
you could always return a pointer to a (flexible?) array. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Linkxgl
|
Posted: Wed Dec 14, 2011 9:21 pm Post subject: RE:Arrays With Multiple Amount Values in Procedures or Functions |
|
|
I mean, what I want to do can be easily done with out a function, but I'm doing this because same idea is used throughout my script, so instead of re-writing code, all I would have to do is make one function and use it as many times as I wanted! Then again, seems like no one really knows how to actually do this... Flexible arrays have proven to me to be super useful, I guess I'll have to side with them for now... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|