1. ## 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. 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);`

3. 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;
}```

4. "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)));`

5. 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. 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

7. 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.

8. Ok quzah, you do have a point there. :P

9. My cat smells of cat food.

10. 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. 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. Of course, integer division. Try changing 'sum' to a float and altering this line:

Code:
`sum += (float) (1/(pow(2,i));`

13. 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;
}```

14. Change your printf function to display %f for sum.

15. Code:
`printf ("The sum of the fractional exponentials from 1. . .%f is %f\n",n, sum);`