Welcome back.
In this video we're going to build an average calculator.
So the idea is that we ask the user to enter a bunch of scores.
For example it's a teacher and they want to calculate the average of the score of their students.
And they just enter away all the numbers.
And then at the end, once they're done, then they get the average.
Okay.
So how would we go about it?
The thing is, first of all, we rename this number here.
Let's say current score okay.
So this will be the current score.
And we're going to say enter your students score enter minus one to finish okay.
So the idea is that once a teacher enters minus one we know that we should calculate the average.
So we need to have a sum here as well by the way.
So sum and what we're going to do is we're going to say sum is equal to sum plus current score.
So whatever the teacher has entered.
Now for this to work we need to initialize sum.
So let's set sum to zero.
Otherwise it would cause problems here.
So then what we can do is we can now go ahead and do the calculation at the end once we have all the
points.
So once we have some and we also need to know how many iterations we had.
So int let's call this one counter.
Or you could say like iterations or whatever I'm going to call this one counter.
And I'm going to say counter plus plus.
So each time we had an iteration we're going to add one to the counter.
And now we can calculate the average.
Now we will get a full number.
So we're using an integer here average.
And that one will be sum divided by counter okay.
So int divided by an int will result in an int.
And even if it's 17.5 it's going to be 70 okay.
So that's what we're doing here.
So now we just need to take care of this while here.
So what we need to say is once current score is exactly equal minus one.
If that's the case then we want to get out.
But actually we need to say once it's exactly not minus one we are going to run the loop.
So this is the condition for us to keep going running the loop.
So whatever we enter that is not minus one that will allow us to keep going.
All right.
So I'd say let's run this and calculate some averages.
Enter your student score.
Enter minus one to finish.
So let's say one at 55 one at 88.
Then another 9100 in there 83.
And then I'm going to say minus one.
And actually let me see minus one.
Will it be considered in there as well.
It would be right.
So we need to check an if statement in here.
If current score is minus one that we ignore it okay.
So let's see.
And I just had finally we need to display the average here.
So the average is and then colon plus average.
Now let me add this if statement here.
So if current score is not equal minus one then run this line of code here okay.
So now we can add the curly brackets here if we want to like so then we should be good.
Let me put that into the next line.
So it's a little more readable.
So if current score is not equal minus one add to the sum.
And we also need to make sure that we put our counter in here because otherwise we're increasing the
counter even if we are actually entering minus one okay.
So now let's run this again and see what's going to come from it.
9977, 83, 55 and then let's say minus one and it says we have an average of 80.
So let me double check that.
And I quickly checked it.
It's 80.4 which is correct.
So there we are.
Now we have the average of our students score so to speak.
So that's pretty much our average calculator which is pretty cool.
So now you have this little software that you can actually use in real life.
All right.
So that's it for this video.
See you in the next one.