Docsity

CSCE 150A Laboratory Assignment 11 - Recursion, Lab Reports of Computer Science

Toggle sidebar
bg1
CSCE 150A - Problem Solving Using Computers Laboratory - Spring 2009
Lab Assignment 11 - 04/09/2009
Name:
Section: 8:00 11:00 12:30 6:30
Problem 1: Basic Recursion
Use recursion to calculate the greatest common divisor of two numbers, aand b, using the following
pseudocode:
gcd(a, b)
if b is zero, return a
else return gcd(b, a % b)
Implement the method with prototype int gcd(int a, int b) in a source file gcd.c and write a main
method to input aand band output gcd(a,b). The table below includes a few sample inputs and outputs.
a b gcd(a,b)
4 6 2
24 5 1
35 7 7
Problem 2: Double Recursion
Write a recursive method int lucas(int n) to take an integer nand output the value of L(n), where
the recursion is defined as:
L(n) =
2 if n= 0
1 if n= 1
L(n−1) + L(n−2) otherwise
The value L(n) is called the nth Lucas number, after some dead French guy.
Write your implementation in a file lucas.c including a main method that inputs a value nand outputs
the value lucas(n).
The table below lists the first eleven values of L(n).
n0 1 2 3 4 5 6 7 8 9 10
L(n) 2 1 3 4 7 11 18 29 47 76 123

Partial preview of the text

Download CSCE 150A Laboratory Assignment 11 - Recursion and more Lab Reports Computer Science in PDF only on Docsity!

CSCE 150A - Problem Solving Using Computers Laboratory - Spring 2009

Lab Assignment 11 - 04/09/

Name:

Section: 8:00 11:00 12:30 6:

Problem 1: Basic Recursion

Use recursion to calculate the greatest common divisor of two numbers, a and b, using the following pseudocode:

gcd(a, b) if b is zero, return a else return gcd(b, a % b)

Implement the method with prototype int gcd(int a, int b) in a source file gcd.c and write a main method to input a and b and output gcd(a,b). The table below includes a few sample inputs and outputs.

a b gcd(a,b) 4 6 2 24 5 1 35 7 7

Problem 2: Double Recursion

Write a recursive method int lucas(int n) to take an integer n and output the value of L(n), where the recursion is defined as:

L(n) =

2 if n = 0 1 if n = 1 L(n − 1) + L(n − 2) otherwise

The value L(n) is called the nth Lucas number, after some dead French guy.

Write your implementation in a file lucas.c including a main method that inputs a value n and outputs the value lucas(n).

The table below lists the first eleven values of L(n).

n 0 1 2 3 4 5 6 7 8 9 10 L(n) 2 1 3 4 7 11 18 29 47 76 123