Calling functions, calculating integral

This is a discussion on Calling functions, calculating integral within the C Programming forums, part of the General Programming Boards category; I'm trying to call multiple functions to calculate an integral on the interval 0 < a < b. My code ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    Calling functions, calculating integral

    I'm trying to call multiple functions to calculate an integral on the interval 0 < a < b.

    My code compiles and it seems to call up the functions correctly but I get a weird number for an answer.

    This is my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    int n;
    double m, A, a, b;
    printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
    scanf("%lf %lf", &m,&n);
    printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
    scanf("%lf",&A);
    printf("\nEnter bounds for integration interval, a < b : ");
    scanf("%lf %lf", &a,&b);
    Intgr(m,n,A,a,b);
    }
    
    double Intgr(double m, int n, double A, double a, double b)
    {
    int n_trap, k;
    double del_x, x, f, sum;
    printf("\nIntegrate f(x) on [a,b]\n");
    printf("Enter the number of trapezoids: ");
    scanf("%d", &n_trap);
    del_x = (b-a)/(double)n_trap;
    x = a;
    f = func(x, m, n, A);
    sum = - 0.5 * f;
    for(k=0; k<=n_trap; k++)
    {
    x = a + k * del_x;
    f = func(x, m, n, A);
    sum += f;
    }
    sum -= 0.5 * f;
    sum *= del_x;
    printf("\nIntegral=%f\n", sum);
    }
    
    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){
           double f=1;
              f*=n--;
              return f;
    }
    This is my output:
    Code:
    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 bounds for integration interval, a < b : 1.5 3.75
    
    Integrate f(x) on [a,b]
    Enter the number of trapezoids: 1000
    
    Integral=2416590846.654750
    The value of the integral should be -0.792905 .

    I'm getting the following warnings when I compile:
    hw7.c:19: warning: type mismatch with previous implicit declaration
    hw7.c:15: warning: previous implicit declaration of `Intgr'
    hw7.c:19: warning: `Intgr' was previously implicitly declared to return `int'
    hw7.c:40: warning: type mismatch with previous implicit declaration
    hw7.c:27: warning: previous implicit declaration of `func'
    hw7.c:40: warning: `func' was previously implicitly declared to return `int'
    hw7.c:47: warning: type mismatch with previous implicit declaration
    hw7.c:44: warning: previous implicit declaration of `nfact'
    hw7.c:47: warning: `nfact' was previously implicitly declared to return `int'

    What do these errors mean and how do I make my Intgr function return the right value? Thank you very much.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You cannot (ok, should not, but look what happens) call a function without a prototype. Put a prototype of each function before int main(). Also, I really hope you don't believe that nfact() computes n! -- and also, it might be nice to just compute it once somewhere, since it's not as though it's changing.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    this is my modified code (declared prototypes, tried to fix the factorial equation):
    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);
    
    int main()
    {
    int n;
    double m, A, a, b;
    printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
    scanf("%lf %lf", &m,&n);
    printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
    scanf("%lf",&A);
    printf("\nEnter bounds for integration interval, a < b : ");
    scanf("%lf %lf", &a,&b);
    Intgr(m,n,A,a,b);
    }
    
    double Intgr(double m, int n, double A, double a, double b)
    {
    int n_trap, k;
    double del_x, x, f, sum;
    printf("\nIntegrate f(x) on [a,b]\n");
    printf("Enter the number of trapezoids: ");
    scanf("%d", &n_trap);
    del_x = (b-a)/(double)n_trap;
    x = a;
    f = func(x, m, n, A);
    sum = - 0.5 * f;
    for(k=0; k<=n_trap; k++)
    {
    x = a + k * del_x;
    f = func(x, m, n, A);
    sum += f;
    }
    sum -= 0.5 * f;
    sum *= del_x;
    printf("\nIntegral=%f\n", sum);
    }
    
    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){
    int t = 1;
    int as;
    for (as = 1; as<=n; as++) {
    t *=as;
    }
    return t;
    }
    It compiles with no warnings but my output seems to stop after I "Enter the number of trapezoids : 1000".
    My output:
    Code:
    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 bounds for integration interval, a < b : 1.5 3.75
    
    Integrate f(x) on [a,b]
    Enter the number of trapezoids: 1000
    And it freezes right there. Why is it not calculating my integral?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Well you could try beginning with indentation, so that people might want to look at your code. It's horrible.

    Here, consider how much easier this is to follow.
    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);
    
    int main()
    {
        int n;
        double m, A, a, b;
        printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
        scanf("&#37;lf %lf", &m,&n);
        printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
        scanf("%lf",&A);
        printf("\nEnter bounds for integration interval, a < b : ");
        scanf("%lf %lf", &a,&b);
        Intgr(m,n,A,a,b);
        //!! what returns here?
    }
    
    double Intgr(double m, int n, double A, double a, double b)
    {
        int n_trap, k;
        double del_x, x, f, sum;
        printf("\nIntegrate f(x) on [a,b]\n");
        printf("Enter the number of trapezoids: ");
        scanf("%d", &n_trap);
        del_x = (b-a)/(double)n_trap;
        x = a;
        f = func(x, m, n, A);
        sum = - 0.5 * f;
        for(k=0; k<=n_trap; k++)
        {
            x = a + k * del_x;
            f = func(x, m, n, A);
            sum += f;
        }
        sum -= 0.5 * f;
        sum *= del_x;
        printf("\nIntegral=%f\n", sum);
        //!! what returns here?
    }
    
    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){
        int t = 1;
        int as;
        for (as = 1; as<=n; as++) {
            t *=as;
        }
        return t;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    As Salem identified, you have two functions that don't return, also, there is not much use having Intgr return a double when you don't use its value. Either think of a value to return or return void, but always return something.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-26-2008, 02:12 AM
  2. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Functions calling themselves...?
    By mikebrewsj in forum C++ Programming
    Replies: 10
    Last Post: 01-17-2002, 11:09 PM
  5. Calling functions written in assembly?
    By The V. in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21