📖 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++
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++
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++
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++
Hint
Use std::cout << "Total price: " << total << std::endl; to print.
Practice
(1/5)
1. What is the starting index of a one-dimensional array in C++?
easy
A. -1
B. 1
C. 0
D. Depends on the array size
Solution
Step 1: Understand array indexing in C++
Arrays in C++ always start indexing from 0, meaning the first element is at index 0.
Step 2: Confirm the index range
Indexes go from 0 up to size-1, so the first index is always 0 regardless of array size.
Final Answer:
0 -> Option C
Quick Check:
Array indexing starts at 0 [OK]
Hint: Remember: C++ arrays always start at zero index [OK]
Common Mistakes:
Thinking arrays start at 1
Assuming negative indexes are valid
Confusing with other languages
2. Which of the following is the correct way to declare an integer array of size 5 in C++?
easy
A. array<int> arr(5);
B. int arr(5);
C. int arr = new int[5];
D. int arr[5];
Solution
Step 1: Recall C++ array declaration syntax
In C++, a fixed-size array is declared using square brackets with size: int arr[5];
Step 2: Check other options for correctness
int arr(5); uses parentheses which is invalid for arrays. array<int> arr(5); uses incorrect syntax (not standard array). int arr = new int[5]; uses dynamic allocation syntax but is incorrect without pointer.
Final Answer:
int arr[5]; -> Option D
Quick Check:
Fixed-size arrays use square brackets [OK]
Hint: Use square brackets [] to declare fixed-size arrays [OK]
Common Mistakes:
Using parentheses instead of brackets
Confusing with vector syntax
Incorrect dynamic allocation syntax
3. What will be the output of the following code?
int arr[4] = {10, 20, 30, 40};
std::cout << arr[2];
medium
A. 30
B. 20
C. 40
D. Compilation error
Solution
Step 1: Identify the array elements and indexes
The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.
Step 2: Access the element at index 2
arr[2] is 30, so std::cout will print 30.
Final Answer:
30 -> Option A
Quick Check:
arr[2] = 30 [OK]
Hint: Remember array index starts at 0, so arr[2] is third element [OK]
Common Mistakes:
Confusing index 2 with 1 or 3
Expecting output 20 or 40
Thinking code causes error
4. Identify the error in the following code snippet:
int arr[3] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
std::cout << arr[i] << " ";
}
medium
A. Missing semicolon after array declaration
B. Loop runs one time too many, causing out-of-bounds access
C. Syntax error in for loop declaration
D. Array size is incorrect, should be 4
Solution
Step 1: Analyze the loop range and array size
The array has size 3 with valid indexes 0,1,2. The loop runs from i=0 to i=3 inclusive, which means 4 iterations.
Step 2: Identify out-of-bounds access
When i=3, arr[3] is accessed, which is outside the array bounds and causes undefined behavior.
Final Answer:
Loop runs one time too many, causing out-of-bounds access -> Option B
Quick Check:
Loop index must be less than array size [OK]
Hint: Loop index must be less than array size, not less or equal [OK]
Common Mistakes:
Using <= instead of <
Assuming array size is 4
Ignoring out-of-bounds errors
5. Given an integer array arr of size n, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0;
for(int i = 0; i < n; i++) sum += arr[i];
B. int sum = 0;
for(int i = 1; i < n; i++) sum += arr[i];
C. int sum = 0;
for(int i = 0; i <= n; i++) sum += arr[i];
D. int sum = arr[0];
for(int i = 1; i < n; i++) sum = arr[i];
Solution
Step 1: Understand correct loop bounds for array traversal
Array indexes go from 0 to n-1, so loop must run i from 0 to less than n.
Step 2: Check each option for correctness
int sum = 0;
for(int i = 1; i < n; i++) sum += arr[i]; starts from 1, missing arr[0]. int sum = 0;
for(int i = 0; i <= n; i++) sum += arr[i]; uses <= n, causing out-of-bounds access. int sum = 0;
for(int i = 0; i < n; i++) sum += arr[i]; correctly sums all elements from 0 to n-1. int sum = arr[0];
for(int i = 1; i < n; i++) sum = arr[i]; overwrites sum instead of adding.
Final Answer:
int sum = 0;
for(int i = 0; i < n; i++) sum += arr[i]; -> Option A
Quick Check:
Loop from 0 to n-1 sums all elements [OK]
Hint: Loop from 0 to less than n to cover all elements [OK]
Common Mistakes:
Starting loop at 1
Using <= n causing errors
Overwriting sum instead of adding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.