CS 210 – Fundamentals of Programming I  Spring 2012 – In­class Exercise for 4/4/2012 & 4/5/2012

advertisement
CS 210 – Fundamentals of Programming I Spring 2012 – In­class Exercise for 4/4/2012 & 4/5/2012
(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 gcd.c 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/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 the 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 basically is the algorithm above.
Analysis of Function GCD
Analysis ­ what data is received, passed back, returned?
Objects
Type
Movement
Name
First positive integer
int
received
m
Second positive integer
int
received
n
Greatest common divisor
int
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. (Note: you must use Euclid's algorithm to earn credit for this exercise.) Build and run this program.
04/04/2012
Page 1 of 2
Name: ________________________
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 or simple) 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 in the space below (as demonstrated in class) 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 gcd.c file and submit under assignment 19­IN10 as usual, and turn in this exercise sheet with your answers to the questions.
04/04/2012
Page 2 of 2
Download