Write a program that prompts the user for a positive integer n and outputs n!, i
ID: 3634013 • Letter: W
Question
Write a program that prompts the user for a positive integer n and outputs n!, its Stirling approx-
imation, and the relative error of the approximation given by
The program is composed of three files: auxiliary.h, auxiliary.c, stirling.c.
auxiliary.h
This header file should be exactly as follows, do not alter any of its contents:
//auxiliary.h
double Exp(double x);
int Factorial(int n);
auxiliary.c
This source file implements the functions in auxiliary.h as follows.
int Factorial(int n)
This function computes n! in a recursive manner.
double Exp(double x)
This function computes ex using its power series expansion:
When calculating the terms, note that the (i + 1)'st term can be calculated from the i'th by
stirling.c
This file starts by including "auxiliary.h" and any other necessary library headers. It consists
of a single function, int main() that asks the user to input a positive integer. It then calls the
functions in auxiliary.c to compute the exact value of n! and its Stirling approximation. Finally,
it calculates the relative error defined in (1), and prints all three numbers as in the sample runs,
with the last two printed with 4-decimal point precision. Calculate using the atan function in
math.h ( = 4*atan(1.)). Use Exp(1) to approximate e.
Ex)
Enter a positive integer: (output) 4(input)
Exact=24 (ouput)
stirling=23.5062 (output)
Error=0.0206 (output)
Enter a positive integer: (output) 7(input)
Exact=5040 (output)
Stirling=4980.3997 (output)
Error=0.0118 (output)
Enter a positive integer: (output) 10(input)
Exact=3628800 (output)
Stirling=3598699.6286 (output)
Error=0.0083(output)
Explanation / Answer
auxiliary.c: #include #include #define NUM_ITERATIONS 10 int Factorial(int n){ if(nRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.