# Homework (Beginner's Function Creation) + Unknown Variable

Printable View

• 11-15-2005
pinkcious
Homework (Beginner's Function Creation) + Unknown Variable
Code:

```/*Write a program which calculates the integral of the function                     f(x)=(A*x^m)/m! on the interval from a to b (a<b). In the main program, scanf a non-negative integer value of m and double values of A, a and b. Call the function Intgr() to evaluate the integral. Your main program should be followed by three functions: double Intgr(int m, double A, double a, double b) double func(double x, int m, double A) double mfact(int m) When writing the function Intgr(), use the program w5-10.c or the program from part B of your hw5, appropriately modified to integrate an arbitrary function f(x) from a to be (a is not necessarily equal to 0.0; also, let the program ask you for n_trap only once). Within Intgr() call another function func() to specify f(x). The return value of func() should be equal to A if m=0, or A*x^m/m! if m>0. To evaluate m! call the function mfact() that you will also write. To evaluate x^m call the function pow() already embedded in the math.h library. ................................................................ Your output should look like this: Enter the exponent m in f(x)=A*x^m/m! : 5 Enter the coefficient A in f(x)=A*x^m/m! : 2.5 Enter the bounds for the integration interval, a < b : 1.25 3.75 Integrate f(x) on [a,b] Enter the number of trapezoids: 1000 The value of the integral is 9.64272 . */ #include <stdio.h> #include <math.h> double Intgr(int m, double A, double a, double b); double func(double x, int m, double A); double mfact(int m); main() {       int n_trap, m;       double del_x, f, x, sum, A, a, b;       printf("Enter the exponent m in f(x)=A*x^m/m! :");       scanf("%d",&m);       printf("Enter the coefficient A in f(x)=A*x^m/m! :");       scanf("%lf",&A);       printf("Enter the bounds for the integration interval, a < b :");       scanf("%lf%lf",&a,&b);       printf("Integrate f(x) on [a,b]\n");       printf("Enter the number of trapezoids:");       scanf("%d",&n_trap);       printf("\nThe value of the integral is %.5f .\n",Intgr(m, A, a, b)); } double mfact(int m) {       int k,abs; printf("(mfact function) m is %d",m);       abs=1;       if(m!=0)       {             for(k=m;k>1;k--)             abs*=k; printf("(mfact function) abs is %d",abs);       }       else if(m=0)       {             abs=1; printf("(mfact function) k is %d",k);       }       else if(m<0)       {             printf("Error: Cannot take factorial of negative integer\n");             return;       } return abs; } double func(double x, int m, double A) {       if(m>=0)       { printf("(in func) A is %f\n",A); printf("(in func) x is %f\n",x); printf("(in func) m is %d\n",m); printf("(in func) mfact(m) is %f\n",mfact(m));           return A*pow(x,m)/mfact(m); printf("(in func) return is %f\n",A*pow(x,m)/mfact(m));       }       else       {           printf("Error: Cannot take factorial of negative number\n");           return;       } } double Intgr(int m, double A, double a, double b) {       int n_trap,k; double del_x,x,f,sum;       del_x=(b-a)/(double)n_trap;       x=0.;       f=func(x,m,A);       sum=0.;       for(k=0;k<=n_trap;k++) // Sum of x0 + x1 + x2 +...+x(n-1)+x(n)       {           x=((double)k*del_x)+a;           sum+=f;       }       for(k=1;k<n_trap;k++) // Sum of x1 + x2 +...+ x(n-2)+x(n-1)       {           x=((double)k*del_x)+a;           sum+=f;       }       sum*=0.5;       sum*=del_x;       return sum; }```
My problem (as of now) is that my 'x' is undefined. This isnt really a programming question, but what is x ( func() function)

Thanks for any help.
• 11-15-2005
pinkcious
and ignore the printf's along the way... I was just trying to see where my problem was.
• 11-15-2005
ExxNuker
you mean you are confused about the x in the f(x) = a*x^m/m! ? or are you confused about the x that is being declared by the program?
• 11-15-2005
pinkcious
basically, yes. func() returns 0 and I cant set an x until the integral part.
• 11-15-2005
ExxNuker
firstly, you are missing a return type for your main function:

Code:

```int main(){     //some code.....     return 0; }```
x is merely a placeholder for a value. In the case of evaluating an integral using the trapezoid method, the x (or delta x more appropriately) is the unit by which you increase the value used to evaluate the function, so you would start with f(a)+f(a+delta x)+....f(b)

you always have x available to the function func() so long as you have read in the values for the boundary conditions on the integral before calling func().

from reading the definitions, it seems to me that func() should return the first element in the summation. I think you might want to reconsider your definition of the function intgr(). It might also be prudent for you to look over your calculus text book and really try to throughly understand the trapezoid method....I don't get the impression from your code that you really understand it.