0
0
C++programming~15 mins

Why arrays are needed in C++ - See It in Action

Choose your learning style9 modes available
Why arrays are needed
📖 Scenario: Imagine you want to store the scores of 5 players in a game. Instead of creating 5 separate variables, you can use an array to keep all scores together.
🎯 Goal: You will create an array to hold 5 player scores, set a threshold score, find which players scored above the threshold, and print their scores.
📋 What You'll Learn
Create an integer array called scores with exactly these values: 50, 75, 60, 85, 40
Create an integer variable called threshold and set it to 60
Use a for loop with variable i to find scores greater than threshold and store them in an array called highScores
Print the highScores array elements separated by spaces
💡 Why This Matters
🌍 Real World
Arrays are used in games, apps, and websites to store lists like scores, names, or settings.
💼 Career
Understanding arrays is essential for programming jobs because they help manage data efficiently.
Progress0 / 4 steps
1
Create the scores array
Create an integer array called scores with these exact values: 50, 75, 60, 85, 40
C++
Need a hint?

Use int scores[5] = {50, 75, 60, 85, 40}; to create the array.

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

Use int threshold = 60; to create the threshold variable.

3
Find scores above the threshold
Use a for loop with variable i from 0 to 4 to check each score in scores. If scores[i] is greater than threshold, add it to an integer array called highScores. Also create an integer variable count to track how many scores are added.
C++
Need a hint?

Use a for loop and an if statement to check and store scores above the threshold.

4
Print the high scores
Use a for loop with variable i from 0 to count - 1 to print each element of highScores separated by spaces.
C++
Need a hint?

Use std::cout inside a for loop to print the scores with spaces.