469,645 Members | 1,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

shadowing a parameter

Write a program which calculates the integral of the function

f(x)=(A*x^m)/n!

on the interval from a to b (0<a<b). In the main program, scanf
a double value for m and a non-negative integer value for n,
a double value of A, and positive double values for a and b>a.
Call the function Intgr() to evaluate the integral.

Your main program should be followed by three functions:

double Intgr(double m, int n, double A, double a, double b)

double func(double x, double m, int n, double A)

double nfact(int n)


When writing the function Intgr(), use the program w4-10.c
or the program from part B of your hw5, appropriately modified
to integrate an arbitrary function f(x) from a to b
(let the program ask you for n_trap only once, and scan
sufficiently large value for n_trap). Within Intgr() call another
function func() to specify f(x). The return value of func() should
be equal to A*x^m if n=0, or A*x^m/n! if n>0. To evaluate n! call the
function nfact() 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 exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000

The value of the integral is -0.792905 .

*/
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double Intgr(double m, int n, double A, double a, double b);
  4. double func(double x, double m, int n, double A);
  5. double nfact(int n);
  6. main()
  7. {
  8. double m, A, a, b, x, del_x, sum, f;
  9. int n, k, n_trap;
  10. printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
  11. scanf("%f %d", &m, &n);
  12. printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
  13. scanf("%f", &A);
  14. printf("\nEnter the bounds for the integration interval, a < b : ");
  15. scanf("%f %f", &a, &b);
  16.  
  17.  
  18. printf("Integrate f(x) on [a,b]\n");
  19. printf("Enter the number of trapezoids : ");
  20. scanf("%d", &n_trap);
  21. printf("\nn! is : %g\n", nfact(n));
  22. printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
  23. }
  24.  
  25. double Intgr(double m, int n, double A, double a, double b)
  26. {
  27. double m, A, a, b;
  28. int n;
  29. del_x = (b-a)/n_trap;
  30. x = a;
  31. f = A*pow(x,m)/nfact(n);
  32. sum = -0.5 * f;
  33. double m, A, a, b;
  34. int n;
  35. del_x = (b-a)/n_trap;
  36. x = a;
  37. f = A*pow(x,m)/nfact(n);
  38. sum = -0.5 * f;
  39. for(k=0; k<=n_trap; k++)
  40. {
  41. x = k * del_x;
  42. f = A*pow(x,m)/nfact(n);
  43. sum +=f;
  44. }
  45. sum -= 0.5 * f;
  46. sum *= del_x;
  47. }
  48.  
  49. double func(double x, double m, int n double A)
  50. {
  51. double x, m, A;
  52. int n;
  53. if(n==0) return A*pow(x,m);
  54. else return (A*pow(x,m))/nfact(n);
  55. }
  56.  
  57. double nfact(int n)
  58. {
  59. if(n==1)return 1;
  60. else return n*nfact(n-1);
  61. }
  62.  
  63.  
everytime i compile, i get these error messages:

iacs5.ucsd.edu% !g
gcc hw7.c -lm
hw7.c: In function `Intgr':
hw7.c:77: warning: declaration of `m' shadows a parameter
hw7.c:77: warning: declaration of `A' shadows a parameter
hw7.c:77: warning: declaration of `a' shadows a parameter
hw7.c:77: warning: declaration of `b' shadows a parameter
hw7.c:78: warning: declaration of `n' shadows a parameter
hw7.c:79: error: `del_x' undeclared (first use in this function)
hw7.c:79: error: (Each undeclared identifier is reported only once
hw7.c:79: error: for each function it appears in.)
hw7.c:79: error: `n_trap' undeclared (first use in this function)
hw7.c:80: error: `x' undeclared (first use in this function)
hw7.c:81: error: `f' undeclared (first use in this function)
hw7.c:82: error: `sum' undeclared (first use in this function)
hw7.c:83: error: `k' undeclared (first use in this function)
hw7.c: At top level:
hw7.c:93: error: parse error before "double"
hw7.c: In function `func':
hw7.c:94: error: number of arguments doesn't match prototype
hw7.c:54: error: prototype declaration
iacs5.ucsd.edu% vi hw7.c
Version SVR4.0, Solaris 2.5.0
"hw7.c" 105 lines, 2783 characters
Nov 10 '06 #1
11 3974
sicarie
4,677 Expert Mod 4TB
First of all, in double func() you need a comma between the last two variables you are passing it. I also don't think you should be redeclaring the amounts you are passing to it.

It's the same thing with Intgr() and from there, you should be able to debug from the output statements (gets you down to about 15, but they look manageable at first glance - but let us know if you start having trouble with them).
Nov 10 '06 #2
momotaro
357 100+
this is ur code corrected!
pay attention to redeclaration NO NEED!
PS: dont pay attention to the 3 warnings that u will have! ;)
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double Intgr(m, n, A, a, b);
  4. double func(x, m, n, A);
  5. double nfact(n);
  6. double m, A, a, b, x, del_x, sum, f;
  7. int n, k, n_trap;
  8. main()
  9. {
  10. printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
  11. scanf("%f %d", &m, &n);
  12. printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
  13. scanf("%f", &A);
  14. printf("\nEnter the bounds for the integration interval, a < b : ");
  15. scanf("%f %f", &a, &b);
  16.  
  17.  
  18. printf("Integrate f(x) on [a,b]\n");
  19. printf("Enter the number of trapezoids : ");
  20. scanf("%d", &n_trap);
  21. printf("\nn! is : %g\n", nfact(n));
  22. printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
  23. }
  24.  
  25. double Intgr(double m, int n, double A, double a, double b)
  26. {
  27. del_x = (b-a)/n_trap;
  28. x = a;
  29. f = A*pow(x,m)/nfact(n);
  30. sum = -0.5 * f;
  31. del_x = (b-a)/n_trap;
  32. x = a;
  33. f = A*pow(x,m)/nfact(n);
  34. sum = -0.5 * f;
  35. for(k=0; k<=n_trap; k++)
  36. {
  37. x = k * del_x;
  38. f = A*pow(x,m)/nfact(n);
  39. sum +=f;
  40. }
  41. sum -= 0.5 * f;
  42. sum *= del_x;
  43. }
  44.  
  45. double func(double x, double m, int n, double A)
  46. {
  47. if(n==0) return A*pow(x,m);
  48. else return (A*pow(x,m))/nfact(n);
  49. }
  50.  
  51. double nfact(int n)
  52. {
  53. if(n==1)return 1;
  54. else return n*nfact(n-1);
  55. }
  56.  
Nov 11 '06 #3
momotaro
357 100+
plz mention ur compiler next time !
thx!
Nov 11 '06 #4
okay, thanks for the help everyone! i appreciate it =] i'll let you know how everything goes.

i'm not quite sure which compiler i'm using because it comes from my professor's website.

the program is called
SSH secure shell
and it uses vi editor to make the C files. not sure if that helps.
Nov 11 '06 #5
oh just for clarification
what does the function
double Intgr() output?

i need it to output the sum, but i don't think it's doing that.
Nov 11 '06 #6
program works fine now
only problem is the output i get for the integral

my output:


Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : 3.25 5

Enter the coefficient A in f(x) = A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids : 1000

The value of the integral is -5.87171e+151

correct output:


Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter the bounds for the integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000

The value of the integral is -0.792905 .


I think this has something to do in my Intgr function correct?
Nov 11 '06 #7
momotaro
357 100+
u r right! ;)
good luck!
Nov 12 '06 #8
any idea how you would integrate
A*x^m/n! ?

mathematically i know how to do it. but the only inputs i'm allowed to have are A, m, and n. Therefore, x would have no defined value.
Nov 13 '06 #9
REICC
3
I think it has to do with something in the first part. Any suggestions, guys?
Nov 23 '07 #10
REICC
3
also, why are you declaring the variables before main? Sorry, I am a noob
Nov 24 '07 #11
seforo
60
Any function that is defined below the main function should have its function prototype declared before the main function. Otherwise you should define (or write) all your functions before defining main function.
Nov 24 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Aguilar, James | last post: by
2 posts views Thread by Mark Sisson | last post: by
3 posts views Thread by Nick Stansbury | last post: by
5 posts views Thread by Dave Taylor | last post: by
2 posts views Thread by David Garamond | last post: by
1 post views Thread by John Salerno | last post: by
16 posts views Thread by hzmonte | last post: by
3 posts views Thread by Cartoper | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.