Qbasicnews.com
May 25, 2020, 11:03:11 PM
 Pages: [1]
 Author Topic: Digit Isolation  (Read 3727 times)
Member

Posts: 82

 « on: September 05, 2003, 06:14:17 PM »

My first challenge to you is to write a program that'll split a whole number of up to 15 digits into single digits that hold each place value. For example:

5918525 would return 5, 9, 1, 8, 5, 2, and 5.

However, you may only achieve this by using mathematical functions! Good luck! I'll be on my ol' DOS computer trying to do it too .
 Logged

Antoni Gual
Na_th_an

Posts: 1434

 « Reply #1 on: September 05, 2003, 06:32:10 PM »

Code:

DECLARE SUB isolate (a&)
a& = 987654321
PRINT "isolating "; a&
isolate a&

SUB isolate (a&)
IF a& = 0 THEN EXIT SUB
b& = a& MOD 10
a& = a& \ 10
isolate a&
PRINT b&
END SUB
 Logged

Antoni
Sterling Christensen
Na_th_an

Posts: 1328

 « Reply #2 on: September 05, 2003, 06:58:42 PM »

Antoni Gual's doesn't work when given a zero, better it's still better than mine.

I always thought you'd have to use an array/stack to start at the one's place and still print them in order - nice work!

Well, anyway, here's mine:

Code:
DEFLNG A-Z

INPUT "Enter a non-negative integer: ", n

divisor = 1000000000  ' start at the billions place
' LONGs cannot have any more digits than this

PRINT "Here are the digits, in order:";

' This flag will be used to clip off leading zeros:
havePrintedANonZeroDigit = 0

DO UNTIL divisor = 1

digit% = (n \ divisor) MOD 10
divisor = divisor \ 10

IF (digit% <> 0) OR havePrintedANonZeroDigit THEN
PRINT digit%; ",";
havePrintedANonZeroDigit = -1
END IF

LOOP

' This ensures that for n = 0, a zero will be printed:
PRINT n MOD 10  ' Print the last digit (in the one's place)

END
 Logged
Member

Posts: 82

 « Reply #3 on: September 05, 2003, 10:44:33 PM »

Yea, I realized that the algorithms I wrote won't work with 15 digits... so 10 has to work . Well, here's my code... it uses MOD and such, probably not as good as you guys though...

Code:
DECLARE SUB isolate (num)
DIM SHARED dig(10)
COMMON SHARED length
CLS
DO
INPUT "What number (1 - 9999999999) to isolate"; num
LOOP UNTIL num > 0 AND num < 9999999999
isolate (num)
FOR x = 1 TO 10
IF length >= x THEN PRINT dig(x);
NEXT x

SUB isolate (num)
FOR x = 1 TO 10
IF num < (10 ^ x) THEN length = x: GOTO lengthfound
NEXT x
lengthfound:
dig(1) = INT(num / (10 ^ (length - 1)))
FOR x = 2 TO length
dig(x) = num MOD 10 ^ (length - (x - 1))
dig(x) = INT(dig(x) / (10 ^ (length - x)))
NEXT x
END SUB
 Logged

Agamemnus
x/ \z

Posts: 3491

 « Reply #4 on: September 05, 2003, 11:39:04 PM »

Reverse order...

Code:

CLS
num1& = 5918525
DO
num2& = num1& \ 10
PRINT num1& - num2& * 10
IF num2& = 0 THEN EXIT DO
num1& = num2&
LOOP
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Member

Posts: 82

 « Reply #5 on: September 05, 2003, 11:47:10 PM »

You guys and your fancy short codes which do all that mine do yet are much simpler  :wink:.
 Logged

Meg
Ancient QBer

Posts: 483

 « Reply #6 on: September 06, 2003, 12:29:16 AM »

Here ya go.  I see this is basically Antoni's algorithm, but I'll post it anyways since it's a bit shorter and allows for input of 0.

Code:
DECLARE SUB split (n&)
INPUT n&
IF n& = 0 THEN PRINT 0 ELSE split n&

SUB split (n&)
IF n& THEN split n& \ 10 ELSE EXIT SUB
PRINT n& MOD 10
END SUB

*peace*

Meg.
 Logged
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #7 on: September 07, 2003, 10:43:38 AM »

That's what i meant   :oops:
Your code is the best, as it happens always  when you decide to show up... :lol:
 Logged

Antoni
Meg
Ancient QBer

Posts: 483

 « Reply #8 on: September 07, 2003, 01:13:12 PM »

title reiterated.

*peace*

Meg.
 Logged
Member

Posts: 82

 « Reply #9 on: September 07, 2003, 06:22:39 PM »

I need to learn how to do recursion effectively
 Logged