For Loops and Arrays
Explore how to use for loops with arrays in C++ to build projects that track weekly data such as steps and sleep hours. Understand looping structures, automate repetitive tasks, calculate totals and averages, and practice integrating AI-generated code for improved development.
The project
You’ve already created a step tracker that records daily steps, but right now it only works when you enter the numbers manually. Printing a single line like: Steps today: 3,000, can be helpful, but it doesn’t show your progress over an entire week.
To make the tracker more useful, you need a system that stores seven days of step counts in a list and then loops through them to calculate both the total and the average.
Your project: build a weekly step tracker that keeps 7 days of steps in an array, uses a loop to add them up, and calculates the average steps per day.
The programming concept
A while loop repeats while a condition is true. But sometimes you already know exactly how many times you want something to be repeated. For example: Say hello for 5 times. Do you think a for loop or a while loop is more natural for that?
C++ gives us both. Let’s start with ...