james42
james42

Reputation: 307

Counter variable: how does it work?

I was writing a piece of software that allows to the user to compute the average of ten integer numbers. This is the code:

// Algorithm for computing the average of the grades of a class with the controlled iteration of a counter

#include <iostream>

using namespace std;

int main()
{
    int total,          // sum of all grades
        gradeCounter,   // n° of inputted grades
        grade,          // a single vote
        average;        // average of grades

        // initialization phase
        total = 0;              //sets the total to zero
        gradeCounter = 1;       //prepares the counter

    // elaboration phase
    while ( gradeCounter <= 10 ) { // 10 times cycle    
        cout << "Enter grade: "; // input prompt
        cin >> grade;            // input grade
        total = total + grade;   // adds the grade to the total
        gradeCounter = gradeCounter + 1; // increases the counter
    }

    // end phase
    average = total / gradeCounter;
    cout << "The class average is " << average << endl;

    return 0;
}

Now, I was thinking that writing average = total / gradeCounter; would work, since the last variable stored in gradeCounter is 10; but the result given by average = total / 10; is the real average. I don't get why there's this discrepancy. Can somebody explain this to me?

Upvotes: 4

Views: 9732

Answers (5)

Sid
Sid

Reputation: 488

Actually what is happening is that after each run of your while loop, the value of gradeCounter is 1 more than the number of times the loop has been run(because you have initialized gradeCounter to 1 before start of the loop). You can do any 1 of the following to fix this.

1)

average = total / (gradeCounter-1);

2)

gradeCounter = 0;
while ( gradeCounter < 10 ) {
    cout << "Enter grade: ";
    cin >> grade;
    total = total + grade; 
    gradeCounter = gradeCounter + 1;
}

Upvotes: 1

RyanP
RyanP

Reputation: 1918

while ( gradeCounter <= 10 ) { //10 times cycle gradeCounter = gradeCounter + 1;

Grade counter will be 11 after the loop ends (last iteration it gets set to 11, then <= 10 is not true and it exits. So you'd be doing a divide by 11 instead of 10.

Upvotes: 1

Daniel Jour
Daniel Jour

Reputation: 16156

while ( gradeCounter <= 10 )

This condition evaluates to false when, in your case, gradeCounter is incremented to 11. (11 is not less or equal to 10)

So your counter is eleven, not ten after your loop.

Upvotes: 1

Jashaszun
Jashaszun

Reputation: 9270

while ( gradeCounter <= 10 ) { //10 times cycle 
    cout << "Enter grade: "; //input prompt
    cin >> grade;            //input grade
    total = total + grade;   //adds the grade to the total
    gradeCounter = gradeCounter + 1; //increases the counter
}

In this piece of code, when gradeCounter is 10, the while loop runs once more, incrementing gradeCounter to 11. So later, you're really dividing by 11 rather than 10.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283614

You should draw a flowchart for this. If gradeCounter <= 10, you go into the loop, and only when gradeCounter >= 11 do you proceed with average = total/gradeCounter

That's why you get a result divided by 11 instead of 10.

Upvotes: 0

Related Questions