Like Tree1Likes
  • 1 Post By Salem

can someone help

This is a discussion on can someone help within the C Programming forums, part of the General Programming Boards category; Code: no matter what input i give the program it will output area equal to zero, I think my calculation ...

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

    can someone help

    Code:
    no matter what input i give the program it will output area equal to zero, I think my calculation are correct but I think it's not going through the loop or the communitcaion with my function is non-existence.
    Code:
     
    #include <stdio.h>
    #include <math.h>
    double f(int x){
            pow(x,2 );
            return 0;
    }
    int main(void)
    {
            int a, b, N, width, i, sum,value, area;
            printf (" Enter the interval a an b");
            scanf ("%d\n%d", &a, &b);
            printf (" Enter the number of trapezoids you would like to use");
            scanf ("%d", &N);
            width = (b-a)/N;
             
          sum=(f(a) + f(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;
    }
    

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > pow(x,2 );
    Where does this result go?

    > return 0;
    Where does this answer go?
    Does it look suspiciously like a zero to you?

    Perhaps say
    return pow(x,2 );
    Crossfire likes this.
    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.

Popular pages Recent additions subscribe to a feed

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