shadowing a parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wuzertheloser
    New Member
    • Oct 2006
    • 22

    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 .

    */
    Code:
    #include <stdio.h>
    #include <math.h>
    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);
    main()
    {
    double m, A, a, b, x, del_x, sum, f;
    int n, k, n_trap;
    printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
    scanf("%f %d", &m, &n);
    printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
    scanf("%f", &A);
    printf("\nEnter the bounds for the integration interval, a < b : ");
    scanf("%f %f", &a, &b);
    
    
    printf("Integrate f(x) on [a,b]\n");
    printf("Enter the number of trapezoids : ");
    scanf("%d", &n_trap);
    printf("\nn! is : %g\n", nfact(n));
    printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
    }
    
    double Intgr(double m, int n, double A, double a, double b)
    {
    double m, A, a, b;
    int n;
    del_x = (b-a)/n_trap;
    x = a;
    f = A*pow(x,m)/nfact(n);
    sum = -0.5 * f;
    double m, A, a, b;
    int n;
    del_x = (b-a)/n_trap;
    x = a;
    f = A*pow(x,m)/nfact(n);
    sum = -0.5 * f;
    for(k=0; k<=n_trap; k++)
    {
    x = k * del_x;
    f = A*pow(x,m)/nfact(n);
    sum +=f;
    }
    sum -= 0.5 * f;
    sum *= del_x;
    }
    
    double func(double x, double m, int n double A)
    {
    double x, m, A;
    int n;
    if(n==0) return A*pow(x,m);
    else return (A*pow(x,m))/nfact(n);
    }
    
    double nfact(int n)
    {
    if(n==1)return 1;
    else return n*nfact(n-1);
    }
    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
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    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).

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      this is ur code corrected!
      pay attention to redeclaration NO NEED!
      PS: dont pay attention to the 3 warnings that u will have! ;)
      Code:
      #include <stdio.h>
      #include <math.h>
      double Intgr(m, n, A, a, b);
      double func(x, m, n, A);
      double nfact(n);
      double m, A, a, b, x, del_x, sum, f;
      int n, k, n_trap;
      main()
      {
      printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
      scanf("%f %d", &m, &n);
      printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
      scanf("%f", &A);
      printf("\nEnter the bounds for the integration interval, a < b : ");
      scanf("%f %f", &a, &b);
      
      
      printf("Integrate f(x) on [a,b]\n");
      printf("Enter the number of trapezoids : ");
      scanf("%d", &n_trap);
      printf("\nn! is : %g\n", nfact(n));
      printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
      }
      
      double Intgr(double m, int n, double A, double a, double b)
      {
      del_x = (b-a)/n_trap;
      x = a;
      f = A*pow(x,m)/nfact(n);
      sum = -0.5 * f;
      del_x = (b-a)/n_trap;
      x = a;
      f = A*pow(x,m)/nfact(n);
      sum = -0.5 * f;
      for(k=0; k<=n_trap; k++)
      {
      x = k * del_x;
      f = A*pow(x,m)/nfact(n);
      sum +=f;
      }
      sum -= 0.5 * f;
      sum *= del_x;
      }
      
      double func(double x, double m, int n, double A)
      {
      if(n==0) return A*pow(x,m);
      else return (A*pow(x,m))/nfact(n);
      }
      
      double nfact(int n)
      {
      if(n==1)return 1;
      else return n*nfact(n-1);
      }

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        plz mention ur compiler next time !
        thx!

        Comment

        • wuzertheloser
          New Member
          • Oct 2006
          • 22

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

          Comment

          • wuzertheloser
            New Member
            • Oct 2006
            • 22

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

            Comment

            • wuzertheloser
              New Member
              • Oct 2006
              • 22

              #7
              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?

              Comment

              • momotaro
                Contributor
                • Sep 2006
                • 357

                #8
                u r right! ;)
                good luck!

                Comment

                • wuzertheloser
                  New Member
                  • Oct 2006
                  • 22

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

                  Comment

                  • REICC
                    New Member
                    • Nov 2007
                    • 3

                    #10
                    I think it has to do with something in the first part. Any suggestions, guys?

                    Comment

                    • REICC
                      New Member
                      • Nov 2007
                      • 3

                      #11
                      also, why are you declaring the variables before main? Sorry, I am a noob

                      Comment

                      • seforo
                        New Member
                        • Nov 2006
                        • 60

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

                        Comment

                        Working...