
-----------------------------------
Freakish
Mon Sep 11, 2006 9:45 pm

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

-----------------------------------
ericfourfour
Mon Sep 11, 2006 10:22 pm


-----------------------------------
I guess you could divide the number by a power of ten. Then, you could truncate the decimal.

-----------------------------------
TokenHerbz
Tue Sep 12, 2006 2:31 am


-----------------------------------
that, or the long way :)


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


-----------------------------------
zylum
Tue Sep 12, 2006 7:26 am


-----------------------------------
or you could do it the cool way  :lol:  (ie the math way)

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

-----------------------------------
TokenHerbz
Tue Sep 12, 2006 10:44 am


-----------------------------------
zylum, why did you use "ceil" when your not dealing with decimals?

-----------------------------------
do_pete
Tue Sep 12, 2006 10:51 am


-----------------------------------
Or you could do it the easy and short way:
var number : int
var stringnumber : string

get number

stringnumber := intstr (number)

for x : 1 .. length (stringnumber)
    put stringnumber (x)
end for


-----------------------------------
Cervantes
Tue Sep 12, 2006 1:39 pm


-----------------------------------
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.

-----------------------------------
zylum
Tue Sep 12, 2006 3:27 pm


-----------------------------------
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:

-----------------------------------
richcash
Tue Sep 12, 2006 8:57 pm


-----------------------------------
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 :
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.
