Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab9 W2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

(COMP-1400) Lab Exercises #9

Objective:
The main objective of this lab activity is to help students to understand the definitions and
applications of functions in C programing language.

Use the following template to complete all problems. The main() function should only get the user
input, call the function, and print the result from main. The function called should not print
anything. Keep in mind different datatypes required to properly compute all example inputs given.

#include <stdio.h>

//define function prototype

int main(){

//Step 1: Get user input

//Step 2: Call function and pass input, receive answer from function

//Step 3: Print answer returned from function

return 0;

//define function (function should return a value, no printing)

A) Using the above template, write a C function to calculate and return the area of a circle given
its radius as an argument (float).
B) Using the above template, write a C function to calculate and return the factorial value of any
positive integer as an argument. The code should properly evaluate the following inputs:

2, 3, 4, 5, 10, 15 (Note: 15! = 1,307,674,368,000)

What is the maximum value of an integer for which the factorial can be calculated correctly
using your algorithm/C code?

C) Using the above template, write a C function to convert any positive decimal number as an
argument into binary.

• You can learn more about it here: https://www.wikihow.com/Convert-from-Decimal-


to-Binary

• The code should properly evaluate the following inputs: 1, 2, 10, 1001, 90250 (Note:
90250 in binary is 10110000010001010).

EVALUATION:
You need to show your GA/TA the complete programs at the end of this lab.. The marks you will
receive for this lab are made of two parts: Lab work marks 8 and attendance marks 2.
Total 10 marks
Page 1 of 2
Practice Problem: (Not graded) Using the above template, write a C function, called Fib, that
calculates the Nth Fibonacci number using recursion.

The formula is as follows: Fib(N) = Fib(N-1) + Fib(N-2) where Fib(0) is 0, and Fib(1) is 1.

Example:
Fib(4) = Fib(3) + Fib(2)
Where
- Fib(3) = Fib(2) + Fib(1) = 1 + 1 = 2
- Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1
- Fib(1) = 1
- Fib(0) = 0
Thus:
Fib(4) = 2 + 1 = 3

The Nth Fibonacci number is always the sum of the previous two in the sequence which has the
following pattern:
N 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Val 0 1 1 2 3 5 8 13 21 34 55 89 144 233

Lab Work Mark: Your C code will be evaluated based on your solutions for the problems based
on the following scheme:
1. Does the code run and meet specifications?
• Is input adequate and input data type properly validated?
• Is processing adequate?
• Is output correct and adequate?
• Is the code compliable? Is the code run properly?
2. Is the code properly commented?
• Are the program title, programmer's first and last name, and the date posted at the top in a
multi-line comment?
• Is each significant step of the program properly commented?
• Are comments added to clarify details? Are comments clear, accurate, and neatly
formatted?
3. Is the code properly formatted?
• Are blocks of code indented according to their parent-child relationship?
• Do curly braces line up vertically?
• Is there an empty line between significant steps (blocks) of the program?
• Is camel-case notation used for variable, e.g. employeeLastName?

Page 2 of 2

You might also like