Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is written in C. Write a program that implements the following func

ID: 3696437 • Letter: T

Question

This program is written in C.

Write a program that implements the following functions. long factorial(int n) double exponent(double x, int n) The functions implemented should follow below guidelines middot Factorial: Computes n! = n times (n - 1) times... times 1 middot Exponent: Computes the sum of first n terms of e^x using the following approximation. f(x, n) = e^x = sigma^n_i = 0 x^i/i! = x^0/0! + x^1/1! + x^2/2! +... + x^n/n! Read the value of n and x from the user and compute the first n terms of e^x using the function exponent. Print the result returned by the function and compare it with the value obtained by calling the math library function exp. When you increase the value of n your result should get closer to the result of exp. Sample execution of the program is given below Enter n and x 20 2.1 Approximation = 8.1753222282 Exact = 8.1661699126

Explanation / Answer


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
/*
* input: integer n>=0
* output: n! as a double
*/
double factorial(int n)
{
    //set initial resultF value to 1
    double resultF = 1;
    while (n>1)
    {
        //finds the factoral and decrements n
        resultF = resultF * n;
        n--;
    }
    //returns the result of the factoral
    return resultF;

}

/*
* input: double x and integer n>=0
* output: return approximation of e^x as a double
*         quality of approximation increases with n
*/
double exponent(double x, int n)
{
    int i;
  
    //sets the intital value of the result to 1
    double resultE = 1;
  
    //so long as n>0, the function will continue to approximate
    while (n>0)
    {
        resultE = pow(x, n)/factorial(n) + resultE;
        n--;
    }
    return resultE;
}

int main(void)
{
    int n, i;
    double x;
    double exactValue;
    double resultAll = 0;
  
    //get value of n from user, keep asking until n>0
    do {
        printf("Please enter the value for the integer n: ");
        scanf("%d", &n);
    } while ((n < 0)); {
  
    //to let the user know they entered a valid int  
    printf("Thanks! ");

    }
  
    printf("Please enter the value for the double x: ");
    scanf("%lf", &x);
  
    //header for the table
    printf("          i    Approximation        ");
    printf("---------------------------------- ");
  
    /*calls the exponent function to continuiously approximate the value of
    the natural exponent*/
    for (i=0;i<=n;i++) {
        resultAll = exponent(x, i);
        printf("%11i    %.10lf ", i, resultAll);  
    }
  
    //calculate the exact value of the natural exponent
    exactValue = exp(x);
    printf("         Exact Value: %lf", exactValue);
    return 0;
}


sample output

l:0

Please enter the value for the integer n: 20
Thanks!
Please enter the value for the double x: 2.1
          i    Approximation      
----------------------------------

          0    1.0000000000
          1    3.1000000000
          2    5.3050000000
          3    6.8485000000
          4    7.6588375000
          5    7.9991792500
          6    8.1182988625
          7    8.1540347462
          8    8.1634154157
          9    8.1656042386
         10    8.1660638914
         11    8.1661516433
         12    8.1661669999
         13    8.1661694806
         14    8.1661698527
         15    8.1661699048
         16    8.1661699116
         17    8.1661699125
         18    8.1661699126
         19    8.1661699126
         20    8.1661699126
         Exact Value: 8.166170

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote