Question

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 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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Code :

#include<stdio.h>           // for using printf() and scanf()

int Factorial(int n)           // this function will return the factorial of n
{
   int result = 1;           // initially the result will 1
  
   for(int i=n;i>=1;i--)               // now calculating the factorial of n
   {
       result = result * i;               // multiplying all the numbers till n
   }
  
   return result;           // returning the value of factorial of n
}

int Fibonacci(int n)           // this function will return the nth fibonacci number
{
   int fib[n+1];                   // this array will store all the fibonacci numbers
   fib[0] = fib[1] = 0;           // first and second fibonacci numbers are always 0
   for(int i=2;i<=n;i++)          
   fib[i] = fib[i-1]+fib[i-2];               // calculating rest of the fibonacci numbers by adding previous two values
  
   return fib[n];           // returning the nth fibonacci number
}

int Gcd(int x, int y)           // this function will return the gcd of both x and y
{
   while(x!=y)            // iterating till x not equals to y
   {
        if(x>y)            // if x is greater than y
        x=x-y;                // we'll simply subtract y from x
        else           // otherwise
        y=y-x;                // we'll simply subtract x from y
    }
    return x;                // and at last, x will be the gcd of both x and y
}

double Power(int a, int b)               // this function will return the pow(a,b)
{
   double result = 1.0;               // initially this double variable will store 1.0
  
   for(int i=1;i<=b;i++)           // iterating b times
   result = result * a;           // multiplying the value of a, b times to get the pow(a,b)
  
   return result;               // returning the value of pow(a,b)
}

int digProduct (int x)               // this function will return the product of digits in the x
{
   int result = 1;               // initially the result will be 1
  
   while(x>0)           // iterating till x>0
   {
       int rem = x%10;               // finding each digits of the x
       result = result * rem;               // multiplying all the digits of x
       x = x/10;               // and each time, we'll remove the digits of the x
   }
  
   return result;               // returning the value of result
}

int main()
{      
   while(true)           // unitl user exits the program
   {
       printf("\n\nLab 1\n\n-----\n\n");               // this is a simple message as stated in the output of the question
       printf("\n1 - int Factorial(int n);\n\n");               // for calculating the factorial of number n
       printf("2 - int Fibonacci(int n);\n\n");               // for finding the nth fibonacci number
       printf("3 - int Gcd(int x, int y);\n\n");               // for finding the gcd of both x and y
       printf("4 - double Power(int a, int b);\n\n");               // for calculating pow(a,b)
       printf("5 - int digProduct (int x);\n\n");               // for finding the product of digits in the x
       printf("0 - QUIT\n\n");               // for quiting the menu by user
              
       int choice;                   // this variable will store the choice value of the user
       printf("Please enter a selection: ");               // asking for the choice of the user
       scanf("%d",&choice);               // taking input of the choice of the user
      
       if(choice==1)           // if choice is 1, we'll call Factorial(n)
       {
           int n;  
           printf("\nEnter a positive integer : ");               // asking for the input of n
           scanf("%d",&n);                   // taking input of n
          
           if(n>=0)           // if requirement is ok, then call Factorial(n)
           {
               int result = Factorial(n);
               printf("\nAnswer: %d",result);
           }
           else               // otherwise printing the error message
           printf("\nError! please enter a positive integer.");
          
       }
       else if(choice==2)           // if choice is 2, we'll call Fibonacci(n)
       {
           int n;
           printf("\nEnter a positive integer : ");               // asking for the input of positive integer
           scanf("%d",&n);                   // taking input of positive integer
          
           if(n>=0)           // if requirement is ok, then call Fibonacci(n)
           {
               int result = Fibonacci(n);
               printf("\nAnswer: %d",result);
           }
           else               // otherwise printing the error message
           printf("\nError! please enter a positive integer.");
       }
       else if(choice==3)           // if choice is 3, we'll call Gcd(x,y)
       {
           int x,y;
           printf("\nEnter the first positive integer : ");
           scanf("%d",&x);
           printf("\nEnter the second positive integer : ");
           scanf("%d",&y);
          
           if(x>0 && y>0)           // if requirement is ok, then call Gcd(x,y)
           {
               int result = Gcd(x,y);
               printf("\nAnswer: %d",result);
           }
           else               // otherwise printing the error message
           printf("\nError! please enter positive integers.");
       }
       else if(choice==4)           // if choice is 4, we'll call Power(a,b)
       {
           int a,b;
           printf("\nEnter the first positive integer : ");
           scanf("%d",&a);                               // taking input of first positive integer
           printf("\nEnter the second positive integer : ");
           scanf("%d",&b);               // taking input of second positive integer
          
           if(a>0 && b>0)           // if requirement is ok, then call Power(a,b)
           {
               double result = Power(a,b);
               printf("\nAnswer: %lf",result);
           }
           else               // otherwise printing the error message
           printf("\nError! please enter positive integers.");
       }
       else if(choice==5)           // if choice is 5, we'll call digProduct(x)
       {
           unsigned int x;
           printf("\nEnter a positive integer : ");
           scanf("%d",&x);               // taking input of positive integer
          
           if(x>0)           // if requirement is ok, then call digProduct(x)
           {
               int result = digProduct(x);
               printf("\nAnswer: %d",result);
           }
           else               // otherwise printing the error message
           printf("\nError! please enter a positive integer.");
       }
       else if(choice==0)               // if user wants to exit the program
       {
           printf("\nGoodbye!");               // printing goodbye
           break;               // breaking the while loop for exiting the program
       }
       else               // if user's choice is invalid
       printf("\nInvalid Input.\n");               // printing invalid input by user
   }
}

Output :

Add a comment
Know the answer?
Add Answer to:
Objectives: - Practice designing/implementing algorithms - Practice use of functions Code and document the following functions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

    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...

    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...

    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...

    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...

    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...

    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....

    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,...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT