Hello everyone, this is my first time here on this website and forum, and after reading through some of it, I think it's very helpful, so I would like to say thank you.
I am facing a problem that I have been trying to solve for a few days so far with little success. I'm a beginner in C programming; I just started to learn it about a month ago on my own through some books. I'm mainly using the CodeBlocks program to write my programs.
I recently finished loops and after writing many programs as exercise, I am stuck on one that I just cant do and it is really annoying me. My code keeps getting mixed up and I have no idea how to tie it together to solve the problem. The functions I'm using seem flawed for some reason.
Here is the description of what I need to do:
Write a C program to compute the value of the mathematical constant e to the power of x (ex) by using the infinite series:
ex = 1 + x/1! + x2/2! + x3/3! + x4/4! + …
Your program should include at least one function called compute_ex which receives any value of x as a parameter and returns the value of ex as a result. Your program should also NOT use the already predefined system function pow.
The computation should stop when the new term added ( term = Xn/n! where n=0,1,2,3,…) is less than 0.0001.
Here is my work on it. I appologize for it being kind of messy as the program is incomplete:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Construct the user-defined functions used */
double compute_ex(double x);
int compute_factorial(int n);
int compute_power(int y);
void instruct_user(void);
void instruct_ending(void);
int main(void)
* Variable declarations */
double exponent;
/* Display user instructions */
instruct_user();
printf("Enter the desired exponent of e^x > e^");
scanf("%lf", &exponent);
double
compute_ex(double x);
{
int n;
int product;
product=1;
while(x<0.0001)
{
for(i=n; i>1;--1)
{
product=product*i}
}
}
}
/* Factorial computation functions */
int
compute_factorial(int n);
{
int i; /* Local variables */
int product_of_factorial; /* Accumulator for product computation */
product_of_factorial=1;
/* Computes the product n x (n-1) x (n-2) x ... x 2 x 1 */
for(i=n; i>1;--i)
{
product_of_factorial=product_of_factoria…
}
/* Returns fuction result */
return(product_of_factorial);
}
/* Power computation functions */
int
compute_power(int y, int a);
{
int j; /* Local variables */
int product_of_power; /* Accumulator for product computation */
int
/* Computes the product n x (n-1) x (n-2) x ... x 2 x 1 */
for(j=a; j>1;--j)
{
product_of_power=y*y;
}
/* Returns fuction result */
return(product_of_power);
}
I tried to ask some friends and they suggested the following way of writing the program. However, I find it too advanced for me, as it has something I do not know, and I want to do it more simply, so I can understand it that way before moving to something more advanced.
/* Write a C program that computes the value ex by using the formula
ex= 1 + x/1! + x2/2! + x3/3! + ....
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x,i;
float e=0.0;
long factorial(int); // Prototype declaration
float power(int,int); // Prototype declaration
clrscr();
printf("Enter the value of \'n\': ");
scanf("%d",&n);
printf("Enter the value of \'x\': ");
scanf("%d",&x);
for(i=0;i<=n;i++)
e += power(x,i)/(float)factorial(i);
printf("e^x = %-15.7f",e);
}
// Recursive factorial() function
long factorial(int n)
{
if(n<0)
return 0;
else if(n==0||n==1)
return((long)(1));
else
return((long)(n*factorial(n-1)));
}
// The power() function
float power(int a,int b)
{
int i,flag;
float p=1.0;
if(b<0)
b*=-1,flag=1;
else
flag=0;
for(i=1;i<=b;i++)
p *= a;
if(flag==0)
return p;
else
return (1/p);
}
I hope this is correct way of posting a question here since its my first time. Thank you in advance for any help you can provide me with.