Problem Scenario (credit: Engineering Problem Solving with C++, Delores Etter an
ID: 3586664 • Letter: P
Question
Problem Scenario (credit: Engineering Problem Solving with C++, Delores Etter and Jeanine Ingber. Pearson/Prentice Hall: 2008. PP.249):
The factorial (n!) of a non-negative integer value n is the product of all positive (non-negative) integers less than or equal to n. (ex. for n = 5, n! = 1 2 3 4 5) For n = 0, n! = 1.
To compute n! for large values of n it is sometimes convenient to use an approximation method called the Stirling formula, (http://en.wikipedia.org/wiki/Stirling%27s_approximation)
The base for natural logarithms () is approximated by the constant 2.718282.
You have been asked to write a computer program
1. to calculate values for n! for an unspecified number of user requests
using both methods (product multiplication and the Stirling formula) and
2. to determine the error between the two calculations
3. to determine the largest error value from all requests. Print the max value just before
the program ends.
If the user enters a negative integer value for n, the factorial should not be calculated.
To determine the error (#2 above), subtract the two calculated values and take the absolute value of the difference. For each valid (non-negative) request of n, echo back the value of n, the two calculated results and the error.
<cmath> Library Functions
Function name Argument type Return type Description
ceil double double ceil(x) returns the smallest integer >= x
cos double double cos(x) returns the cosine of x in radians
fabs double double fabs(x) returns the absolute value of x
floor double double floor(x) returns the largest integer <= x
pow 2 doubles double pow(x,y) returns x to the yth power
sin double double sin(x) returns the sine of x in radians
sqrt double double sqrt(x) returns the square root of x
Assignment Requirements:
(1 pt) The program should welcome the user and prompt the user for input data with clear understandable messaging. Input is from the keyboard and output is to the monitor.
Label all output well.
The program should print an exit message just before the program ends.
(5 pts) The program must include the required documentation and follow programming guidelines. See all notes from program assignment #2.
Include Identification Header,
Problem Specification and
Problem Analysis sections as comments.
Use a named constant for the approximated value of e (2.718282), and
also a named constant for the approximated value of ( 3.141593).
A sentinel control repetition must be used.
Example: “Enter non-negative value or -99 to end the program“
Example: “Do you wish to continue? Type Y or any key to end the program ”
(NOTE: special programming instructions needed for character input.)
For each n > 0, a counter-controlled repetition must be used for the inner loop to calculate n! using product multiplication.
Reminder
- The variable n is to be an integer data type.
- Compute n! only for non-negative values of n.
If the user enters a negative value for n, an appropriate error message should be displayed.
Use the predefined functions, sqrt(), pow(), fabs().
Reminder: #include <cmath>
When computing n! using the Stirling method, explicitly cast the integer n as double type in the arithmetic expression including usage with the library function calls.
The result will be a double type (for the sake of this program).
... static_cast<double>(n) ...
Of course, when computing n! using repetitive multiplication of the products, use n as an integer type. The result will be an integer type.
When determining the error, use explicit casting to temporarily cast the n! result from the product multiplication method to a double data type. Error variable should be double type.
Reminder: #include <iomanip>
Include the following as the first action statements (after variable definitions):
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
Explanation / Answer
factorial.cpp
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define PI 3.141593
#define E 2.718282
int factorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
}
double StirlingFact(double n)
{
double nbyE=pow(n/E, n);
double sqrtVal=sqrt(2 * PI * n);
double thirdTerm=(1 + 1.0/(12*n));
return sqrtVal * nbyE * thirdTerm;
}
int main(void)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
int num;
double max_diff=0.0;
cout<<"Welcome to factorial program..."<<endl;
cout<<"Enter non-negative value or -99 to quit"<<endl;
cin>>num;
while(num!=-99){
if(num<0)
cout<<"Please enter a non-negative value"<<endl;
else{
int f1=factorial(num);
double f2=StirlingFact(static_cast<double>(num));
double diff=fabs(static_cast<double>(f1)-f2);
cout<<"Given value:"<<num<<endl;
cout<<"Factorial value="<<f1<<endl;
cout<<"Stirling Factorial value="<<f2<<endl;
cout<<"Difference="<<diff<<endl;
if(max_diff<diff)
max_diff=diff;
}
cout<<"Enter non-negative value or -99 to quit"<<endl;
cin>>num;
}
cout<<"max difference observered="<<max_diff<<endl;
cout<<"Good bye. Have a nice day"<<endl;
return 0;
}
Output:
$g++ factorial.cpp
$ ./a.out
Welcome to factorial program...
Enter non-negative value or -99 to quit
8
Given value:8
Factorial value=40320
Stirling Factorial value=40318.0272735333
Difference=1.9727264667
Enter non-negative value or -99 to quit
6
Given value:6
Factorial value=720
Stirling Factorial value=719.9401487465
Difference=0.0598512535
Enter non-negative value or -99 to quit
9
Given value:9
Factorial value=362880
Stirling Factorial value=362865.7318743326
Difference=14.2681256674
Enter non-negative value or -99 to quit
12
Given value:12
Factorial value=479001600
Stirling Factorial value=478990535.4751656651
Difference=11064.5248343349
Enter non-negative value or -99 to quit
-99
max difference observered=11064.5248343349
Good bye. Have a nice day
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.