Exercise II Consider the following pseudocode for a reduce-and-conquer recursive SumDigit (num) algorithm that finds the
Posted: Tue Jul 12, 2022 8:20 am
Exercise II Consider the following pseudocode for a reduce-and-conquer recursive SumDigit (num) algorithm that finds the sum of all digits in a number. Example calling SumDigit (1936) should return 19 (which is 6+3+9+1) integer SumDigit (num) input: an integer number, num output: sum of all the digits in num if num is a one-digit number //base - then the sum of all the digits in num is, num // example if num-7, then SumDigit (num), should return 7 otherwise case // general case: num has multiple digits in it - break num into 2 parts - One part, part 1, has the rightmost digit (use modulo operation to do that). Ex. 1936 10 -> 6 - The other part, part 2, has all the digits of num, except the rightmost digit. (use regular division to do this. Ex. 1936/10 -> 193 - Call the function SumDigit() itself on part 2, and get the result of the sum of all the digits in part 2.. Add the result from SumDigit (part 2) to part 1, and return it a. Write the corresponding C++ or Python code for the pseudocode. Submit only the code for the function SumDigit() and not the whole program. b. Using the modulo operation "%", addition operation "+", and division operation "/" as basic operations, set up and solve a recurrence relation for the number of basic operations the algorithm executes. c. Modify the code to create a new recursive function CountDigits(num) that counts the number of digits in a number num. Example: a call to the function CountDigits(1587) would return the value 4. Submit only the code for the function CountDigits(num). d. Using the modulo operation "%", addition operation "+", and division operation "/" as basic operations, set up and solve a recurrence relation for the number of basic operations for CountDigits(num).