0
0
C++programming~30 mins

Array size and bounds in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Array size and bounds
📖 Scenario: You are helping a small store keep track of the number of items sold each day in a week. You will use an array to store these numbers.
🎯 Goal: Create an array to hold daily sales for 7 days, set a limit for the array size, and print the sales for each day without going out of bounds.
📋 What You'll Learn
Create an integer array named dailySales with exactly 7 elements.
Create an integer variable named size and set it to 7.
Use a for loop with an index variable i to access each element of dailySales safely.
Print each day's sales using std::cout.
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like daily sales, temperatures, or scores. Knowing the size helps avoid errors when reading or writing data.
💼 Career
Understanding array size and bounds is essential for safe programming in many jobs, including software development, data analysis, and embedded systems.
Progress0 / 4 steps
1
Create the array with sales data
Create an integer array called dailySales with these exact values: 5, 8, 6, 7, 9, 4, 3.
C++
Need a hint?

Use int dailySales[7] = {5, 8, 6, 7, 9, 4, 3}; to create the array.

2
Set the array size variable
Create an integer variable called size and set it to 7.
C++
Need a hint?

Use int size = 7; to store the array size.

3
Loop through the array safely
Use a for loop with the index variable i from 0 to less than size to access each element of dailySales. Inside the loop, write a comment // Access dailySales[i].
C++
Need a hint?

Use for (int i = 0; i < size; i++) to loop safely through the array.

4
Print the sales for each day
Inside the for loop, print the text "Day " followed by i + 1, then ": ", and then the value of dailySales[i] using std::cout. After the loop, print a newline.
C++
Need a hint?

Use std::cout << "Day " << (i + 1) << ": " << dailySales[i] << std::endl; inside the loop.