0
0
C++programming~15 mins

One-dimensional arrays in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with One-Dimensional Arrays in C++
📖 Scenario: You are helping a small store keep track of the prices of five products. You will use a one-dimensional array to store these prices and then calculate the total cost.
🎯 Goal: Create a program that stores product prices in an array, calculates the total price, and prints the total.
📋 What You'll Learn
Create a one-dimensional array called prices with exactly five double values: 10.5, 23.0, 7.25, 15.0, and 9.99
Create a variable called total of type double and initialize it to 0
Use a for loop with the variable i to iterate over the prices array
Add each price to the total inside the loop
Print the total with the message Total price:
💡 Why This Matters
🌍 Real World
Stores and shops often use arrays to keep track of product prices or quantities.
💼 Career
Understanding arrays and loops is essential for programming tasks like data processing, inventory management, and financial calculations.
Progress0 / 4 steps
1
Create the prices array
Create a one-dimensional array called prices of type double with these exact values: 10.5, 23.0, 7.25, 15.0, and 9.99.
C++
Need a hint?

Use the syntax double prices[5] = {value1, value2, ..., value5}; to create the array.

2
Create the total variable
Create a variable called total of type double and set it to 0 inside the main function, after the prices array.
C++
Need a hint?

Write double total = 0; inside the main function.

3
Calculate the total price using a for loop
Use a for loop with the variable i to iterate from 0 to 4 (inclusive). Inside the loop, add prices[i] to the total variable.
C++
Need a hint?

Use for (int i = 0; i < 5; i++) and inside the loop write total += prices[i];.

4
Print the total price
Write a std::cout statement to print the text Total price: followed by the value of total. Then add a newline.
C++
Need a hint?

Use std::cout << "Total price: " << total << std::endl; to print.