Thread: trapezoidal rule

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    15

    trapezoidal rule

    Code:
     I am new to c programing and I had spend 2 days on a program and I can't fix the error:
            gcc Test.c -o Test.exe
    /tmp/ccZkbk0V.o: In function `f':
    Test.c:(.text+0x2f): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    
    the program I am working in is:
    
    #include <stdio.h>
    #include <math.h>
    int f(int x){
            return (sqrt(4.0 - x*x));
    }
    int main(void)
    {
            int a, b, N, width, i, sum,value, area;
            printf (" Enter the interval a an b");
            scanf ("%d\n%d\n", &a, &b);
            printf (" Enter the number of trapezoids you would like to use");
            scanf ("%d\n", &N);
            width = (b-a)/N;
            sum=(f(a*a) + f(b*b))/2;
            for (value = a + width; value < b; value += width)
                    sum += f(value);
            
            area = sum * width;
            printf (" The area under the curv is %d\n", area);
            return 0;
    }
     
    can some one help me dig myself out of a hole

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Link to the math library. The math library is called "m" on most GCC and other systems.
    The option -lm is what works for most systems; that is a lowercase L not an I.

    1st Suggestion to try
    Code:
    gcc Test.c -lm -o Test.exe
    2nd Suggestion to try
    Code:
    gcc Test.c -o Test.exe -lm
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    15
    thanks you for all your help it work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trapezoidal rule
    By howlin in forum C Programming
    Replies: 8
    Last Post: 03-06-2012, 12:02 PM
  2. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 09:00 AM
  3. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  4. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 10:01 AM
  5. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM