Session 5: Week 9/10: <i>Introducing Recursion</i>




Document: Software Engineering 1: Course Notes

next Term 2: Weeks 11-20
up Term 1: Weeks 1-10
previous Hints

Session 5: Week 9/10: Introducing Recursion

 

This exercise is divided into 4 parts. For each part your report should have separate sections labelled Plan, Coding and Testing. There should be a single overall Conclusion section.

The first two parts represent revision of material already encountered in previous exercises: arrays, user defined functions, the for statement, the printf() function etc. The third and fourth parts introduce the notion of recursion - a function which calls or invokes itself!

  1. Develop (i.e., design, enter, compile, test and debug) a function called print_array(array, size). The parameter array is an array of integers (each of type int) and size is the number of entries in array. The function print_array() should have the effect of displaying the contents of array. Each element of array should be displayed on a separate line. Use a for statement to achieve the required repetition.

  2. Develop a function called reverse_print_array(array, size). The parameter array is an array of integers (each of type int) and size is the number of entries in array. The function reverse_print_array() should have the effect of displaying the contents of array, but in reverse order (i.e. the last element of array should be displayed first etc.). Each element of array should be displayed on a separate line. Use a for statement to achieve the required repetition.

  3. Study the definition and discussion of the factorial function, on page 25 of Illustrating C. Enter, compile, and test the program FACTRIAL.C. Note that this program cannot work correctly for indefinitely large integers. Attempt to identify the biggest integer for which the function gives the correct answer.

  4. Develop a function called summer(n). The parameter n is an integer (type int). summer(n) should have the effect of calculating the sum of all the integers up to n, inclusive. It should achieve the required repetition using recursion. Attempt to identify the biggest integer for which the function gives the correct answer.




Document: Software Engineering 1: Course Notes

next Term 2: Weeks 11-20
up Term 1: Weeks 1-10
previous Hints



McMullin@ugmail.eeng.dcu.ie
Wed Mar 15 10:20:49 GMT 1995