Sorting Variables?
Author |
Message |
qwerty22
|
Posted: Sat Jul 28, 2012 6:53 pm Post subject: Sorting Variables? |
|
|
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?
I have several variables that I want to sort alphabetically, but can't figure out how.
Describe what you have tried to solve this problem
I know some Turing, but I am very far from being an expert, so spare me if the solution is obvious to you. I have tried to use some sample code that sorts an array alphabetically, but the things I want to sort are strings, and it would be very difficult for me to change this. It would be most helpful if you could supply me with some ideas, but explain them in simple terms aswell. In case you want to know, the names of the variables are "entry1" all the way to "entry10". Also, I have heard of bubble sort, but don't really understand it, and I'm not even sure if it's relevant to this. One more thing, if there is any way to put my variables into an array, then I would be able to use the sample code I mentioned earlier. I copied that code from somewhere, so I would be able to figure out how to alter it, but if I could simply put my text in as the array it ries to sort, then that would be a solution
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
There really isn't any relevant code
Turing: |
<Add your code here>
|
Please specify what version of Turing you are using
I am using Turing 4.1.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sat Jul 28, 2012 7:22 pm Post subject: Re: Sorting Variables? |
|
|
You can't, and shouldn't have to, sort variables by name. You choose the names, so there is no need to have the program sort them. If you want to sort their values then you should use an array and sort that.
qwerty22 wrote: if there is any way to put my variables into an array You can put the values into an array, but you can't put the names into the array (in fact, your program doesn't know the names of variables while it runs).
I'm sorry if this isn't very helpful. Whatever you are trying to accomplish by sorting variables by name can probably be done in a different way. Unfortunately, I don't know what the end goal is here and so I can't give you any ideas on how you might accomplish it. |
|
|
|
|
![](images/spacer.gif) |
qwerty22
|
Posted: Sat Jul 28, 2012 8:21 pm Post subject: RE:Sorting Variables? |
|
|
Thanks for your time. I will try to clarify this a little more:
I am trying to sort the variables by their 'content', 'value' or what ever you wish to call it. Not by their actual name. The are all strings. Unless there is a way to transfer the values of the 10 variables into an array, an array is not a solution (unfortunately). Hopefully this helps. |
|
|
|
|
![](images/spacer.gif) |
qwerty22
|
Posted: Sat Jul 28, 2012 8:25 pm Post subject: RE:Sorting Variables? |
|
|
One more thing, in case this helps set the end goal clearer. My task is to create a program that writes bibliographies. Each individual entry is set to a string variable ('entry1' to 'entry10'). Once all the entries are filled out, they need to be sorted alphabetically for the display of the finished bibliography. |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sat Jul 28, 2012 9:05 pm Post subject: Re: Sorting Variables? |
|
|
Well, if your variables are already named entry1 to entry10 then it shouldn't be too hard to change that into entry(1) to entry(10). If you feel your missing something about arrays, check out this tutorial, http://compsci.ca/v3/viewtopic.php?t=14333.
Turing: | var arr1, arr2, arr3, arr4, arr5 : string
get arr1
get arr2
arr3 := arr1 + arr2
if arr1 > arr2 then
arr4 := arr1
else
arr4 := arr2
end if
arr5 := "Hello World!"
put arr1
put arr2
put arr3
put arr4
put arr5
% Is basically the same as
var arr : array 1..5 of string
get arr(1)
get arr(2)
arr(3) := arr(1) + arr(2)
if arr(1)(1) > arr(2)(1) then
arr(4) := arr(1)
else
arr(4) := arr(2)
end if
arr(5) := "Hello World!"
for i : 1..5 %I can use a for loop to go through the array
put arr(i)
end for |
An array is really just a sequence of variables. Once you have an array sorting shouldn't be too tough.
Hope this helps. |
|
|
|
|
![](images/spacer.gif) |
qwerty22
|
Posted: Sat Jul 28, 2012 11:49 pm Post subject: RE:Sorting Variables? |
|
|
Alright, that's perfect! Thanks! |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Tue Jul 31, 2012 12:21 am Post subject: RE:Sorting Variables? |
|
|
What language is this? it looks so easy. Turing? Never heard of it |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Tue Jul 31, 2012 1:21 pm Post subject: RE:Sorting Variables? |
|
|
It is easy, that's the point. It's used for learning programming concepts and such |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue Jul 31, 2012 1:25 pm Post subject: RE:Sorting Variables? |
|
|
It's pseudocode. |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Tue Jul 31, 2012 1:57 pm Post subject: Re: Sorting Variables? |
|
|
mirhagk wrote:
It's pseudocode.
Turing is far from pseudocode. |
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue Jul 31, 2012 2:15 pm Post subject: RE:Sorting Variables? |
|
|
well most people's pseudocode is pascal style (curly braces look ugly in pseudo code) so turing's sytnax is similar to pseudocode. |
|
|
|
|
![](images/spacer.gif) |
|
|