Starting from:

$25.99

CS2- Module 1 Solved

Goals-  

Create recursive functions 
 

This is a    standalone module and is  NOT     part of the cumulative project.                                         You must still earn a    grade of         S.                                  

 

1.                  Write a function that recursively reverses a string.  The function will have a single parameter of type string.  It will print the reversed characters to the screen followed by a newline character.

What is the base case?  Which option prints the reversed string?   

a.       print out the characters before the recursive function call?   

b.      print out the characters after the recursive function call?   

 

2.                  Write a function that recursively calculates the sum of an array.  The function should have 2 parameters, the array and an integer showing the number of elements in the array.  The function will use a recursive call to sum the value of all elements.  What is the base case?  What is the structure of the recursive call?   

 

3.                  A triangular number is the sum of all items that form a triangle with N rows.  It may help to think of the number of bowling pins if you changed the number of rows.  For example in a standard game there are 4 rows and 10 pins.  If you add a fifth row you add 5 pins for a total of 15.  Always remember mathisfun.com!   

 

Write a recursive function to calculate the triangular number for N.  (Or a function that counts the number of pins necessary for N rows.)  What is the base case?    HINT:  Compare this to the factorial function.   

 

You must demonstrate your functions in a program.  For #1 you must prompt the user for a string and your program will print the reversed string.  For # 2 you must prompt for a series of numbers to enter in the array.  You will need to count them too.  How does the user indicate they are finished entering numbers?  Then your program will print the sum.  For #3 prompt the user to enter an integer and your program will return the total number of items in the triangle with that number of rows.   

 

You will have a single program to demonstrate all three recursive functions.  Provide a menu for the user to select which function to run, and then return to the menu.  You will need a fourth item in the menu to allow them to exit the program.   

 

NOTE:  Remember to validate input where appropriate

More products