help with function that calls another function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arperidot
    New Member
    • Feb 2010
    • 11

    help with function that calls another function

    Write a program which calculates the integral of the function

    f(x)=g(x)/n! , where g(x)=1+K*cos^n( n*x)

    on the interval from a to b (a<b). In the main program, scanf a
    non-negative integer value of n and double values of K, a, and b.
    Call the function Integ() to evaluate the integral.

    Your main program should be followed by four functions:

    double Integ(int n, double K, double a, double b)

    double func(double x, int n, double K) to evaluate f(x)

    double gunc(double x, int n, double K) to evaluate g(x)

    double nfact(int n) to evaluate n!

    integrate an arbitrary function f(x) from a to b (letting the
    program ask you for n_trap only once). Within the Integ() function
    call another function func() to specify f(x). Within the function
    func(), call the function nfact() to evaluate n!, and to
    to evaluate g(x) call the function gunc().

    This is my code so far, but ssh keeps prompting for errors that I can't find. Could someone tell me what's wrong with the code?


    Code:
    #include<stdio.h>
    #include<math.h>
    double Intgr(int n, double K, double a, double b);
    double func(double x, int n, double K;
    double gunc(double x, int n, double K);
    double mfact(int n);
    main()
    {
    printf("\n\n");
    
    int n, n_trap;
    double K, a, b, integral;
    
    printf("Enter the non-negative integer: ");
    scanf("%d", &n);
    printf("Enter the coefficient K in g(x): ");
    scanf("%lf", &K);
    printf("Enter the bounds for the integration interval, a < b : ");
    scanf("%lf %lf", &a, &b);
    printf("\n");
    printf("Integrate f(x)on [a,b]\n");
    
    integral = Intgr(n, K, a, b);
    printf("\nThe value of the integral is %.5f\n", integral);
    printf("\n\n");
    }
    
    double Intgr(int n, double K, double a, double b)
    {
            int k, n_trap, n1;
            double f, del_x, x, sum;
            printf("Enter the number of trapezoids: ");
            scanf("%d", &n_trap);
            n1 = n_trap + 1;
            del_x = (b - a)/n_trap;
            x = a;
            f = func(x,n,K);
            sum = -0.5 * f;
            for(k=0; k<n1; k++)
            {
             x = a + k * del_x;
             f = func(x,n,K);
             sum += f;
            }
            sum -= 0.5 * f;
            sum *= del_x;
            return sum;
    }
    
    double gunc(double x, int n, double K)
    {
            double g;
            g = 1+K*cos^n(n*x);
            return g;
    }
    
    double func(double x, int n, double K)
    {
            double factorial, y;
            if(n <= 0) y = K;
            else
            {
              factorial = mfact(n);
              y = gunc(x,n,K)/factorial;
            }
            return y;
    }
    
    
    double mfact(int n)
    {
            int i=1, n_fact=1;
            for(i=m; i>0; i--)
            {
             n_fact *= i;
            }
            return n_fact;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    When you say "ssh keeps prompting for errors" do you actually mean that you get errors from the compiler when you compile the code (an operation that you might be doing via ssh but that in fact has absolutely nothing to do with ssh).

    If this is the case then post the errors because those error message pretty much say exactly what is wrong.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      BTW line 53 is nothing like C syntax and lene 73 references an undeclared variable m.

      Comment

      • arperidot
        New Member
        • Feb 2010
        • 11

        #4
        thanks for the quick reply
        so i realized that i was supposed to use pow() for line 73
        g = 1+K*pow(cos,n)* (n*x);
        and changed m to n but i still get this error so i cant execute the program

        error: expected declaration specifiers or '...' before 'main'
        >>referencing to line 7

        Comment

        • arperidot
          New Member
          • Feb 2010
          • 11

          #5
          or actually g = 1+K*pow(cos(n*x ),n);

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            OK, I left C++ more than 2 years ago.

            But I'm asking if your function mfact should explicitly cast the int to double when it returns?

            You have declared mfact like this:
            double mfact(int n)
            But you return int not double at:
            int i=1, n_fact=1;
            return n_fact;
            Try declare n_fact as double and see if it works.

            Thanks,
            Bassem

            Comment

            • Bassem
              Contributor
              • Dec 2008
              • 344

              #7
              Hey,

              Plus what Banfa has reviewed, I see line #4 the declaration of the function not completed with ")" before semicolon.
              double func(double x, int n, double K;
              Thanks,
              Bassem

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                And at line 7 you have not declared the return type (int) of main.

                main should always be declared either as

                Code:
                int main()
                {
                }
                or
                Code:
                int main(int argc, char *argp[])
                {
                }

                Comment

                • arperidot
                  New Member
                  • Feb 2010
                  • 11

                  #9
                  thanks alot guys! that helped alot.
                  turns out my gunc() wasn't properly defined so i ended up computing g(x) directly on f(x)

                  double func(double x, int n, double K)
                  { double factorial, y;
                  factorial = mfact(n);
                  y = (1+K*pow(cos(n* x),n))/factorial;
                  return y; }

                  Comment

                  Working...