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. The
program should contain a loop that permits users to enter a new
choice of function for each loop, until exit from the loop
explicitly.
Write a explanation for each line
1
Factorial(0) = 1;
Factorial(n) = n * (n-1) * . . . * 2 * 1
Requirement: n >= 0; reject with error message otherwise
2
Fibonacci(0) = 0;
Fibonacci(1) = 1; Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);
Requirement: n >= 0; reject with error message otherwise
3
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
4
Power(a,b) = ??
Requirement: a > 0, b > 0, b is an integer; reject with error message otherwise
5
digProduct (x) = x if x <9
digProduct (x) = rightDigit * digProduct ( x/10)
Requirement: x is an unsigned integer (> 0); reject with error message otherwise
Sample Interaction
Lab 1
-----
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 6
Invalid Input.
Lab 1
-----
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 1
Enter a positive Integer: 5
Answer: 15
Lab 1
-----
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 4
Enter the first positive Integer: 2
Enter the second positive Integer: 3
Answer: 8
Lab 1
-----
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 0
Goodbye!
Thanks for the question.
below is the code you will be needing Let me know if you have any doubts or if you need anything to change.
Thank You !!
===========================================================================
#include<stdio.h>
int Factorial(int n){
if (n<=1)return 1;
else return n*Factorial(n-1);
}
int Fibbonacci(int nth){
if(nth==0 || nth==1) return nth;
else return Fibbonacci(nth-1)+Fibbonacci(nth-2);
}
int Gcd(int x, int y)
{
if (y != 0)
return Gcd(y, x%y);
else
return x;
}
double Power(int a, int b){
if(b==0)return 1;
if(b==1)return a;
else return a*Power(a,b-1);
}
int digProduct(int x){
if(x<9)return x;
else return x%10*digProduct(x/10);
}
int main(){
printf("1 - int Factorial(int
n)\n");
printf("2 - int Fibbonnaci(int
n)\n");
printf("3 - int Gcd(int x, int
y);\n");
printf("4 - double Power(int a, int
b)\n");
printf("5 - int digProduct (int
x);\n");
printf("0 - QUIT\n");
printf("Please enter a selection:
");
int choice;
scanf("%d",&choice);
int x,y;
switch(choice){
case
0:break;
case 1:
printf("Enter a positive number: ");
scanf("%d",&x);
printf("Factorial is %d\n",Factorial(x));
break;
case 2:
printf("Enter a positive number: ");
scanf("%d",&x);
printf("Fibbonacci is
%d\n",Fibbonacci(x));
break;
case 3:
printf("Enter value of x: ");
scanf("%d",&x);
printf("Enter value of y: ");
scanf("%d",&y);
printf("GCD is %d\n",Gcd(x,y));
break;
case 4:
printf("Enter value of a: ");
scanf("%d",&x);
printf("Enter value of b: ");
scanf("%d",&y);
printf("Power is %lf\n",Power(x,y));
break;
case 5:
printf("Enter value of x: ");
scanf("%d",&x);
printf("Dig Product is
%d\n",digProduct(x));
break;
default:
printf("Invalid
input.\n");
}
}
Objectives: - Practice designing/implementing algorithms - Practice use of functions Code and document the following functions...
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. The program should contain a loop...
C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...
I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...
code in c please Concepts to Practice User-written functions math.h Description For the prelab assignment, you are to write a function to calculate Power (exponentiation). Your function will be called MyPower. MyPower() will raise a given double type number to a positive integer power. You may NOT call any function from math.h such as pow) in your implementation of MyPower) The main) function in your program should look exactly like this: int main(void) int i,j; for (i-1;i<5;i++) { for (j-1;j<5;j++)...
C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...
Trace the following code and enter correct information into the working memory box. Code Memory Working Memory #include <stdio.h> // Prototypes double factorial(int); void main() 1 int num; // Number from user double fact; // Get value from user do { printf ("Give an integer greater or equal to zero : "); scanf("%d", &num); if (num < 0) printf ("Value must be larger or equal to zero.\n"); } while (num < 0); fact = factorial (num); printf ("The factorial of...
PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to: -define a recursive procedure/function and call it. -use syscall operations to display integers and strings on the console window -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9 if n <= 5 =...
Using C++ Write two `void` functions. These functions each take two integer parameters. The functions accomplish the following tasks. The first function prints out the numbers from the first argument up to and including the number passed as the second argument counting by the first argument (i.e. if the arguments are 2 and 10, the information below was given: #include <iostream> using namespace std; // function definitions// // main program int main() { int firstNumber,secondNumber; char countType, doAgain; // we will do this...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Utilizing Lab 2.2 code Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds.There are 2.2 pounds in one kilogram. Create an...