Question

In: Computer Science

Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them...

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.


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: 120

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!

Solutions

Expert Solution

#include <stdio.h>

// function to calculate factorial of number
int Factorial(int n) {

   // if number is negative
   if(n < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }

   // calculate factorial
   int fact = 1;
   for(int i = 1; i <= n; i++) {
      fact = fact * i;
   }

   // return the result
   return fact;
}


// function to find the fibonacci number
int Fibonacci(int n) {

   // if n is negative 
   if (n < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }

   // declaring and initializing variables
   int first = 0;
   int second = 1;
   int next = 1;

   if(n == 0) {
      return first;
   }
   else if(n == 1) {
      return second;
   }
   else{

      // calculting the nth fibonacci number
      for(int i = 2; i <= n; i++) {
         next = first + second;
         first = second;
         second = next;
      }
   }

   // return the result
   return next;
}


// function to calculate the gcd of two number
int gcd(int x, int y) {

   // if numbers are less than 1
   if(x < 1 || y < 1) {
      printf("Invalid input!!\n\n");
      return -1;
   }

   // calculating gcd

   // while both number are not equal
   while(x != y) {

      // if x is greater
      if(x > y){
         x = x - y;
      }

      // if y is greater
        else {
         y = y - x;
      }
    }

   // return the result
   return x;
}


// function to calculate the a to the power b
double Power(int a, int b) {

   // validating input
   if(a < 1 || b < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }

   double result = 0;

   // if b is 0 which mean a raised to power 0 which is always 1
   if(b == 0) {
      return 1;
   }
   else {

      // calculating the power
      for(int i = 1; i < b; i++) {
         result = result + (a * a);
      }
   }

   // return the result
   return result;
}


// function to calculate the product of the digits of x
int digProduct(int x) {

   // if x is negative
   if(x < 1) {
      printf("Invalid input!!\n\n");
      return -1;
   }

   // if x is a single digit number
   if(x <= 9) {
      return x;
   }

   int result = 1;

   // while x is positive
   while(x > 0) {

      // calcualting product
      result = result * (x % 10);

      // reducing x by a place
      x = x / 10;
   }

   // return the result
   return result;
}


// main function to drive the code
int main(void) {

   // using loop to run the code until user wants to quit
   while(1) {

      // display menu
      printf("Lab 1\n");
      printf("1 - int Factorial(int n);\n");
      printf("2 - int Fibonacci(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");

      // read user option
      int option;
      printf("Please enter a selection: ");
      scanf("%d", &option);

      // validate option
      if(option < 0 || option > 5) {
         printf("Invalid Input.\n\n");
         continue;
      }

      // perform the required operation as option selected
      else {
         if(option == 0) {
            printf("Goodbye!");
            break;
         }
         else if(option == 1) {
            int n;
            printf("Enter a positive Integer: ");
            scanf("%d", &n);

            printf("Answer: %d\n\n", Factorial(n));
         }
         else if(option == 2) {
            int n;
            printf("Enter a positive Integer: ");
            scanf("%d", &n);

            printf("Answer: %d\n\n", Fibonacci(n));
         }
         else if(option == 3) {
            int x, y;
            printf("Enter first positive Integer: ");
            scanf("%d", &x);
            printf("Enter second positive Integer: ");
            scanf("%d", &y);

            printf("Answer: %d\n\n", gcd(x, y));
         }
         else if(option == 4) {
            int a, b;
            printf("Enter first positive Integer: ");
            scanf("%d", &a);
            printf("Enter second positive Integer: ");
            scanf("%d", &b);

            printf("Answer: %lf\n\n", Power(a, b));
         }
         else if(option == 5) {
            int x;
            printf("Enter a positive Integer (greater than 0): ");
            scanf("%d", &x);

            printf("Answer: %d\n\n", digProduct(x));
         }
      }
   }
}


FOR HELP PLEASE COMMENT.
THANK YOU.


Related Solutions

Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
write both non-recursive and recursive functions that take the strings from the user and display strings...
write both non-recursive and recursive functions that take the strings from the user and display strings in backwards in python
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Using MongoDB, what command would you enter to retrieve this document using only the zip code...
Using MongoDB, what command would you enter to retrieve this document using only the zip code 11242? db.inspections.find(???????) Database: city Collection: inspections {"_id":{"$oid":"56d61033a378eccde8a898ae"}, "id":"23536-2015-ENFO", "certificate_number":5373970, "business_name":"NZO CORP.", "date":"Apr 22 2015", "result":"Violation Issued", "sector":"Grocery-Retail - 808", "address":{"city":"BROOKLYN", "zip":11242, "street":"COURT ST", "number":26}}
Develop an iteration schedule diagram using the information provided in the attached document. Create this schedule...
Develop an iteration schedule diagram using the information provided in the attached document. Create this schedule using either Microsoft Project or ProjectLibre. Discipline Activity Effort Planning Set up work environment 2 days Develop WBS and plan work 1 day Analysis Activities Meet with Community Board users 2 days Meet with several agents 2 days Define data 1 day Model use cases 2 days Design Activities Design screen layouts 3 days Design Database 1 day Design use case processing 3 days...
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove the garbage data printing as discussed in class. Code: char data; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: while(Serial.available()==NULL){ data = Serial.read(); Serial.print("given character is: "); Serial.println(data); } Serial.end(); }
1. Using the following code identify the fault. 2. Using the following code indentify a test...
1. Using the following code identify the fault. 2. Using the following code indentify a test case that results in an error, but not a failure. public static int lastZero (int[] x) { //Effects: if x == null throw NullPointerException //else return the index of the last 0 in x. //Return -1 if 0 does not occur in x for (int i = 0; i < x.length; i++) { if (x[i] == 0) { return i; } } return -1;...
Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT