Name: ________________________ CS 210 – Fundamentals of Programming I  Fall 2011 – In­class Exercise for 11/16/2011

advertisement
Name: ________________________
CS 210 – Fundamentals of Programming I Fall 2011 – In­class Exercise for 11/16/2011
(10 points) This exercise consists of both a coding part and a written part. The purpose of this exercise is to gain experience writing recursive functions. An empty CodeBlocks project should be created for this exercise and the main program file should be downloaded from the course website to the project folder, then the file added to the project. Problem Statement
For some applications (such as reducing a fraction), we need to find the greatest common divisor of two positive integers, that is, the largest integer that divides both numbers. (One number q divides another number p if there is no remainder from p/q. In C++ code, this would mean that p % q == 0.) The Greek mathematician Euclid devised the following recursive algorithm to compute the greatest common divisor (GCD) of two positive numbers, m and n. ●
●
●
if n <= m and n divides m, then GCD(m, n) is n if n > m, then GCD(m, n) is GCD(n, m) otherwise GCD(m, n) is GCD(n, the remainder of m divided by n) This algorithm states that if n is the smaller number and n divides m, then the GCD is n. If m is the smaller number, then the GCD determination should be performed with the arguments reversed. Otherwise, if n does not divide m, the answer is obtained by finding the GCD of n and the remainder of m divided by n. Here is an analysis for the function GCD(m,n). The design is basically the algorithm above.
Analysis of Function GCD
Analysis ­ what data is received, passed back, returned, or local?
Objects
Type
Kind
Movement
Name
First positive integer
int
variable
received
m
Second positive integer
int
variable
received
n
Greatest common divisor
int
variable
returned
­­­­­
Assignment 0. The downloaded program is an implementation of the a program that will read in two input values and call the function GCD with the two values. 1. (5 points) Add the prototype and a definition for the GCD function that uses Euclid's algorithm above to compute the greatest common divisor of the two input values. Build and run this program.
11/15/2011
Page 1 of 2
2. (5 points) When you are confident your program works correctly, answer the following questions: a. What makes Euclid's algorithm a recursive algorithm? b. What is the base (or anchor) case of the algorithm? c. What are the recursive (or inductive) cases of the algorithm? d. What is the greatest common divisor of 24 and 84? e. Draw a trace (as demonstrated in class, on the back of this sheet) of the function call GCD(24,84). Note that you can put a printf statement at the very beginning of your GCD function to print out the values of m and n to check your trace. When you have completed this exercise, make sure name is in the program heading comment block, zip up your main.c file and submit under assignment 19­IN10 as usual, and turn in this exercise sheet with your answers to the questions.
11/15/2011
Page 2 of 2
Download