Lab 1-Algorithm Function- designing and implementing algorithms using C program
60-141 – Introduction to Programming II
Objectives:
– Practice designing/implementing algorithms
– Practice use of functions
Code and document the following functions using NON-RECURSIVE ITERATION only.
Test the functions by calling them from a simple interactive main() function using a menu, with
different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu:
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.]
Factorial(0) = 1;
Factorial(n) = n * (n-1) * . . . * 2 * 1
Requirement: n >= 0; reject with error message otherwise
Fibonacci(0) = 0;
Fibonacci(1) = 1;
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);
Requirement: n >= 0; reject with error message otherwise
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
Power(a,b) = ??
Requirement: a > 0, b > 0, b is an integer; reject with error message otherwise
Answer:
/*
Title: Lab #1: Algorithm, Function
Objective: Create a program with one main that solves 5 different problems using non-recursive method
including Summation: (Σ1+2+3+...+n), factorial(n), fibonacci(n), gcd(x,y) and power(a,b) by calling a
function using a menu.
*/
//Includes
#include <stdio.h> //C-Preprocessor Directives
//Function Prototypes
int summation(int n); // The sum of n and all the integer numbers below it down to 0
int factorial(int n); // The product of n and all the integer numbers below it down to 1
int fibonacci(int n); // Fibonacci(n) returns the nth Fibonacci number.
int gcd(int x,int y); // The greatest common denominator of the of the two integers numbers
int power(int a,int b); //Integer a raised to the power of integer b
//Main function
int main(void){
int choice=0;
//Main loop
do{
// Display the menu choices to the user and prompt for input.
printf("1- Use the SUMMATION with non-recursive iteration\n");
printf("2- Use the FACTORIAL with non-recursive iteration\n");
printf("3- Use the FIBONACCI with non-recursive iteration\n");
printf("4- Use the GCD with non-recursive iteration\n");
printf("5- Use the POWER with non-recursive iteration\n");
printf("0- Quit\n");
scanf("%d", &choice);
switch(choice){
case 1:{
// Collect information and call summation(n) function.
int n,result;
printf("Please enter a value of n \n");
scanf("%d",&n);
if(n<1){
printf("\nError, Please enter a number greater than or equal to 1\n\n");
continue;
}
else{
result=summation(n);
printf("\nResult is %d \n\n", result);
break;}
}
case 2:{
// Collect information and call factorial(n) function.
int n,result;
printf("Please enter a value of n \n");
scanf("%d",&n);
if(n<0){
printf("\nError, Please enter a number greater than or equal to 0\n\n");
continue;
}
else{
result=factorial(n);
printf("\nResult is %d \n\n", result);
break;
}
}
case 3:{
// Collect information and call fibonacci(n) function.
int n,result;
printf("Please enter a value of n \n");
scanf("%d",&n);
if(n<0){
printf("\nError, Please enter a number greater than or equal to 0\n\n");
continue;
}
else{
result=fibonacci(n);
printf("\nResult is %d \n\n", result);
break;
}
}
case 4:{
// Collect information and call gcd(x,y) function.
int x,y,result;
printf("Please enter a value for the first Number and then Enter a value for the Second Number \n");
scanf("%d %d",&x,&y);
if(x<0 || y<0){
printf("\nError, Please enter a number greater than or equal to 0\n\n");
continue;
}
else{
result=gcd(x,y);
printf("\nResult is %d \n\n", result);
break;
}
}
case 5:{
// Collect information and call power(x,y) function.
int x,y,result;
printf("Please enter a value for the first Number and then Enter a value for the Second Number \n");
scanf("%d %d",&x,&y);
if(x<0 || y<0){
printf("\nError, Please enter a number greater than or equal to 0\n\n");
continue;
}
else{
result=power(x,y);
printf("\nResult is %d \n\n", result);
break;
}
}
case 0:{
// Display a message for user after pressing 0 to Quit the program.
printf("\nThank you for using this program, Please come back again \n\n\n");
break;
}
default:{
// Display a message for user after pressing a number other than 0,1,2,3,4 and 5.
printf("\nIncorrect input, please choose a number between 1 and 5,or press 0 to Quit \n\n\n");
break;
}
}
}while (choice !=0); // End the while loop if the choice was 0
return 0; //exit main
}// end of main
/*
Objective: Compute the sum of n + all the numbers below it to 1
input: A positive integer that is 1 and higher
output: The sum of n plus all integer numbers down to 1, example: ∑ n +(n-1) +...+ 4+ 3+ 2+ 1
*/
int summation(int n){
int total=0,i;
for (i = 1;i <=n;i++){
total += i;
}
return total;
}
/*
Objective: Compute the factorial for integer n
input: A positive integer that is 1 and higher (Non-negative integer)
output: Return the factorial for integer n example: n*(n-1)*...* 4*3*2*1
*/
int factorial(int n){
int i;
int total = 1;
for (i = 1; i <= n; i++){
total *= i;
}
return total;
}
/*
Objective: Compute the nth Fibonacci number.
input: A positive integer that is 1 and higher
output: Compute the nth Fibonacci number which is determined by adding the last two Fibonacci numbers together,
example: (to find the third Fibonacci number we add the two previous numbers in the sequence 0,1,1 which is 1+1=2)
*/
int fibonacci(int n)
{
int i, k = 0, j = 1, fib = 1;
for (i = 1; i < n; i++){
fib = k + j;
k = j;
j = fib;
}
return fib;
}
/*
Objective: Compute the greatest common denominator of the of the two integers numbers x and y
input: x , y are integers and y is greater than 0
output: Return the greatest common denominator of the of the two integer numbers x,y
such that(gcd(x,y)=gcd(y,x MOD y), if y>0 and gcd(x,y)=x, if y=0)
*/
int gcd(int x, int y){
int temp;
while (y > 0){
temp = x;
x = y;
y = temp%y;
}
return x;
}
/*
Objective: Compute a raised to the power of b
input: Two positive integer a and b
output: return a raised to the power of b. (example for a = 3, b = 3 then power(3,3)= a*a*a = 3*3*3=27).
*/
int power(int a, int b){
int power = 1, i;
for (i = 0; i < b; i++){
power *= a;
}
return power;
}
Leave a reply