below is the problem i am given, after this i explain where i am at so far and the code i have created,
Write the following functions in Scheme:
1.a "digitinc4" -takes as input a 4-digit integer, and returns another 4-digit integer constructed of the original input integer's digits, each incremented. For example, (digitinc4 4833) would return 5944. When a digit is a 9, the corresponding output digit should be a 0. For example:
(digitinc4 4892) would return 5903. You can assume that the input number is at least 1000 - that is, there are no leading zeros.
b. Extend your answer in problem (a), above, to handle input integers with an arbitrary number of digits. This function should be named "digitinc" (without the "4" in the name). For example:
(digitinc 83) would return 94, and (digitinc 22897) would return 33908.
so far i know i can increment each digit by adding by
+1
+10
+100
+1000
etc
and as an example
The following results are the remainders which also correspond to the digit I would test:
assuming input is 4816
1st digit
4816 % 10 = 6 (does not equal 0, increment next digit)
2nd digit
4816 / 10 = 481.6
481.6 % 10 = 1 (does not equal 0, increment next digit)
3rd digit
481.6 / 10 = 48.16
48.16 % 10 = 8 (does not equal 0, increment next digit)
4th digit
48.16 / 10 = 4.816
4.816 % 10 = 4 (does not equal 0, increment next digit)
And I can use these remainders to determine if something is a 0
After incrementing that digit, then I can skip the next digit increment
there are some special situations when a digit will become 0 if it was a 9 added by 1. (because 9+1 = 10)
so i can check if this digit is zero (after the addition) to know to skip the next digit addition using
mod: 4816+1 >>>> 4817 % 10 does not equal zero. so i know the digit is not zero so i can continue to add +10
this is a situation where mod is used:
4819+1 >>>> 4820 % 10 = 0 so i know i can now skip +10 and add +100 (because i dont want to increment a digit twice
based on this i have generated some code which runs yet does not perfrom desirable results.
everything works as predicted until the end of the first cond.
by the second cond, at the first test, the debugger says that 2408/5 is happening.
i am totally lost here i have debugged many times but drracket is not sophisticated enough to give me more details.
please if someone could share some insight or advice on this situation or maybe how i could implement what i have in a simpler more efficient way.
and here is my scheme code below:
(define (mod x)
(modulo x 10))
(define (di4 n)
(+ n 1) ; increment 1st digit
(cond ;check if 1st digit became 0 if so skip and increment 100 otherwise continue normal
[(= (mod n) 0) (+ n 100)] ;increment 3rd if 2nd is 0
[else (+ n 10)]) ;increment 2nd digit
(cond
[(= (mod (/ n 10)) (+ n 1000))] ;increment 4th if 3rd is 0
[else (+ n 100)]) ;increment 3rd
(cond
[(= (mod (/ n 100) (+ n 10000)))] ;increment 5th if 4th is 0
[else (+ n 1000)]) ;increment 4th
)