1

I am very much a beginner in C++ and was just delving into for loops when I ran into a problem that I solved by winging it and not by understanding. My script adds numbers from 1 to 10 and calculates the average. The thing is that I had to introduce a new variable "number", other than "sum" and "count", to not have the average be wrong.

#include <iostream>
int main ()
{
    float count, sum, avg, number;
    sum=avg=number=0.0;
    for (count=1.0;count<=10.0;count+=1.0)
    {
        sum=sum+count;
        avg=sum/count;
        number=count;
    }
    printf("\n\nThe number of iterations is %1.f",number);
    printf("\nThe sum of the numbers between 1 and 10 is = %.1f",sum);
    avg=sum/number;
    printf("\nThe average is %.1f",avg);
}

Produces the right result, but

#include <iostream>
int main ()
{
    float count, sum, avg;
    sum=avg=0.0;
    for (count=1.0;count<=10.0;count+=1.0)
    {
        sum=sum+count;
        avg=sum/count;
    }
    printf("\n\nThe number of iterations is %1.f",count);
    printf("\nThe sum of the numbers between 1 and 10 is = %.1f",sum);
    avg=sum/count;
    printf("\nThe average is %.1f",avg);
}   

Gives one more iteration than I expected and causes the average to be wrong. I assume that maybe calling "count" to print adds up but have no certainty and would really like to know why. Sorry if the question but I couldn't figure it out.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    The latter includes the iteration that broke the loop when the conditional eval tested false after the increment step (and therefore broke the loop rather than entering the loop body). The former retains the last `count` that allowed entrance into the loop body because the condition was still *true*, storing that `count` as `number`. – WhozCraig Mar 28 '22 at 23:15
  • You may want to move `avg=sum/count;` to after the `for` loop. My understanding is that the *average* is the sum divided by the quantity. – Thomas Matthews Mar 28 '22 at 23:18
  • The floating point question is relevant, but I'm not sure it counts as a duplicate. This problem isn't being caused by the floating point imprecisions, right? Even if you [change](https://godbolt.org/z/T1njo8T8f) count to be an `int` the problem is the behavior identified in Vlad's answer, not anything where "Oh you thought the final loop was 10 exactly but actually it was 9.99999998" – Nathan Pierson Mar 28 '22 at 23:19
  • 1
    Note that the correct header file for the code in the question is `#include `. With that change, the code is entirely C. – Pete Becker Mar 28 '22 at 23:46
  • 1
    @NathanPierson Yes, I made a mistake and reverted closure already :) I had the thought that counting within integer range shouldn't result in any rounding error, but I missed the `<=`. – Yksisarvinen Mar 28 '22 at 23:51
  • There's very little point in computing your average this way. If you do the math, you'll see you don't need a loop at all. The average will just be the midpoint. If you really want the sum of all integer values between these points, that can also be done without a loop. Using a float value as an integer-like loop variable is an anti-pattern. – paddy Mar 29 '22 at 00:01

1 Answers1

3

In this loop

for (count=1.0;count<=10.0;count+=1.0)
{
    sum=sum+count;
    avg=sum/count;
    number=count;
}

number can not be greater than 10.0.

But in this loop

for (count=1.0;count<=10.0;count+=1.0)
{
    sum=sum+count;
    avg=sum/count;
}

count is greater than 10.0 after exiting the loop due to the condition count<=10.0.

So these statements

avg=sum/number;

and

avg=sum/count;

produce different results because number and count are unequal after the loops.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335