09-23-2013 HW p

posted Sep 23, 2013, 5:33 AM by Samuel Konstantinovich   [ updated Oct 3, 2013, 5:15 AM ]
  • Write the following functions:

    • (getOnesDigit x)   →  returns the ones digit of an integer x

    • (getTensDigit x)   →  returns the tens digit of an integer x

    • (getHundredsDigit x) → returns the hundreds digit of an integer x

      • How many mathematical ways can you extract the tens place?


  • What is a generic way to extract the nth place of a number? e.g. extract the 1st digit, or 3rd digit. Think about how the tens and hundreds can be written almost the same way BEFORE trying the last part.


    New Scheme Function (expt a b) is an exponent function that raises a to the b power. (expt base power)


    Hints to help with the problems + homework:

    If 5329 is the number, you can get 9 by using the (remainder) function, (remainder 5329 10) gives 9. This works on any number, so we want to use in our function.


    Quotient is the counterpart to remainder. (quotient 5329 10) gives 532. This is super useful also because it gets rid of the 1's place. Quotient (5329 100) gives 53, so that got rid of the 10's and the 1's.


    If you get rid of the right number of columns you can just extract the 1's digit


    Answers to lab:
    (define (getOnesDigit x) (remainder x 10) )
    (define (getTensDigit x) (getOnesDigit (quotient x 10) )
    (define (getHundredsDigit x) (getOnesDigit (quotient x 100) )


  • Homework  (Printed and brought in tomorrow)

    write a function:

    • (getPlace x column) 

      x is the number you are processing, 

      column is a power of 10, so 2 means 10^2 or 100's place.) 

      This function extracts the digit at that place, or 0 if the column doesn't exist. 

      Examples:

      • (getPlace 552834 3) → 2 (extracts the 10^3 or the 1000's place which is 2)

      • (getPlace 552834 2) → 8 (extracts the 10^2 or the 100's place, which is 8)

      • (getPlace 552834 0) → 4 (extracts the 10^0 or 1's place, which is 4.

        If column is larger than the leftmost digit of the number, you should evaluate to a 0.



Comments