Author |
Message |
Freakish
|
Posted: Mon Sep 11, 2006 9:45 pm Post subject: Splitting up digits into var |
|
|
How can I split up digits into var
Ex.
I type in the number 392175
var digit01 is = to 3
var digit02 is = to 9
var digit03 is = to 2
var digit04 is = to 1
var digit05 is = to 7
var digit06 is = to 5 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
ericfourfour
|
Posted: Mon Sep 11, 2006 10:22 pm Post subject: (No subject) |
|
|
I guess you could divide the number by a power of ten. Then, you could truncate the decimal. |
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Tue Sep 12, 2006 2:31 am Post subject: (No subject) |
|
|
that, or the long way
code: |
var number: string %%We use a string so we can minipulate it.
%%This here makes sure the strink that was entered is pure numbers.
loop
get number %%get you string
if strintok (number) then %%basicly means: if string can convert to int ok then
exit %%we leave the loop
else %%otherwise we tell them its wrong
put "Enter numbers only" %%message.
end if
end loop
var numbers: array 1 .. length(number) of int %%make an array to the length of the string (should use flexy)
for i: 1 .. upper (numbers) %%for loop to the amount to the string
numbers(i) := ord(number(i)) %%we declare the variables to the place in the string
end for
for i: 1 .. upper (numbers)
put "Number ", i, " is ", number(i) %%we show that it really works :)
end for
|
|
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Tue Sep 12, 2006 7:26 am Post subject: (No subject) |
|
|
or you could do it the cool way (ie the math way)
code: | var number : int := 392175
var digits : array 1 .. ceil (ln (number + 1) / ln (10)) of int
for i : 1 .. upper (digits)
digits (upper (digits) - i + 1) := (number div (10 ** (i - 1))) mod 10
end for
for i : 1 .. upper (digits)
put digits (i)..
end for |
|
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Tue Sep 12, 2006 10:44 am Post subject: (No subject) |
|
|
zylum, why did you use "ceil" when your not dealing with decimals? |
|
|
|
|
![](images/spacer.gif) |
do_pete
![](http://i38.photobucket.com/albums/e112/do_pete/1943.gif)
|
Posted: Tue Sep 12, 2006 10:51 am Post subject: (No subject) |
|
|
Or you could do it the easy and short way:
code: | var number : int
var stringnumber : string
get number
stringnumber := intstr (number)
for x : 1 .. length (stringnumber)
put stringnumber (x)
end for
|
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Tue Sep 12, 2006 1:39 pm Post subject: (No subject) |
|
|
Short, maybe, but not easy. At least not for the computer. Does it really make sense to turn a number into a string, split it up, then turn it back? It makes far more sense to do it the math way. |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Tue Sep 12, 2006 3:27 pm Post subject: (No subject) |
|
|
TokenHerbz wrote: zylum, why did you use "ceil" when your not dealing with decimals?
where do you not see decimals? i used 2 lns and a divide... looks like plenty of decimals to me ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
richcash
|
Posted: Tue Sep 12, 2006 8:57 pm Post subject: (No subject) |
|
|
Quote: zylum, why did you use "ceil" when your not dealing with decimals?
It's good that you asked, beacuse I say that one should never use an algorithm unless they know fully what it means. I get the feeling that some people won't know what zylum is doing giving such a complex upper bound to his array. Well, maybe it would help if you thought of it like this :
code: | fcn LOG_10 (n : int) : real
result ln (n) / ln (10)
end LOG_10
var digits : array 1 .. floor (LOG_10 (number)) + 1 of int %floor is the opposite of ceil |
The upper bound of this array is equal to the number of digits in number. Now you can see it more easily, if you know what LOG is. |
|
|
|
|
![](images/spacer.gif) |
|