Question

Please do it in c++, will up vote!! create a recursive function to perform a factorial...

Please do it in c++, will up vote!!

create a recursive function to perform a factorial calculation.

- int factorial(const int value)

- return -1 if any negative number passed into the function

- Calculate the factorial of the number entered by the use

Determine value at which stack overflow occurs.

Homework Answers

Answer #1

The code followes will do the requirements specified in the question. Everey steps are explained using comments.

1) is without stack overflow and 2) is with stack overflow.

1) The code below will determine the factorial of a number without stack overflow:

#include<iostream>
using namespace std;
int factorial(const int n); //declaring factorial function
int main()
{
    int n; //declaring integer to get user input
    //asking student for input
    cout << "Enter a Integer: ";
    cin >> n;  //getting user iput to n
    //printing message and factorial calling fact function
    cout << "Factorial of " << n << " = " << factorial(n);

    return 0;
}
//defining fact function recrussive
int factorial(const int n)
{
    //cheking if number is 2 or greater
    if(n > 1)
        return n * factorial(n - 1);  //recrussively calling fame function by number-1  
    //checking if the number entered is -ve
    else if(n < 0)
        return -1;  //if entered -ve number returning -1
    else
        return 1;   //if number is 0 or 1 factorial = 1
}

OUTPUT (when enter a positive integer):

OUTPUT (when enter a -ve number):

OUTPUT (when entering 0 and 1):

2) The code below will determine the factorial when stack overflow occurs: (ie If the base case is not reached or not defined, then the stack overflow problem may arise.) In our case, if the return value is not defined for n =< 1 (factorial of 1 and 0 is 1), the stack overflow will raise. Check the code and output below for further clarification.

#include<iostream>
using namespace std;
int factorial(const int n); //declaring factorial function
int main()
{
    int n; //declaring integer to get user input
    //asking student for input
    cout << "Enter a Integer: ";
    cin >> n;  //getting user iput to n
    //printing message and factorial calling fact function
    cout << "Factorial of " << n << " = " << factorial(n);

    return 0;
}
//defining fact function recrussive
int factorial(const int n)
{
    //cheking if number is 2 or greater
    if(n > 1)
        return n * factorial(n - 1);  //recrussively calling fame function by number-1  
}

OUTPUT (when entering 3(+ve integer not 0 and 1)):

OUTPUT (when entering 0):

this answer is wrong cause factorial of 0 is 1. this is the problem with the stack overflow.

OUTPUT (when entering 1):

this remains same. and the answer is correct.

Know the answer?
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
Similar Questions
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Python recursive design question: Background: The class vote creates vote objects. A vote is either a...
Python recursive design question: Background: The class vote creates vote objects. A vote is either a blue vote, a red vote, or a purple. This information is stored in the "value" attribute. The function Poll returns a list of random votes. In the code below, some_poll is a list of 32 random votes. some_poll = Poll(32) # ----------------------------------------------------- import random # ----------------------------------------------------- # # Return a list of n random votes # # ----------------------------------------------------- def Poll(n=16): # # A random...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
1) Develop a C++ function that determines the average value of an array of type double...
1) Develop a C++ function that determines the average value of an array of type double elements double GetAverage(double array[], int size) The function should accept as input an array of double values The function should accept as input the number of elements in the array of double values The function should return a double value which is the array's average value 2) Develop a C++ function that determines the variance of an array of type double elements double GetVariance(double...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...