Like Tree1Likes
  • 1 Post By quzah

Why my code is not working? Anyone can look at my code plz?!

This is a discussion on Why my code is not working? Anyone can look at my code plz?! within the C Programming forums, part of the General Programming Boards category; I am trying to use the Simpson's Method to calculate area under the curve using this function 2x^2+x but its ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    Why my code is not working? Anyone can look at my code plz?!

    I am trying to use the Simpson's Method to calculate area under the curve using this function 2x^2+x but its not working?

    Can some one help me and fix the problem? Its using Simpson's Method and want user to input a,b and N then calculate the area of the function given 2x^2+x

    insert
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <cstdlib>
    
    double simpson(double a, double b, int n);
    double fk(double x);
    //**********************************
    int main()
    {
    
    double a,f,b;
    int n;
    //Duomenys----------------
    printf("Enter value of a, example a=0\n a:");
    scanf("%lf", &a);
    //------------------------
    printf("Enter value of n, example n=7\n n:");
     scanf("%d",&n);
     printf ("Enter Value of b, example b=1\nb:");
     scanf("%lf", &b);
     f=simpson(a ,b , n); // Integralas
     //Rezultatas----------------
     printf("The result is f=%7.2f\n",f);
    
    }
    //**********************************
    double simpson(double a, double b, int n)
    {
        double c= (a+b)/2.0;
        double h3= abs(b-a)/6.0;
        double result= h3*(fk(a)+4.0*fk(c)+fk(b));
        return result;
    
    
    
    }
    //**********************************
    double fk(double x)
    {
    return x * x;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What isn't working about it?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    Code:
    double fk(double x)
    {
    return x * x;
    }
    That is not the function 2x^2 + x. It is simply x^2.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For doubles, use %lf, instead of %f. %f is for floats, not doubles.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    Quote Originally Posted by Adak View Post
    For doubles, use %lf, instead of %f. %f is for floats, not doubles.
    That is true for the scanf family of functions, which the OP is using correctly. It is not true for the printf family (which the OP is also using correctly), however. For printf, %f is for doubles, and the compiler will promote a float as needed. The l (ell) in %lf for printf has no effect.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    Why my code is not working? Anyone can look at my code plz?!

    I fixed it again and its still not working. ;(

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <cstdlib>
     
    double simpson(double a, double b, int n);
    double fk(double x);
    //**********************************
    int main()
    {
     
    double a,f,b;
    int n;
    //Duomenys----------------
    printf("Enter value of a, example a=0\n a:");
    scanf("%lf", &a);
    //------------------------
    printf("Enter value of n, example n=7\n n:");
     scanf("%d",&n);
     printf ("Enter Value of b, example b=1\nb:");
     scanf("%lf", &b);
     f=simpson(a ,b , n); // Integralas
     //Rezultatas----------------
     printf("The result is f=%7.2f\n",f);
     
    }
    //**********************************
    double simpson(double a, double b, int n)
    {
        double c= (a+b)/2.0;
        double h3= abs(b-a)/6.0;
        double result= h3*(fk(a)+4.0*fk(c)+fk(b));
        return result;
     
     
     
    }
    //**********************************
    double fk(double x)
    {
    return 2x * x+x;
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    Did you even try to compile that code? It doesn't work. Getting rid of your bogus conio.h and cstdlib includes, I get the following when I compile:
    Code:
    $ gcc -Wall -o simpson simpson.c
    simpson.c: In function ‘simpson’:
    simpson.c:30: warning: implicit declaration of function ‘abs’
    simpson.c:40:8: error: invalid suffix "x" on integer constant
    Line 30: abs is defined in stdlib.h, so you need a #include <stdlib.h> at the top
    Line 40: You can't multiply by sticking a number and variable next to each other. You need a * between them: 2 * x * x + x;

    Also, I don't see any use of absolute value in the Wikipedia page for Simpson's Rule, so I'm not sure whether you should use it in your code. Actually, it seems you shouldn't, since if I integrate by hand from 2 to 4, and from 4 to 2, I should get answers with the same magnitude but different signs.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kevave View Post
    I fixed it again and its still not working. ;(
    You still aren't explaining what doesn't work. Why should we help you when you don't bother to help us?


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by anduril462 View Post
    That is true for the scanf family of functions, which the OP is using correctly. It is not true for the printf family (which the OP is also using correctly), however. For printf, %f is for doubles, and the compiler will promote a float as needed. The l (ell) in %lf for printf has no effect.
    Thanks!

    Old habit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why this code is not working?
    By kapil1089thekin in forum C++ Programming
    Replies: 22
    Last Post: 09-05-2010, 01:41 AM
  2. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  3. code not working as it should..
    By transgalactic2 in forum C Programming
    Replies: 21
    Last Post: 12-05-2008, 01:10 PM
  4. Non-Working Code
    By SheilahT in forum C++ Programming
    Replies: 9
    Last Post: 09-19-2006, 04:04 PM
  5. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM

Tags for this Thread


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