1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C - unwanted output

  1. Feb 15, 2010 #1
    http://pastebin.com/d49e23fad [Broken] --
    I input 0.0 for the left endpoint and 100.0 for the right endpoint, yet it printed "1.910014e-3074.243992e-314"... What is going on??
     
    Last edited by a moderator: May 4, 2017
  2. jcsd
  3. Feb 15, 2010 #2

    Mark44

    Staff: Mentor

    Code (Text):

    int main()
    {
        int sub_intrvl;
        double left_ep;
        double right_ep;
        double answer;
        printf("Number of Subintervals: ");
        scanf("%d", & sub_intrvl);
        printf("Left endpoint: ");
        scanf("%f", & left_ep);
        printf("Right endpoint: ");
        scanf("%f", & right_ep);
        printf("%e", right_ep);
        printf("%e", left_ep);
    }
     
    I would change the %f conversion specifiers in scanf() to %lf, since you're reading in a double value.
     
    Last edited by a moderator: May 4, 2017
  4. Feb 17, 2010 #3
    Thanks, I got that problem fixed. But now, when i input everything, the output doesn't display for some reason. I thought I had an infinite loop, but that doesn't seem to be an issue. http://pastebin.com/m32ac3107 [Broken]
     
    Last edited by a moderator: May 4, 2017
  5. Feb 17, 2010 #4

    Mark44

    Staff: Mentor

    I think you might have an infinite loop, despite what you are saying.

    BTW, why not post your code here? Just put it between a [ code] tag and the beginning and a [ /code] tag at the end -- without the leading spaces.

    Code (Text):

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>

    double trpzd_method(int, double, double);
    double f(double);
    int main()
    {
        int sub_intrvl;
        double left_ep;
        double right_ep;
        double answer;
        printf("Number of Subintervals: ");
        scanf("%d", & sub_intrvl);
        printf("Left endpoint: ");
        scanf("%lf", & left_ep);
        printf("Right endpoint: ");
        scanf("%lf", & right_ep);
        answer = trpzd_method(sub_intrvl, left_ep, right_ep);
        printf("%lf", answer);
        system("PAUSE");
       
    }
    double trpzd_method(int sub_intrvl, double left_ep, double right_ep)
    {
        double total = 0.0;
        double final_result;
        double first_value = f(left_ep);
        double last_value = f(right_ep);
        double increment = (right_ep - left_ep)/sub_intrvl;
        while((left_ep + increment) <= right_ep);
        {
            total += f(right_ep + increment);
            increment = (increment + ((right_ep - left_ep)/sub_intrvl));
        }
        final_result = increment*((0.5*(first_value + last_value)) + total);
        return final_result;

    }
    double f(double x)
    {
        double e = 2.71828182845904523536;
        double exponent = (-1)*(x)*(x);
        double result = pow(e, exponent);
        return result;
    }
     
    From your description, trpzd_method() isn't returning, and I can see exactly why. Take a very close look at your while loop.
     
  6. Feb 17, 2010 #5
    Thanks, I found the problem. The funny thing is, the professor was just going over that today, and I was thinking "lol who would make that mistake?".
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook