Hw 11: Recursion
Hw 11: Recursion

 

Due Date: Thursday, Nov. 30, 2017, 23:55

See the General Guidelines for homework assignments.

This assignment must be done individually.

For each question below, design a recursive algorithm that satisfies the stated requirements. Express your answer using the pseudocode notation covered in the course notes on Algorithms. Use descriptive names for your variables, and include comments as necessary. Note: if you do not use the pseudo-code notation from the course notes, we will not grade your submission.

1. [50 points] Design a recursive algorithm that will display the sum of the digits of an integer.

Examples:

sumDigits( 126 )   displays    9
sumDigits( -49 )
  displays   13
sumDigits( 3 )
    displays    3

# Find the sum of the digits.
#
#number N          # integer to sum digits
algorithm sumDigits takes number num



2. [50 points] Design a recursive algorithm that will display whether it is possible to choose two integers from a list of integers such that the difference of the two equals a given value. Hint: You may wish to invoke another algorithm, (i.e., function), that accepts more
parameters which performs the recursion.

 Examples:

diff2( {2, 4, 8}, 3, 4)         displays     true
diff2( {1, 2, 4, 8, 1}, 5, 7)   displays     true
diff2( {2, 4, 4, 8}, 4, 7)      displays     false


# Find a specific difference between any 2 elements of a list.

#list number nums           # the list of values
#
number size                # the list size
#number diff                # the desired difference to find

algorithm diff2 takes list number nums, number size, number diff >

Computer Science 2104 Introduction to Problem Solvingt>
D. Barnette