COMP1410 Lab 2-Algorithm, Recursive Function Solved

35.00 $

Category:

Description

5/5 - (1 vote)

 

  • Practice designing/implementing algorithms using recursion
  • Practice use of recursive functions

 

Pre-requisite(s):

  • Read and review chapters 1-5.

 

In this Lab #2, you must code and document the following functions using RECURSION only.

 

As with the last Lab #1, test the functions by calling them from a simple interactive main() function using a menu, with different values. Overall, you should have one C program (call it Lab2.c) containing one main() function and 5 other functions listed in the table below, where the functions are called based on the user input to the menu choices.  The program should contain a loop that permits users to enter a new choice of function for each loop, until exit from the loop is explicitly chosen.

 

1 Summation:   ∑𝑛𝑘=1𝑘=1+2+3+⋯+𝑛 ;

n > 1; reject with error message otherwise

[Note that this sum is equal to n(n+1)/2.  DO NOT program the function – program the series.]

2 Factorial(0) = 1;

Factorial(n) = n * (n-1) * . . . * 2 * 1

Requirement: n >= 0; reject with error message otherwise

3 Fibonacci(0) = 0;

Fibonacci(1) = 1;

Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);

Requirement: n >= 0; reject with error message otherwise

4 gcd (x, y) = x, if y=0

gcd (x, y) = gcd (y, x MOD y), if y > 0

Requirement: x and y both > 0; reject with error message otherwise

5 Power(a,b) = 𝑎𝑏

Requirement:  a > 0, b > 0, b is an integer; reject with error message otherwise

 

How to document functions?

/* Objective: Describe the function/its purpose briefly

Input: Describe the input parameters, or the assumptions/requirements for the function.

Output: Describe the output of the function. (What does it return? What does it print, if anything?) */

 

EVALUATION OF WORK AND ATTENDANCE: Total 5 marks.

You need to show to your lab instructor the work you have completed for this lab assignment, generally in the form of a working program. The marks you will receive for the lab are made of two parts, the programming part and lab attendance. Do not email your work to the Instructor or to any teaching assistant.

 

Lab Work Mark: You will be evaluated based on your solution for the problem assigned, using the following scheme:  You must attend the lab and show your work in order to earn any of the following marks.

 

  • mark = No appreciable and relevant work done.
  • marks = Incomplete code / does not compile, with no, little or invalid documentation.
  • marks = Complete running program with no, little or invalid documentation.
  • marks = Incomplete code / does not compile, with suitable documentation.
  • marks = Complete running program with suitable documentation (minor errors may be accepted).

 

  • lab02-jnbagc.zip