Addition problem in loop
Hi, I'm a beginner programming and have run into a problem on a project. Here's my loop.
while(scanf("%d", &x) != EOF)
a = 1 / x;
b += a;
c = d / b;
This loop is supposed to calculate the harmonic mean of a number series. The problem here is with the variable "b". I get an error with that because it is not assigned a value. Normally I would set a variable like "b" to 0 when I declare it but since I divided 1 by "x" variable "b" remains 0 for some reason and that's what the loop spits out. What's the problem here?
If x is an integer, then 1/x is zero. (Well, zero remainder one, but you didn't ask for the remainder, or unless x equals one or zero or negative one.) You need to be using floating-point variables for this.
What is d initialized to? It would be best if you just posted the entire "working" (ie: broken) program.
That was the problem. I thought it was something simple I was forgetting. Thanks.
Originally Posted by tabstop