I AM HAVING A REALLY TOUGH TIME WITH THIS ASSIGNMENT AND ITS DUE TONIGHT. PLEASE
ID: 3796997 • Letter: I
Question
I AM HAVING A REALLY TOUGH TIME WITH THIS ASSIGNMENT AND ITS DUE TONIGHT. PLEASE HELP ITS IN THE C LANGUAGE. I would like an explination if possible.
(100 pts) Write a program that implements the following functions iteratively (no recursion).
double factorial(int n)
double exponent(double x, int n)
The functions implemented should follow below guidelines
• factorial: Computes n! = n × (n 1) × . . . × 1
• exponent: Computes the sum of first n terms of e x using the following approximation.
f(x, n) = e x = n i=0 x i i! = x 0 0! + x 1 1! + x 2 2! + . . . + x n n!
You can use pow() and f actorial() functions in your exponent function. In main() use argc and argv read the value of n and x from the user and compute and print the approximation of e x for all values up to n using the function exponent. Print the results as a table as shown below. Also, print the exact value of e x using the math library function exp(). When you increase the value of i your result should get closer to the result of exp. Name your program assign3.c Sample execution of the program is given below. First parameter is n and second parameter is x. You need to use functions atoi() and atof() in stdlib.h to convert strings to integer and double respectively.
i Approximation --------------------------------
0 1.0000000000
1 3.2000000000
2 5.6200000000
3 7.3946666667
4 8.3707333333
5 8.8002026667
6 8.9576747556
7 9.0071659835
8 9.0207760712
9 9.0241029815
10 9.0248349018
Exact Value = 9.0250134994
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//function to calculate factorial
double factorial(int n)
{
double f=1;
int i;
if(n==0 || n==1)
{
return 1;
}
else
{
for(i=n;i>1;i--)
{
f=f*i;
}
return f;
}
}
//function to calculate e
double exponent(double x, int n)
{
double expn=0;
int i;
for(i=0;i<=n;i++)
{
expn+=(i*pow(x,i))/factorial(i);
}
}
int main(int argc,char *argv[])
{
double x,f,e;
int n;
int i=0;
//converts first argument to int
n=atoi(argv[0]);
//converts 2nd argument to float
x=atof(argv[1]);
// f=factorial(n);
//e=exponent(x,n);
printf("i Approximation ");
for(i=0;i<=n;i++)
{
e=exponent(x,i);
printf("%d %lf ",i,e);
}
e=exponent(x,n);
printf("Exact value=%lf ",e);
return 0;
}
output:
i Approximation
0 1.0000000000
1 3.2000000000
2 5.6200000000
3 7.3946666667
4 8.3707333333
5 8.8002026667
6 8.9576747556
7 9.0071659835
8 9.0207760712
9 9.0241029815
10 9.0248349018
Exact Value = 9.0250134994
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.