Question

CS 153 Program 4 - Stirling Approximation (separate compilation) 1. Write a pure function double factoriallint Ni that comput

program in C. please paste code, thanks!

1. Write a pure function double factoriallint Ni that computes NI | IN = 1*2*3*4* *(N-1)*N Do this the obvious way by initial

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code :-

stirlingFun.h

double myPow(double x,int n);
double factorial(int N);
double stirling(int N);

stirlingFun.c

#include<math.h>
#include"stirlingFun.h"
double factorial(int N)
{
   int count;
   double product = 1;
   for (count = 1 ; count <= N ;count++)
   {
       product = product * count;
   }
   return product;
}
double stirling(int N)
{
   double rootTerm=0.0,powerTerm=0.0,product=0.0;
   rootTerm = sqrt(2*M_PI*N);
   powerTerm = N/M_E;
   powerTerm = myPow(powerTerm,N);
   product = rootTerm*powerTerm;
   return product;
}
double myPow(double x,int n)
{
   //Assuming n is positive integer
   double power = 1;
   int count;
   for (count = 1;count<=n;count++)
   {
       power = power * x;
   }
   return power;
}

stirlingMain.c

#include<stdio.h>
#include"stirlingFun.h"
int main()
{
   double pureFactorial=0.0,stirlingFactorial=0.0;
   int number,count;
   printf("Enter a number : ");
   scanf("%d",&number);
   printf("\tn\t\tn!\t\tapprox.\t\t\n");
   printf("-------------+-------------------+------------------------\n");
   for(count = 0;count<=number;count++)
   {
       pureFactorial = factorial(count);
       stirlingFactorial = stirling(count);
       printf("\t%3d %20.0f %20.4f\n",count,pureFactorial,stirlingFactorial);
   }
   return 0;
}

Note :- 20.0 means float with 0 decimal places

Output :-

C: \2020\27-02-2020>gcc - Wall stirlingFun.c stirlingMain.c -o file C:\ 12020\27-02-2020>file.exe Enter a number : 15 approx.


Add a comment
Know the answer?
Add Answer to:
program in C. please paste code, thanks! CS 153 Program 4 - Stirling Approximation (separate compilation)...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that computes Pi with the approximation algorithm that I introduced in class....

    Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of adjacent elements (remember...

  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

  • Exercise 5.5 (EASY) for beginning CS CLASS PART A Copy or cut and paste program P5_2.cpp...

    Exercise 5.5 (EASY) for beginning CS CLASS PART A Copy or cut and paste program P5_2.cpp (located below) to a new program called ex55.cpp. Make PI a constant value. Compile and run the program for the following values: r = 2 cm, h = 10 cm The correct answer should be:       The cross-section area of the cylinder is 3.89556 inch-sq       The side area of the cylinder is 19.4778 inch-sqr Did you get the correct answer? You didn't! Explain...

  • The class declaration (interface) and the function definitions (implementation) must be in separate files - the...

    The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private. Write a class called BankAccount that has: a string data member called customerName, a string data member called customerID, and a double data member called customerBalance a constructor that takes two strings and a double (name, ID, balance) and uses them...

  • public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which...

    public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which does the following: In the main method: Use a for loop which repeats three times. Within the loop, a. prompt the user to enter the base of the triangular prism and store the value in a double array called base b. prompt the user to enter the corresponding height and store the value in a double array called height c. prompt the user to...

  • C++ code and also provide comments explaining everything Credit Card Debt The True Cost of Paying...

    C++ code and also provide comments explaining everything Credit Card Debt The True Cost of Paying Minimum Payment Write a C++ program to output the monthly payment schedule for a credit card debt, when each month nothing more is charged to the account but only the minimum payment is paid. The output stops when the balance is fully paid - remaining balance = 0. Input: Data input must be done in a separate function. Input the following: credit card balance,...

  • Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

    Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

  • Please help code in c++ and use the answers you get from question 1 and 2...

    Please help code in c++ and use the answers you get from question 1 and 2 into the 3rd answer! Question 1 is first, question 2 is in the middle, question 3 is last Input Array (10 pts) te a function only (no main) that takes an array of doubles as a parameter as well as another integer rray. Create a for loop that asks the user to input any real number between-100 and r each element of the array....

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT