Can functions return arrays?
Author |
Message |
Diablo117
|
Posted: Tue May 26, 2009 3:57 pm Post subject: Can functions return arrays? |
|
|
What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
What is the problem you are having?
<Answer Here>
Describe what you have tried to solve this problem
<Answer Here>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
<Add your code here>
|
Please specify what version of Turing you are using
<Answer Here> |
|
|
|
|
|
Sponsor Sponsor
|
|
|
corriep
|
Posted: Tue May 26, 2009 4:06 pm Post subject: RE:Can functions return arrays? |
|
|
looks like someone pressed submit instead of preview;)
And yes, functions can return arrays but not flexible arrays. |
|
|
|
|
|
Kharybdis
|
Posted: Tue May 26, 2009 4:29 pm Post subject: RE:Can functions return arrays? |
|
|
diablo does your name start with D? |
|
|
|
|
|
Diablo117
|
Posted: Tue May 26, 2009 8:59 pm Post subject: Re: Can functions return arrays? |
|
|
Oh wow, I feel stupid now...
thats the sort im trying to make into a function, looks strange because I was tinkering with it.
Turing: | function Sort (asize : int, anums : array 1 .. * of int) : array 1 .. 1000 of int
var size : int := asize
var nums : array 1 .. size of int
var temp : int
for s : 1 .. size
if s <= size - 1 then
if nums (s) > nums (s + 1) then
temp := nums (s + 1)
nums (s + 1) := nums (s)
nums (s) := temp
end if
end if
end for
result nums
end Sort
Heres where im using it in my main
import MathMod
var topnum : int
var nums : array 1 .. 5 of int
for i : 1 .. 5
nums(i) := Rand.Int(1,100)
end for
nums(1):= MathMod.Sort(5,nums) |
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="turing"]Code Here[/syntax] |
|
|
|
|
|
|
corriep
|
Posted: Tue May 26, 2009 10:03 pm Post subject: Re: Can functions return arrays? |
|
|
Why not just use this: Turing: | proc Sort (var nums : array 1 .. * of int)
%Code
end Sort | !!NOTE!! NOT TESTED
Also, you don't need a separate argument for the size of your array, just use the upper command. ex. Turing: | proc Sort (var nums : array 1 .. * of int)
var size : int := upper (nums )
end Sort |
|
|
|
|
|
|
Diablo117
|
Posted: Wed May 27, 2009 1:03 am Post subject: Re: Can functions return arrays? |
|
|
The key thing about this, is that it is a function, meaning I want it to return the array rather than just print it out onto the screen in case I want to do something else with it.
And yes, I started doing something like that but after debugging and lots of anger and frustration, I shove in random things which even I can barely understand. |
|
|
|
|
|
A.J
|
Posted: Wed May 27, 2009 10:00 am Post subject: RE:Can functions return arrays? |
|
|
@Diablo117 - A function can return an array. But you have to set an upper limit to the returned array (i.e. the maximum # of numbers you might put on the array). I see that you set it to 1000. Now, you can just set your array 'nums = Sort(nums)'. This can only be done if nums is an array from 1..1000, as the function returns an array from 1..1000. But you don't have to fill the array up with 1000 numbers. Like you have done above, it suffices to just pass an upper limit to the function so that the function just has to sort those numbers.
@corriep - he didn't use the 'upper' function because of this. he wants to let the function know how many numbers to sort in the array from 1..1000. Setting it to 'upper' (i.e. 1000) might crash, as he might have filled the array with less than 1000 numbers.
@Diablo117 - your bubble sort function is a bit off. Remember that you have to 'bubble up/down' all the elements in your array in order to sort them correctly.
Here is an example of a bubble sort function that returns an array:
Turing: |
function Sort (asize : int, anums : array 1 .. * of int) : array 1 .. 1000 of int
var size := asize
var nums : array 1 .. 1000 of int
for i : 1 .. asize
nums (i) := anums (i)
end for
var temp : int
for i : 1 .. size - 1
for j : i .. size
if nums (i) > nums (j) then
temp := nums (j)
nums (j) := nums (i)
nums (i) := temp
end if
end for
end for
result nums
end Sort
var nums : array 1 .. 1000 of int
for i : 1 .. 10
nums (i) := Rand.Int (1, 100)
end for
nums := Sort (10, nums)
for i : 1 .. 10
put nums (i)
end for
|
|
|
|
|
|
|
DemonWasp
|
Posted: Wed May 27, 2009 10:05 am Post subject: RE:Can functions return arrays? |
|
|
You can actually return flexible arrays in Turing, but you have to wrap them in a class to do it; you're returning a copy of the class (which contains the array), not the array itself. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
A.J
|
Posted: Wed May 27, 2009 10:25 am Post subject: RE:Can functions return arrays? |
|
|
Never use a flexible array in turing!!!!
I mean, what DemonWasp is saying is right, but never use flexible arrays in turing. They are extremely inefficient. I can't emphasize how inefficient they are. |
|
|
|
|
|
DemonWasp
|
Posted: Wed May 27, 2009 10:58 am Post subject: RE:Can functions return arrays? |
|
|
...inefficient as opposed to what other arbitrary-length sequential-access data structure? |
|
|
|
|
|
A.J
|
Posted: Wed May 27, 2009 11:09 am Post subject: RE:Can functions return arrays? |
|
|
Just make a huge global array while storing the size of the array individually. So, deleting ar adding something is just incrementing or decrementing the size variable. |
|
|
|
|
|
DemonWasp
|
Posted: Wed May 27, 2009 12:18 pm Post subject: RE:Can functions return arrays? |
|
|
Augh! That practice is so bad that you literally made me bleed! Here's why:
How huge? What if I need more? What if I only use a few? (then you're either wasting memory or will run out!)
The biggest slowdown you're likely to see in using flexible arrays is in resizing them (this requires an array-allocation, array-copy, then array-destroy). Instead of increasing the size by 1 each time when you resize, increase it by double or so. This strategy is the one I used in my Collections framework (see Turing Submissions), and the one used in the Java Collections Framework. |
|
|
|
|
|
A.J
|
Posted: Wed May 27, 2009 12:35 pm Post subject: RE:Can functions return arrays? |
|
|
Hey, hey, hey!
Don't jump on me!
Ask anyone, flexible arrays are horrible. You can always do without them. And I know what a Collections framework is, thank you .
There's only one tip I can give people who want to use flexible arrays in turing : DON'T!
Use C++ or something. Turing is pretty obsolete. |
|
|
|
|
|
DemonWasp
|
Posted: Wed May 27, 2009 3:42 pm Post subject: RE:Can functions return arrays? |
|
|
I was just wondering if there was a reasonable alternative. However, you cannot go without them - it's just a required capacity.
Collections frameworks (mine happens to be based on Java's) are a collection of classes that provide Lists, Sets, Maps, and other similar high-level structures for free.
Don't use C++ if you can avoid it. It's (hopefully) heading for obsoleteness pretty fast as it's not particularly necessary anymore. |
|
|
|
|
|
Dusk Eagle
|
Posted: Wed May 27, 2009 5:48 pm Post subject: Re: Can functions return arrays? |
|
|
A.J wrote: There's only one tip I can give people who want to use flexible arrays in turing : DON'T!
A.J, you offer great advice... for people free to follow. However, many of us are stuck using Turing thanks to our school, and although an experienced programmer might get away with global arrays, it is a terrible practice for someone learning programming, which is all Turing is really supposed to be for. Sure, flexible arrays are inefficient, but no one is building the applications of tomorrow using them. It's just high school kids who have been kept on the baby milk of programming languages too long, and our up against the wall in terms of what Turing reasonably allows one to do.
So, Diablo117, if you understand why global variables should be avoided in almost all real cases, and why arrays shouldn't be arbitrarily large or small, go ahead and make a global array of a huge size. But for those that don't understand the above, it would probably be better to do it some other way. |
|
|
|
|
|
|
|