Thread: Do While loop

  1. #1
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Do While loop

    I am trying to get a program going that is the sum of the fractions (starting from one to arbitrary # n) in the form of (1/2^i) using a do while loop.
    Code:
    {
        int i=0, n, sum=0;
      printf("Enter n: \n");
      scanf("%i", &n);
        do 
        {
        sum += (1/(pow(2,i));
        printf ("The sum of the fractional exponentials from 1. . .%d is %d\n",n
               sum);   
        } 
        while (i++<n);
    
        return 0;
    }
    The compiler highlights the "sum +=(1/pow(2,i)).
    Thank you ahead of time for any suggestions.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    What does the compiler actually say?

    Have you included the math.h header file?


    [EDIT]

    His surname is actually spelt 'Wiggum', by the way. :P


    [EDITv2]

    Also, you need to add a comma to this line:

    Code:
    printf ("The sum of the fractional exponentials from 1. . .%d is %d\n",n,sum);
    Last edited by bivhitscar; 09-29-2006 at 03:18 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Damn Wiggum. . .I am particularly fond of him.
    thanks for the corrections
    The compiler reads 2 lines I put in in the code box. i guess I don't need code but it looks better
    Code:
     
                   In function `main': 
    line 11     syntax error before ';' token
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
        int i=0, n, sum=0;
      printf("Enter n: \n");
      scanf("%i", &n);
        do 
        {
        sum += (1/(pow(2,i));
        printf ("The sum of the fractional exponentials from 1. . .%d is %d\n",n,
               sum);   
        } 
        while (i++<n);
    
        return 0;
    }
    Last edited by ralphwiggam; 09-29-2006 at 03:34 AM.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    "line 2:18 [Warning] extra tokens at end of #include directive "

    Sounds to me like one of your include lines has a semi-colon, or some other character at the end of it - which it doesn't need.

    Always read the compiler errors and warnings from the top first - they usually trigger the errors that follow them.

    [EDIT]

    Bahahahahahaha, I'm an idiot, I should have noticed earlier. You need one more bracket in that line.

    Code:
    sum += (1/(pow(2,i)));
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    bivhitscar
    You are definitely NOT an idiot. Your corrections have been spot on.
    My preprocessor directives were misspelled but I changed that.
    I put one more parenthesis and it compiles and executes asking me for an "n" but it does not figure it out. . .it just vanishes

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Ahh, that would be the fact that it returns without waiting.

    Have a read of this: Stop my Windows Console from disappearing everytime I run my program?


    Actually, this would probably help more - http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bivhitscar
    His surname is actually spelt 'Wiggum', by the way. :P
    Well you can't seriously expect Ralph to be able to spell his own name! I mean, come on, it's Ralph we're talking about here!

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

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Ok quzah, you do have a point there. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    My cat smells of cat food.

  10. #10
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Har-Har so wikipedia has an article on Ralph. It is hilarious. It also preserves the humor which I think is special because so often when you seek to dissect something it dies in the process. . .thanks

    bivhitscar I appreciate that second link. I used "poor-mans method" system ("pause"); and that has resolved that problem with the disappearance but it just displays an output of "1" no matter what the input is

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    sum += (1/(pow(2,i));
    if pow(2,i) is > 1, then (integer) 1/(pow(2,i) will always return 0.

    Presumably you want to use a float/double variable instead?

  12. #12
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Of course, integer division. Try changing 'sum' to a float and altering this line:

    Code:
    sum += (float) (1/(pow(2,i));
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  13. #13
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    SKEane
    You are right. . .thanks. Man I am sloppy!
    bivhitscar
    Sorry am changing my code right now per your suggestion

    EVen with that (float) it still displays that number

    Damn I need to change %d. . .thanks you all!

    Okay changed the %d and I'm getting closer.

    When I select a number for "n" like 3 it displays 4 lines "The sum. . .is 1.0000"
    "The sum. . .1.5000"
    "The sum. . .1.75000"
    "The sum. . .1.87500"
    so I'm headed in the right direction with this thing.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
        float i=0, n, sum=0;
      printf("Enter n: \n");
      scanf("%f", &n);
        do 
        {
        sum += (float) (1/(pow(2,i)));
        printf ("The sum of the fractional exponentials from 1. . .%f is %f\n",n,
               sum);   
        } 
        while (i++<n);
        system ("pause");
        return 0;
    }
    Last edited by ralphwiggam; 09-29-2006 at 04:38 AM.

  14. #14
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Change your printf function to display %f for sum.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  15. #15
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    printf ("The sum of the fractional exponentials from 1. . .%f is %f\n",n, sum);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM