
-----------------------------------
8749236
Thu Feb 03, 2011 11:31 am

How to sort the integer by not using &quot;array&quot;
-----------------------------------
What is it you are trying to achieve?
Our teacher give us a class assignment, he want us to sort the integers by not using code :"array"

What is the problem you are having?
The only way i know is use the "if" statement. is their any easier way to sort the integers?

Describe what you have tried to solve this problem
i tried "label" but doesn't work well (maybe is my fault..)

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)








Please specify what version of Turing you are using
Turing 4.1

-----------------------------------
Clayton
Thu Feb 03, 2011 12:35 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
I'm not exactly sure what you're asking here. Are you supposed to sort a bunch of given integers? Are you sorting one or two user-inputted integers? Why aren't you to use arrays?

-----------------------------------
SNIPERDUDE
Thu Feb 03, 2011 4:46 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
A full description of the task required would be helpful.

-----------------------------------
8749236
Thu Feb 03, 2011 8:14 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
we are trying to make a program..
the user input a bunch of integer, and our program need to reorganize it from smallest to biggest, but we cannot use the code "array"

-----------------------------------
Tony
Thu Feb 03, 2011 8:33 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
How many integers? What's the range of valid values? Are you supposed to avoid just the keyword of "array" (build in implementation) or the concept of an array (let's say C's pointer + offsets)? Can you store integers in any other data-structure such as lists or trees?

-----------------------------------
8749236
Fri Feb 04, 2011 10:30 am

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
just avoiding the "array"...
and others have no limit...

-----------------------------------
Zren
Fri Feb 04, 2011 1:48 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
First lets get some questions out of the way.

Do you know what arrays are? It's hard to not use something when you don't even understand what it is.
Is there a maximum amount of numbers (Eg 5 numbers) that you need to sort?
Do you understand the concept of sorting in general? If you had 3 different numbers in 3 different variables, how would you print them to the screen in ascending order (low->high)?

-----------------------------------
TokenHerbz
Fri Feb 04, 2011 8:06 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
why cant i call same named variables differing in numbers 1 .. upper in a for loop to use them???



var text1: string := "TEST"
var text : string := ""

for i: 1 .. 1
    %%%WHY CAN'T I CALL VARIABLES LIKE THIS???
    put "TEST" + intstr(i) %%  num (intstr (j)) then
                tempsmall := num (intstr (j))
                templarge := num (intstr (i))
                num (intstr (i)) := tempsmall
                num (intstr (j)) := templarge
                reset_fors := true
            end if
        end for
    end for
    exit when reset_fors = false
end loop
put "DONE"
put num1
put num2
put num3

THX

-----------------------------------
DemonWasp
Fri Feb 04, 2011 9:40 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
Why not? Because that's what arrays are for. Duh. This concept is actually called "variable variables" and if used improperly, it's almost impossible to debug. Very few languages support it (those that do include PHP and Ant).

Why aren't you using arrays, again?

-----------------------------------
huskiesgoaler34
Sat Feb 05, 2011 4:08 pm

Re: How to sort the integer by not using &quot;array&quot;
-----------------------------------
DemonWasp is correct. This would be a perfect time to use arrays. That's what they are supposed to do. I don't understand why your teacher wouldn't allow you to use arrays when arrays are the most efficent way to slove your problem.

-----------------------------------
TokenHerbz
Sat Feb 05, 2011 7:01 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
perhaps they are trying to encourage an recursive type of answer or something, But yeah, arrays rule.

I tried some other ways and well, I'm curious to know why you cant use them either.

-----------------------------------
DemonWasp
Sat Feb 05, 2011 7:26 pm

RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------
I mean, it's certainly possible to sort things in non-array objects, such as lists and trees. However, it seems unlikely that a course at that level would be writing in Turing.

-----------------------------------
huskiesgoaler34
Sat Feb 05, 2011 8:45 pm

Re: How to sort the integer by not using &quot;array&quot;
-----------------------------------
The author of the thread wrote that he thought of an if statement. An if statement is used only for conditions. If somethings happens then do this, etc. Label won't work very well for this program.

-----------------------------------
huskiesgoaler34
Sat Feb 05, 2011 8:53 pm

Re: RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------

var num1 : int := 7
var num2 : int := 3
var num3 : int := 5

loop
    var reset_fors : boolean := false
    var tempsmall, templarge : int := 0
    for i : 1 .. 3
        if reset_fors = true then
            return
        end if
        for j : 1 .. 3
            if reset_fors = true then
                return
            end if
            if num (intstr (i)) > num (intstr (j)) then
                tempsmall := num (intstr (j))
                templarge := num (intstr (i))
                num (intstr (i)) := tempsmall
                num (intstr (j)) := templarge
                reset_fors := true
            end if
        end for
    end for
    exit when reset_fors = false
end loop
put "DONE"
put num1
put num2
put num3

THX[/quote]

This program doesn't work. The variable num hasn't been declared nor has it been set a value.

-----------------------------------
Insectoid
Sat Feb 05, 2011 8:57 pm

Re: RE:How to sort the integer by not using &quot;array&quot;
-----------------------------------

var num1 : int := 7
var num2 : int := 3
var num3 : int := 5

loop
    var reset_fors : boolean := false
    var tempsmall, templarge : int := 0
    for i : 1 .. 3
        if reset_fors = true then
            return
        end if
        for j : 1 .. 3
            if reset_fors = true then
                return
            end if
            if num (intstr (i)) > num (intstr (j)) then
                tempsmall := num (intstr (j))
                templarge := num (intstr (i))
                num (intstr (i)) := tempsmall
                num (intstr (j)) := templarge
                reset_fors := true
            end if
        end for
    end for
    exit when reset_fors = false
end loop
put "DONE"
put num1
put num2
put num3

THX

This program doesn't work. The variable num hasn't been decalred nor has it been set a value.[/quote]

If you'd care to read the entire post containing this code segment you'd see the author already knew it wouldn't work. Yeah, this can be done using conditions if you know EXACTLY how many numbers you're inputting but if it's a variable quantity then it's probably impossible in Turing.

-----------------------------------
huskiesgoaler34
Sat Feb 05, 2011 9:23 pm

Re: How to sort the integer by not using &quot;array&quot;
-----------------------------------
Sorry, I didn't read the entire section. 

You are right that it's probably impossible unless you know the exact quantity.

-----------------------------------
TokenHerbz
Sat Feb 05, 2011 10:16 pm

Re: How to sort the integer by not using &quot;array&quot;
-----------------------------------
Okay Iv'e figured this out, and made some Turing codes for you to have a ground start... I think I commented it good, and as we on compsci.ca goals are, to help you "LEARN" so I will not give you the completed source code.   That is for you to work on, and Iv'e provided you with alot of good informtion and concepts to get this working.  However if you do run into problems or more questions, Please don't hesitate in some replys, But You must tell me what you've done or tried to sort this out first, Because we arn't here to do your projects first.

This was a very interesting question your presented us with and in programming, there are ALWAYS ways to do things, Cant be limited, just gotta try and think outside the box a bit, which can be hard to do sometimes, Anyways this should keep you busy for the night.

Please post final SOURCE code when its complete so we may test it and critique it...  Should be fun!


%%I been thinking how we could do this, so after alot of thinking i figured...
%% Finding which is bigger / smaller would be easier if we compared this with 2 numbers...
%% TEXT is easy enough to manipulate, add to, change...
%% So lets try to use a TEXT of numbers and match them up to make this work, if ya fallow...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Basic idea of what we want to try would be, "get 1 number to start" then every number after,
%% We compare it to the current list of numbers.  we sort threw the string of numbers to see
%% Where we add it, and when user done entering numbers, we have a sorted string of numbers...

var tempN : string
var tempS : string := ""
var all_numbers : string := ""
var get_number : string
var sort_numbers_completed : boolean := false

loop
    %%Get user number
    loop
        put "Please enter a number to be sorted!"
        get get_number
        if strintok (get_number) = true then
            exit
        elsif Str.Lower(get_number) = "q" then
            sort_numbers_completed := true
            exit
        end if
        cls
    end loop

    %%we have to sort and add to the string correctly.    
    tempS := all_numbers
    all_numbers += "B" + get_number   %%we can use "B" as a marker (space) if you will between numbers
    tempN := ""
    loop
        for i: 1 .. length(tempS) %%old string we need to search
            if tempS(i) = "B" then  %%BREAK --> temp Number is set for 1 number!
                for temp: (i + 1) .. (length(tempS))
                    if strintok(tempS(temp)) = true then
                        tempN += tempS(temp)
                    else
                        return
                    end if
                end for
            end if
            %%%Keep place in string here right, So lets compare numbers!
            if strint(tempN)  strint(get_number) then
                if i = 1 then %%start of numbers
                    %%add number to front of string
               %% elsif middle of string breaks then 