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.

#
for(j=a; j>1;--j)
#
{
#
product_of_power=y*y;
#

#
}

product_of_power=y*y ?
always return square(y)
you should implement product_of_power=product_of_power*y;

I did some fixing to my code and came up with the following:

#include <stdio.h>
#include <stdlib.h>



/* Construct the user-defined functions used */
double compute_ex(double x);
int compute_factorial(int n);
int compute_power(int y, int a);

int main(void)
{
     /* Variable declarations */

    double exponent;
    double result;


    /* Display user instructions */

    instruct_user();

    printf("Enter the desired exponent of e^x > e^ ");
    scanf("%lf", &exponent);

    result = compute_ex(exponent);

    printf("\n\n The value of e^ %.2f is \n", exponent, result);


   /* Ending notes */

    return(0);
}


/* Exponential computation functions */

double
compute_ex(double x);
{

    double term;
    double sum;
    int b;

    term = 1.0f;
    sum = 1.0f;

    for(b = 1; term > 0.0001f; b++)
    {
        term = (compute_power( x, b))/(compute_factorial(b));
        sum = sum + term;

    }

    return(sum);

    }



/* 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_factorial*i;

            }
   /* 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 */

    product_of_power=1;

         /* Computes the product of y to the power a (y^a) */

        for(j=0; j<a;j++)
        {
            product_of_power=product_of_power*y;

            }
   /* Returns fuction result */
    return(product_of_power);
}

However, I got many errors, can someone help me solve them? I dont know where the problem is, I'm very confused.

P.S. im working using codeblocks.

OK, your function definitions are causing some errors for me.

Whenever you are simply writing a function prototype, you use a semicolon. But when you are defining a function, you don't.

my_prototype( int a, char b );

my_function( int a )
{

}

After I did that, the code ran OK, but the answer was wrong because it seems to lose precision. I modified the line 53 to term = ([b]1.0*[/b]compute_power( x, b))/(compute_factorial(b)); ,
and then I got the right answer for e^2.

So overall, your program seems to be working pretty well.

OK, your function definitions are causing some errors for me.

Whenever you are simply writing a function prototype, you use a semicolon. But when you are defining a function, you don't.

my_prototype( int a, char b );

my_function( int a )
{

}

After I did that, the code ran OK, but the answer was wrong because it seems to lose precision. I modified the line 53 to term = ([b]1.0*[/b]compute_power( x, b))/(compute_factorial(b)); ,
and then I got the right answer for e^2.

So overall, your program seems to be working pretty well.

Thanks a lot! You are a life saver! It finally worked! After a spend over 4 hours trying to figure out where i was wrong, silly me. However there is a small problem, the program doesnt seem to be accepting any fractions like 2.5 or whatsoever, how can i fix it? And one more thing, how can I add a loop to it and where exactly to make this program reusable? I.E. give you a choice to enter a new value once you are done or a sentinel to stop? Thanks again.

I tried to change this

int
compute_power(int y, int a);
{    int j;  
int product_of_power

to

double
compute_power(double y, int a);
{    int j;  
doubleproduct_of_power

this seems to solve the problem in 2.5 and 3.5, however it makes munbers 4 and above give negative results, so i guess my try didnt work.

The negative values are probably coming from compute_factorial. The numbers there would get pretty large, so the integers are probably overflowing. Along with your changes, I replaced some ints with doubles and everything worked out pretty well.

double
compute_factorial(int n);
{
    int i;  /* Local variables */
    double product_of_factorial;

To make the program loop at input, you can try something like this:

do
{
    printf("Enter the desired exponent of e^x (use '0' to quit) > e^ ");
    scanf("%lf", &exponent);

    result = compute_ex(exponent);

    printf("\n\n The value of e^ %.2f is \n", exponent, result);
} while( exponent != 0 );

I hope this helps!

I would like to thank everyone who took time and effort to reply to my request and help me out. I really appriciate it. If anyone would have any advice or tips for me, I would be pleased to recieve them. I have been learning C by myself for about a month so far so there is plenty that I dont know, so anything that would help me learn something new is more than welcome from anyone, as I am trying to learn all I can, and its not as easy for a beginner. Once again thank you all!

P.S.

IsharaComix,

My hat goes off to you! I have been trying to solve this problem for a few days so far, and even asked soem friends, which didnt help, but you did. Thank you very much for your time and effort, I really appriciate everything you done for me. You seem to know a great deal about C programming, perhaps your could teach me a little? I could use all the help I could get. Actually, at the moment, I started to learn about pointers, arrays, and strings, any advice or tips on that? I find them quite confusing since I'm learning by myself. Do you know any good books/references I could study from? Once again thank you very much for everything, cheers mate!

I really like "Let us C" by Yashwant Kanetkar

This book assumes no previous knowledge of programming and teaches C programming in a very step step and methodical manner

I'm very happy I was able to help you, d00mhammers. I'm sorry to say that I probably can't teach you much, and I can't suggest any books or references.

Regardless, I wish you the best of luck in your studies. If you ever get stumped on another problem while learning C, please don't hesitate to start another thread, and I or one of the other fine folks here at DaniWeb will try to help you out.

You can also help US out by marking this thread as solved. ;)

#include<stdio.h>
#include<conio.h>
void main()
{  //exponentiial
  int i;
float s=1,t=1,x,r;
  printf("exponentiel of x Enter the value of x:-");
  scanf("%f",&x);
  r=x;
  for(i=1;i<=25;i++)
  {
    t=t*i;
    printf("%f\n",t);
    s=s+r*(1/t);
    r=r*x;
  }
  printf("%f\n",s);
  getch();
}

exponent shortest code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.