Reputation: 301
Hello could you take a quick glance at my code and point out the mistake(s). I'm trying to calculate sum of n numbers going like this: 1- 1/2 + 1/3 - 1/4 ... etc...
With the following code, I get 1.00000 each time, but it should be between 0 and 1, for example for 3 it should be 1 - 1/2 + 1/3 = 0,83333.
#include <stdio.h>
int main () {
int n, prefix;
float sum;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= prefix/n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}
Upvotes: 1
Views: 655
Reputation: 46365
Three errors that I can see:
premix
vs prefix
)sum
Try the following instead:
#include <stdio.h>
int main () {
int n, n_initial;
double sum=0.0, prefix = 1.0;
printf("enter the value for n:\n");
scanf("%d", &n);
if(n<1) {
printf("n must be > 0!\n");
return 1;
}
n_initial = n;
if (n%2==0) prefix = -1.0; else prefix= 1.0;
do {
sum+= prefix/(double)n;
prefix *= -1.0;
n--;
} while (n > 0);
printf("The sum of the series over %d terms is: %lf\n", n_initial, sum);
return 0;
}
Note - I keep n
as an integer, and cast it explicitly before the divition. It might be better just to make it a float / double - remember to change the format specification for the scanf
accordingly. I only do the modulo operation once (after that, the sign of prefix
just keeps changing). Also - it's always a good idea to add a prompt for the input of a number ("why isn't it doing anything?!"), to annotate the result (rather than print "just a number"), and to end the output with a newline (so the prompt doesn't obscure the output of the program).
Finally - you might want to check that the user doesn't enter a negative number, which would make your code give a bad result.
As an afterthought - you could test for large values of n
, and just return log(2.0)
. But that would be cheating... and this series does converge awfully slowly (it oscillates quite badly - the 3rd digit is still changing when n=1000). Consequently, rounding errors risk really compounding. This is why you need to be using the double
type; but I would suggest it's instructive to look at other ways to compute log(2.0)
- for example, using one of the other series given at http://www.math.com/tables/expansion/log.htm . You could actually implement all of them, and compare their accuracy after n terms (by printing out the error: sum - log(2.0)
.)
Upvotes: 2
Reputation: 11577
you are doing prefix/n
when both are integers. so you are getting the answer as integer (which always is 0)
use
(double)prefix/n
or
prefix/(double)n
or even
(double)prefix/(double)n
here is the full code:
#include <stdio.h>
int main () {
int n, prefix;
float sum = 0;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= (double)prefix/(double)n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}
i ran it on compileonline.com with the input of 10 and got:
Upvotes: 1
Reputation: 149
Adding to the above answers you have uninitialized sum(initialize it to zero).And I think your code doesn't work when input is 0(it gives divide by zero error).So better use while or for loop or come up with an alternative in do while.
Upvotes: 2
Reputation: 409166
Both prefix
and n
are integers, so that means you will have integer division which truncates the result (i.e. simply cuts of the decimals). If you want a floating point result, one or both of the variables also have to be a floating point variable.
Upvotes: 0