Computer Science Canada

How to sort the integer by not using "array"

Author:  8749236 [ Thu Feb 03, 2011 11:31 am ]
Post subject:  How to sort the integer by not using "array"

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)
<Answer Here>

Turing:


<Add your code here>



Please specify what version of Turing you are using
Turing 4.1

Author:  Clayton [ Thu Feb 03, 2011 12:35 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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?

Author:  SNIPERDUDE [ Thu Feb 03, 2011 4:46 pm ]
Post subject:  RE:How to sort the integer by not using "array"

A full description of the task required would be helpful.

Author:  8749236 [ Thu Feb 03, 2011 8:14 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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"

Author:  Tony [ Thu Feb 03, 2011 8:33 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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?

Author:  8749236 [ Fri Feb 04, 2011 10:30 am ]
Post subject:  RE:How to sort the integer by not using "array"

just avoiding the "array"...
and others have no limit...

Author:  Zren [ Fri Feb 04, 2011 1:48 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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)?

Author:  TokenHerbz [ Fri Feb 04, 2011 8:06 pm ]
Post subject:  RE:How to sort the integer by not using "array"

why cant i call same named variables differing in numbers 1 .. upper in a for loop to use them???

Turing:


var text1: string := "TEST"
var text : string := ""

for i: 1 .. 1
    %%%WHY CAN'T I CALL VARIABLES LIKE THIS???
    put "TEST" + intstr(i) %% <-- "variable name"                                          :::OUTPUTS TEST1
    put  text + intstr(i) %%how do i call text1 using this type of method?          :::OUTPUTS 1
                     %%%%WANT IT TO OUTPUT ::: "TEST"
end for



I want wanting to just test something but This wont work, and imo it should. but alas turing cant use variables well... Is there a way to use them in this type of set up????

Turing:

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

Author:  DemonWasp [ Fri Feb 04, 2011 9:40 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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?

Author:  huskiesgoaler34 [ Sat Feb 05, 2011 4:08 pm ]
Post subject:  Re: How to sort the integer by not using "array"

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.

Author:  TokenHerbz [ Sat Feb 05, 2011 7:01 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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.

Author:  DemonWasp [ Sat Feb 05, 2011 7:26 pm ]
Post subject:  RE:How to sort the integer by not using "array"

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.

Author:  huskiesgoaler34 [ Sat Feb 05, 2011 8:45 pm ]
Post subject:  Re: How to sort the integer by not using "array"

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.

Author:  huskiesgoaler34 [ Sat Feb 05, 2011 8:53 pm ]
Post subject:  Re: RE:How to sort the integer by not using "array"

Turing:

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.

Author:  Insectoid [ Sat Feb 05, 2011 8:57 pm ]
Post subject:  Re: RE:How to sort the integer by not using "array"

huskiesgoaler34 @ Sat Feb 05, 2011 8:53 pm wrote:
Turing:

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.

Author:  huskiesgoaler34 [ Sat Feb 05, 2011 9:23 pm ]
Post subject:  Re: How to sort the integer by not using "array"

Sorry, I didn't read the entire section.

You are right that it's probably impossible unless you know the exact quantity.

Author:  TokenHerbz [ Sat Feb 05, 2011 10:16 pm ]
Post subject:  Re: How to sort the integer by not using "array"

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!


Turing:

%%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
                %%nothing, we need to go back to for loop...
                return
            elsif 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 <--
                    %%add into the middle
                else  %%end of it
                    %%stays the same (cause we add it to the end to start with)
                end if
            end if
            return %%not sure if we need this here, but to be safe to restart the loops if we change vars to sort threw
        end for
           
            %%we exit when loop when nothing else needs sorting.
    end loop

    %%exit when user is done adding numbers to be sorted (q)
    exit when sort_numbers_completed = true
end loop

put ""
put "FINALLY DONE!"
put "All the numbers sorted is here!!!"
put all_numbers



****Ofcourse I can't give you all the free-be answers, but Here is a few more hints to help you figure this out!!!! (This method does work, its your best bet to use without arrays, IMO).. Feel free to try something else tho.****


Turing:

%%an example of text manipulation
var test : string := "B5B6B8"  %%in reference to our other program :)

put test
put test (3) %%output "B"
test += "B10"
put test

for i : 1 .. length (test)
    if i = 7 then %break after 8 before 10 //(say we find a 9)
        var temp_start, temp_end : string := ""
        for s : 1 .. (i - 1) %%up to BEFORE the B
            temp_start += test (s)
        end for
        for e : i .. length (test) %all numbers start with "B" break to find easy!
            temp_end += test (e)
        end for
        %%now we "REMAKE THE STRING!!"
        test := temp_start + "B9" + temp_end
    end if
end for

put test  %%as you see, it added the B9 between 8 and 10..

%% E D I T E D      P O S T      T O       A D D        T H I S        P A R T !! ! !
for i: 1 .. length(test)
    if test(i) not= "B" then
        put (test(i)) ..
    else
        put " "..
    end if
end for
%%And ofcourse, To print the final string of numbers you could do something like this ....   I mean its a kind of "CHEATING" lol, but i mean, without arrays good luck doing this better....

Author:  Tony [ Sat Feb 05, 2011 10:31 pm ]
Post subject:  RE:How to sort the integer by not using "array"

This is a neat idea, but it should be clear that:

1) a string is an array of characters

2) Turing's strings hold a max of 255 characters.

If we are going to be building our own implementation of an array, File's read/write/seek (note: binary mode; not ASCII put/get) should work exceptionally well.

Author:  TokenHerbz [ Sat Feb 05, 2011 10:38 pm ]
Post subject:  RE:How to sort the integer by not using "array"

well i was under the assumption that he couldn't use "var array x .. y" style arrays lol...

Okay but you got a point there tony, Tomorrow i shall see about posting this doing just that, Should be crazy.


Although, This i think put my program within the acceptable limitations his teacher wanted, Do it without CODE : array.
8749236 @ Thu Feb 03, 2011 12:31 pm wrote:
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"

Author:  Tony [ Sat Feb 05, 2011 11:02 pm ]
Post subject:  RE:How to sort the integer by not using "array"

That's true, but when asked about number of integers to be sorted, the answer was
Quote:

...have no limit...


If you are looking to build something interesting, I could suggest an abstract Array class that could then have a number of different underlying implementations, including strings (as you've done already), files, linked-lists, or anything else that could hold some amount of data.

Allowing for some constrains, a single character could serve as a data-store for an array of 8 bits Wink

Author:  TokenHerbz [ Sun Feb 06, 2011 11:06 am ]
Post subject:  RE:How to sort the integer by not using "array"

yeah i guess im crippled with up to 255 chars to use in that string (not that i think someone would use that many...) but a valid point.

That would be something neat to look into indeed.


: