Thread: Can you spot my mistake?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    Can you spot my mistake?

    As part of a homework assignment my program has to have a function which will carry out Simpson's method for integration. Anyway my function is given here:

    Code:
    double function1(double a, double b, int N)
    {
           double result;
           double functerm;
           double funca = sqrt(1+pow(a,4));
           double funcb = sqrt(1+pow(b,4));
           int count = 1;
           double x=a+((b-a)/N);
    
           printf("%g\n",funca);
           printf("%g\n",funcb);
           while (x<b)
           {
                 if(count%=2 == 0)
                 functerm+=2*(sqrt(1+(x*x*x*x)));
                 else
                 functerm+=4*(sqrt(1+(x*x*x*x)));
    
                 x+=((b-a)/N);
                 count++;
           }
           printf("%g\n",functerm);
           result=(functerm + funca + funcb)*((b-a)/(3*N));
    
           return result;
    }
    When used with the values a=-1, b=1 and N=4, the values for funca and funcb give correct answers, however the result of functerm is a massive number (in the order of 10^238 or something) when it should be about 10. Can anyone spot where my mistake is being created?

    The formula for Simpson's rule was given on a sheet by the way, as neither myself nor my maths student friends seem to remember it as its given there.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (x<b)
    Maybe because this can execute 0 times, and leave the result uninitialised.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    I don't understand.

    I thought that was saying that while x was less than b, it would continue to do the loop, and as x increases everytime it would only loop a finite number of times (i.e. until x equals or gets bigger than b). Right?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is dividing by zero.
    Code:
    if ( count%=2 == 0 )
    Use parentheses.
    Code:
    if ( (count%=2) == 0 )
    And do you really want %=, or just %?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So if x > b, how many times does the loop execute?

    Why don't you use a debugger to step through the code (or just put a lot of printf() statements in there) to see what route the code takes, and what values are being generated.

    When results != expectation, you've found a bug.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Quote Originally Posted by Dave_Sinkula
    This is dividing by zero.
    Code:
    if ( count%=2 == 0 )
    Use parentheses.
    Code:
    if ( (count%=2) == 0 )
    And do you really want %=, or just %?
    Nope. That makes no difference to the answer. I thought that what I'd written there was count (which should increase by one every loop) divided by 2 and giving the remainder (i.e. a check for even numbers).

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Quote Originally Posted by Salem
    So if x > b, how many times does the loop execute?

    Why don't you use a debugger to step through the code (or just put a lot of printf() statements in there) to see what route the code takes, and what values are being generated.

    When results != expectation, you've found a bug.
    I thought I'd defined x to be less than b in the first place (as long as a was less than b, which being the lower limit of the integral it will be).

    But if x was greater than b the loop wouldn't run would it, so it skip the code block, correct?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have no idea what (count%=2) is all about. Never seen it before.

    Are you sure you don't want to use:
    Code:
    if((count % 2) == 0)
    ?

    Adak

  9. #9
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    check what salem said. Initialise that term to zero at the start of the code and check if it is changing at all in the first place.

    >I have no idea what (count%=2) is all about. Never seen it before.
    it caught me a little off gaurd too.
    that is something like " sum+=2 " right?
    it means,
    count = count %2
    Last edited by kris.c; 11-12-2006 at 11:15 AM.
    In the middle of difficulty, lies opportunity

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    It appears that a divison by zero is happening, as N isn't being defined for some reason.

    Surely
    Code:
    printf("Please enter the number of intervals over which the integration is to be made (must be even and positive)\n");
            scanf("%d",&N);
    should define the value of N right? Where N is an integer.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kris.c
    check what salem said. Initialise that term to zero at the start of the code and check if it is changing at all in the first place.

    >I have no idea what (count%=2) is all about. Never seen it before.
    it caught me a little off gaurd too.
    that is something like " sum+=2 " right?
    it means,
    count = count %2
    Ok, so he's modifying count ON PURPOSE, and THEN making a checking of that value against 0.
    I thought he wanted a check of the remainder of count, only. Interesting.

    Adak

  12. #12
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    may be you are not passing it correctly to the function
    In the middle of difficulty, lies opportunity

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    My next stage has to been to ask for and define the value of N within the function itself rather than relying on passing it across from the main code. Yet its still not being defined correctly. My code is thus:
    Code:
    printf("Enter number of intervals\n");
           scanf("%d",&N);
    where N is defined as an integer. Surely this should ask for a value for N and then store it within N, yes?

    This program is doing my head in!

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems OK so far.
    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

Similar Threads

  1. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 01:28 AM
  2. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  3. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 10:45 PM
  4. Whom do you blame for the mistake in Pres. Bush's State of the Union speech?
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-15-2003, 07:03 AM
  5. How do I put text in a specified spot of an EDIT box?(PLEASE READ!)
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-10-2001, 07:55 AM