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...
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...
In C programming language please. -Construct a program that will make use of user defined functions,...
In C programming language please. -Construct a program that will make use of user defined functions, the do/while loop, the for loop and selection. -Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. -If the user enters in a 0 or negative number the program should exit with a message to the user indicating they...
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...
Use C code please! Write the function definition for a function called FindQuo that takes one...
Use C code please! Write the function definition for a function called FindQuo that takes one argument of type double (arg1 ) and returns a double.  The purpose of the function is to calculate the quotient of arg1 divided by a double (num) that will be entered by the user. Inside the function declare ask and get a double value from the user called num Use a loop (while or do/while) to ensure that the user does not enter a 0.0....
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...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...