In this lecture, we'll process the data in an array to calculate the average of the elements in the array. As usual, we have an array of scores, although I keep changing the number of scores we're going to use, so this time we'll have four scores that we're processing. We'll first read in those scores, and we're using that size function that we've talked about, and I've declared i to be of type size t because I hate warnings. Here's our standard input validation stuff, and now we're going to walk the array and calculate the average. I initialize a sum to zero. I do another for-loop here, again using size and using size t. Each time through the loop, I add scores i to the sum. This plus equal is identical to this, but it's a convenient shorthand, so many programmers just use plus equal if we're adding something to sum here. Of course, we have minus equal and divide equal and times equal, and so on. This for-loop adds up all the values in our scores array. Then I declare an average variable that is of type float and I typecast the sum to float so that I do floating point division here, not integer division, and I divide by how many elements there are in the array. Of course, if we add up all those elements and divide by how many there are, that is by definition, the average. Finally, I do one more for-loop and I walk through the elements in the array and print out the value of each element and then I print out the average as well. As you did in programming assignment 2, we're going to set our precision for our average output to be two decimal places. If our average doesn't have any decimal places or if it only has one, then it will print none or one. But if it has more than two decimal places, then this will be rounded to be two decimal places. I'll run the code and I'll show you an average with no decimal places. As you can see, it prints an average with just the whole number because that's all the average has. I can show with one decimal place, you can see it's 2.5. These are standard ways that we've seen that we process an array, whether we're populating the array with values provided by the user or we're printing out the contents of an array or we're walking the array to do some processing. I said back in the iteration lectures that I told you a small y that there was another variant of the for-loop that we can use that I hadn't shown you yet. I want to show that to you now because we very commonly use it when we're processing arrays. This variant of the for-loop is called a range based for-loop. Instead of doing this, we can do this. We can say int score and provide a range of values to walk across. Let me fix Line 36 and then we'll come back to that. Here's the idea. We have a range that we want to iterate over. Remember the looping is the iteration control structure. Instead of having to index into the array each time on line 36, we provide a variable that will get populated each time we go through the loop with a different element of scores. The first time through the loop, score will be set to scores zero. Down here, we will add scores zero to our sum. When we come back to the top of the for-loop again, score gets set to scores 1, and we go through and do some stuff until we've walked the entire scores array. Notice we don't have to calculate the size of that scores array or anything. The system knows that scores has four elements, and so this for-loop will execute four times. As that happens, the first time through score will be score zero, then scores 1, then scores 2, then scores 3, and then the loop will stop. I'm going to run the code to show you that this works the same way. As you can see, it works exactly the same way. The range based for-loop is a very powerful variant of the for-loop that we regularly use when we need to iterate over a range of values, an array of values. You'll notice though, I could try to do that here as well, but we'll actually have a problem because I'm using i inside the body of the for-loop. In a range based for-loop, we don't get i, we don't know which iteration we're on in the body of the for-loop. If we don't care, like we didn't when we were adding up the values of all the elements, then we should use a range based for-loop. But if we do care, like we do when we want to label our output to tell the user which score we're displaying, then we need to use a for-loop that uses the loop control variable as shown here. Similarly, when we're reading in this scores, we're actually telling the user which score they're entering, so a range based for-loop isn't appropriate in this scenario. Now if you want to provide less helpful output to the user, you could use a range based for-loop, but it's really best to give the most helpful output to the user and use range based for-loops when they're appropriate, and they are appropriate many times. To recap, in this lecture, you learned how to process the data in an array, and you also learned about the range based for-loop.