i am getting infinity in my program !!! ??

This is a discussion on i am getting infinity in my program !!! ?? within the C++ Programming forums, part of the General Programming Boards category; this is what i am suppose to do.: Scan two doubles a and b (a<b). Integrate numerically y=0.5*cos^2(x)+0.25 from x=a ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    Question i am getting infinity in my program !!! ??

    this is what i am suppose to do.:
    Scan two doubles a and b (a<b).
    Integrate numerically y=0.5*cos^2(x)+0.25 from x=a to x=b.

    Use two do/while statements to continue the
    calculations for different n_trap and different a and b. For
    example, use ask=1 to continue calculations for different n_term
    within the inner do/while loop, and ask=0 to exit that loop.
    Use flag=1 to continue calculations for different a and b
    within the outer do/while loop, and flag=0 to exit that loop.


    and this is my program so far. btw im only doing the first part havent tried the flag part because im confused

    #include <stdio.h>
    #include <math.h>
    main ()
    {
    double a, b, y, del_x, Integral, x, sum;
    int flag, k, n_trap, ask;
    printf("Scan a and b:");
    scanf("%1f %1f %1f", &a, &b);
    do
    {
    printf("Enter the number of trapezoids:\n");
    scanf("%d", &n_trap);
    del_x = (b-a)/(double)n_trap;
    printf("n_trap =%d\t del_x =%g\n", n_trap, del_x);
    x=a;
    y=0.5*cos(x)*cos(x)+0.25;
    sum= -0.5 * y;

    for(k=0; k<=n_trap; k++)
    {
    x=a+k*del_x;
    y=0.5*cos(x)*cos(x)+0.25;
    sum += y;
    }
    sum -= 0.5*y;
    sum += del_x;
    printf("Integral = %f\n", sum);
    printf("Would you like to continue with another n_trap? y=1/n=0\n");
    scanf("%d", &ask);
    }
    while (ask == 1);
    //while (ask !=1 0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    First off, you want code tags. Read the sticky "Posting Code?? Read This First!!" (I should make it a link, but I'm too lazy.)

    You also want %lf, not %1f.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    okay sorry i forgot about that. but yea im not much of a programmer so this is all new to me

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    okay so i notcied what i did wrong.
    okay so short and sweet. my answer comes out to be Integral = 49.661105
    and im trying to match the answer we were given which is2.23272
    any ideas?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Another pro tip would be to not have three %lf in your format string if you only intend to read two numbers. You are also supposed to multiply your final answer by delta x, not add it.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    yea i changed that sutff and its working. now im gonna start the second loop/ hopefully it goes well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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