Posted: 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.
Sponsor Sponsor
TokenHerbz
Posted: 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
ifstrintok(get_number)=truethen exit elsifStr.Lower(get_number)="q"then
sort_numbers_completed :=true exit endif cls endloop
%%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)) ifstrintok(tempS(temp))=truethen
tempN += tempS(temp) else return endif endfor endif %%%Keep place in string here right, So lets compare numbers! ifstrint(tempN) <= strint(get_number)then %%nothing, we need to go back to for loop... return elsifstrint(tempN) > strint(get_number)then if i =1then%%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) endif endif return%%not sure if we need this here, but to be safe to restart the loops if we change vars to sort threw endfor
%%we exit when loop when nothing else needs sorting. endloop
%%exit when user is done adding numbers to be sorted (q) exitwhen sort_numbers_completed =true endloop
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 =7then%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) endfor for e : i .. length(test)%all numbers start with "B" break to find easy!
temp_end += test (e) endfor %%now we "REMAKE THE STRING!!"
test := temp_start + "B9" + temp_end
endif endfor
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" "..
endif endfor %%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....
Tony
Posted: 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.
Posted: 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"
Tony
Posted: 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