Thread: C programming help0 Simpson Rule

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    C programming help0 Simpson Rule

    HI I am new to C programing and i am trying to do simpson rule in C . But its crashing when I enter a value to the prompt "Enter number of sub-intervals 'N' (must be even):"

    please help

    Here is the code



    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <math.h>
    double f(double x); // function prototype
    float simpson(float a, float b, int n);		// funt prototype
    
    
    int main()
    {
    	int n; //
    	float a,b;
    	// Program Instructions
    
               printf("\n\nPlease enter lower integration limit:");
               scanf("%lf",&a);
               printf("Please enter upper integration limit:");
               scanf("%lf",&b);
                printf("Enter number of sub-intervals 'N' (must be even):");
               scanf("%d",&n);
               if((n%4)!=0)
               {
                   printf("Number of sub-intervals must be a multiple of 4 \n");
                   exit(1);
               }
    
    
     //printf("a=%.10f",a); printf("b=%.10f",b); printf("n=%.l0f",n0);
    	float SN; // SN will hold the next value of S
    	SN = simpson(a, b, n);
    	printf("Integral=",SN);
    	return 0;
    
    }
    
    
    double f(double x)
    {
    	return sin(x);
    }
    
    
    
    float simpson(float a, float b, int n)
    {
    	n = 2 * n;
    	float x;
    float h = (b - a) / n;
    float  S = f(a);
    
     for( int i;i<n-1;i+2)
     {
             x = a + (h* i);
        S = S + (4 * f(x));
     }
    
      for( int i;i<n-2;i+2)
    
    {
    
        x = a + (h* i);
        S = S + (2 * f(x));
    }
    
    S = S + f(b);
    //F = (h * S )/ 3;
    return ((h * S )/ 3);
    }
    Last edited by Salem; 09-22-2010 at 03:03 PM. Reason: Use code tags, not quote tags when posting code

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    First of all, you don't want to include "iostream.h" you need <stdio.h> for this. Secondly, maybe scanf is the problem. Sombody?
    Last edited by Syscal; 09-22-2010 at 02:39 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try compiling with a decent level of warnings.
    Code:
    $ gcc -std=c99 -W -Wall foo.c
    foo.c:2:22: error: iostream.h: No such file or directory
    foo.c: In function ‘main’:
    foo.c:14: warning: implicit declaration of function ‘printf’
    foo.c:14: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:15: warning: implicit declaration of function ‘scanf’
    foo.c:15: warning: incompatible implicit declaration of built-in function ‘scanf’
    foo.c:15: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘float *’
    foo.c:17: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘float *’
    foo.c:30: warning: too many arguments for format
    foo.c: In function ‘simpson’:
    foo.c:50: warning: statement with no effect
    foo.c:56: warning: statement with no effect
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I would declare a and b to be 'double'.
    As Salem pointed out, you probably need to include <stdio.h>

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    O_O As Syscal pointed out...

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    U added the header file and changed the a,b to double. Now it hangs. Not sure if my function returning a correct value..please help


    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    double f(double x); // function prototype
    float simpson(float a, float b, int n); // funt prototype


    int main()
    {
    int n; //
    double a,b;
    // Program Instructions

    printf("\n\nPlease enter lower integration limit:");
    scanf("%lf",&a);
    printf("Please enter upper integration limit:");
    scanf("%lf",&b);
    printf("Enter number of sub-intervals 'N' (must be even):");
    scanf("%d",&n);
    if((n%4)!=0)
    {
    printf("Number of sub-intervals must be a multiple of 4 \n");
    exit(1);
    }


    //printf("a=%.10f",a); printf("b=%.10f",b); printf("n=%.l0f",n0);
    float SN; // SN will hold the next value of S
    SN = simpson(a, b, n);
    printf("Integral=",SN);
    return 0;

    }


    double f(double x)
    {
    return sin(x);
    }



    float simpson(float a, float b, int n)
    {
    n = 2 * n;
    float x;
    float h = (b - a) / n;
    float S = f(a);

    for( int i;i<n-1;i+2)
    {
    x = a + (h* i);
    S = S + (4 * f(x));
    }

    for( int i;i<n-2;i+2)

    {

    x = a + (h* i);
    S = S + (2 * f(x));
    }

    S = S + f(b);
    //F = (h * S )/ 3;
    return ((h * S )/ 3);
    }

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    or( int i;i<n-1;i+2)
    .
    .
    .
    for( int i;i<n-2;i+2)
    These loops are not changing the value of i.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    maybe you're looking for:

    for(blah; blah; i+=2)

    instead of i+2.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    foo.c:50: warning: statement with no effect
    foo.c:56: warning: statement with no effect
    THESE !!!!
    for( int i;i<n-2;i+2)
    have no side effect (the value of i is NOT changed)

    I didn't post a bunch of warnings just to feel good, I posted them to make you THINK!

    Apparently, you think it's OK just to run code that compiles without syntax errors.

    One more thing.
    It's [code][/code] around code, not [quote][/quote]
    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.

Popular pages Recent additions subscribe to a feed