0
0
C++programming~15 mins

Array initialization in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Array initialization
📖 Scenario: You are helping a small store keep track of the prices of 5 popular fruits. You want to store these prices in an array so you can use them later for calculations.
🎯 Goal: Create an array to hold the prices of 5 fruits, set a threshold price, find which fruits are more expensive than the threshold, and print those prices.
📋 What You'll Learn
Create an array called prices with exactly these values: 120, 80, 150, 90, 200
Create an integer variable called threshold and set it to 100
Use a for loop with variable i to check each price in prices and create a new array expensive that contains only prices greater than threshold
Print each price in the expensive array on its own line
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like prices, scores, or measurements in many programs.
💼 Career
Understanding array initialization and filtering is a basic skill for programming jobs that handle data processing.
Progress0 / 4 steps
1
Create the prices array
Create an integer array called prices with these exact values: 120, 80, 150, 90, 200.
C++
Need a hint?

Use curly braces {} to list the values inside the array.

2
Set the threshold value
Create an integer variable called threshold and set it to 100.
C++
Need a hint?

Just write int threshold = 100; below the array.

3
Find prices greater than threshold
Use a for loop with variable i from 0 to 4 to check each price in prices. Create an integer array called expensive with size 5. Use an integer variable count to track how many prices are greater than threshold. Add those prices to expensive.
C++
Need a hint?

Use a for loop and an if statement inside it to check each price.

4
Print the expensive prices
Use a for loop with variable i from 0 to count - 1 to print each price in the expensive array on its own line using std::cout.
C++
Need a hint?

Use a for loop and std::cout to print each price on its own line.