Making loops and writing functions

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.
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.

I'm not sure what you're trying to do with the Power computation function. But if you just want to do stuff like 3^3=27 you can use the pow() function which is included in math.h (or cmath.h, not sure right now).

Also when declaring a function, you shouldn't put a semicolon (;). This is only done when using prototypes.

Further in line 22 you never declared "instruct user". So if you declare that function you should be fine.

By the way, please put your code in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 tags, which makes it easier for us to help you :)

Cheers!

(your new code would now be, including new comments:


[code]#include <stdio.h>
#include <stdlib.h>



/* Construct the user-defined functions used */
double compute_ex(double x);  //here you do prototyping, so here you need a semicolon
int compute_factorial(int n);
int compute_power(int y, int a);

int main() //no need to place "void" here, since empty brackets mean the same
{
/* Variable declarations */

double exponent;
double result;


/* Display user instructions */

instruct_user();  //you never declared this

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; //just use return 0, it's quiker to write than (0) :)
}


/* Exponential computation functions */

double compute_ex(double x) //no semicolon here (!) 
{

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; //same here, you can just return sum, no need for brackets

}



/* Factorial computation functions */

int compute_factorial(int n) //no semicolon here either
{
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; //same as the other returns ;)
}


/* Power computation functions */


int compute_power(int y, int a) //no semicolon
{
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;
}


Please note I didn't try to figure what you're trying to do with this code (just looking for errors), so my suggestion about the pow function could be irrelevant.

By the way, try reading the errors your compiler gives, it's usually quite clear ;)

Cheers mate!
closed account (D80DSL3A)
The approach you are using will work if done properly. It looks like you are getting closer. If your program must handle non integer x though your integer power function won't work for that.
Also, watch your variable types. This line:
term = (compute_power( x, b))/(compute_factorial(b));
is int/int so it will return only 0,1,2,... never 0.1, etc. Casting the values to type double will fix that problem.

I wanted to point out that a MUCH simpler method can be used for this problem.
Note how each term in the series is related to the previous one:

term k+1 = ( term k )*x/(k+1)

You can find each term from the last without any need for a factorial function or your product_of_power function. The function e^x can be found this way using perhaps 10 lines of code!
Feel free to ask if you are interested.
Last edited on
Well, I managed to fix the errors and program ran fine, however, now I have a new problem: the program is not proporly handling fractions like 2.5 , 3.5 etc. Where exactly do I need to switch double and int definitions? Here is the final code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

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


    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 %.6f \n", exponent, result); 


   /* Ending notes */ 

    return(0); 
} 


/* Exponential computation functions */ 

double 
compute_ex(double x) 
{ 

    double term; 
    double sum; 
    int b; 

    term = 1.0; 
    sum = 1.0; 

    for(b = 1; term > 0.0001; b++) 
    { 
        term = (1.0*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); 
}  

closed account (D80DSL3A)
Changing int compute_power(int y, int a) to double compute_power(double y, int a) should help. You are already passing a double for y where you call this function anyway. Make product_of_power a double in that function. Consider doing the same with compute_factorial(). This will also help you avoid a possible overflow issue (value too high) because the factorial function grows so fast.
You need double/double on line 52. Have you learned about type casting yet?
Well thank you that helped, yet it wasnt the only thing that needed editing. The factorial function needed similar adjustments. Here is the final code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>



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

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

    double exponent;
    double result;


    /* Display user instructions */


    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 %.6f \n", exponent, result);
    }
    while( exponent != 0 );


   /* Ending notes */

    return(0);
}


/* Exponential computation functions */

double
compute_ex(double x)
{

    double term;
    double sum;
    int b;

    term = 1.0;
    sum = 1.0;

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

    }

    return(sum);

    }



/* Factorial computation functions */

double
compute_factorial(int n)
{
    int i;  /* Local variables */
    double product_of_factorial;    /* Accumulator for product computation */

    product_of_factorial=1.0;

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


double
compute_power(double y, int a)
{
    int j;  /* Local variables */
    double product_of_power;    /* Accumulator for product computation */

    product_of_power=1.0;

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


I added a loop to it to make it reuseable as well.

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. Fun2code, thank you very much mate, you have been a lot of help, I appriciate it! Answering your question, no, as a matter of fact, I havent heard about it before. Perhaps you can explain to me more about it, what it does, etc, and teach it to me? Cheers mate!
I haven't done c, but I think it might work similar to c++
so have a look at http://www.cplusplus.com/doc/tutorial/typecasting/ ;)
Topic archived. No new replies allowed.