📖 Scenario: Imagine you want to store the scores of 5 students in a class. Instead of creating 5 separate variables, you can use an array to keep all scores together in one place.
🎯 Goal: You will create an array to store 5 student scores, set a passing score threshold, find which students passed, and print their scores.
📋 What You'll Learn
Create an integer array called scores with exactly 5 elements: 58, 75, 62, 90, 45
Create an integer variable called passing_score and set it to 60
Use a for loop with variable i to check each score in scores against passing_score
Print the scores of students who passed using printf
💡 Why This Matters
🌍 Real World
Arrays are used in real life to store lists of things like scores, temperatures, or names all together.
💼 Career
Knowing arrays is important for programming jobs because they help manage and process collections of data efficiently.
Progress0 / 4 steps
1
Create the scores array
Create an integer array called scores with exactly 5 elements: 58, 75, 62, 90, and 45.
C
Hint
Use int scores[5] = {58, 75, 62, 90, 45}; to create the array.
2
Set the passing score
Create an integer variable called passing_score and set it to 60.
C
Hint
Use int passing_score = 60; to set the threshold.
3
Check which students passed
Use a for loop with variable i from 0 to 4 to check each score in scores. Inside the loop, use an if statement to check if scores[i] is greater than or equal to passing_score.
C
Hint
Use for (int i = 0; i < 5; i++) and inside it if (scores[i] >= passing_score).
4
Print the passing scores
Inside the if block, use printf to print the message: "Student i passed with score score\n", where i is the student index and score is scores[i].
C
Hint
Use printf("Student %d passed with score %d\n", i, scores[i]); inside the if block.
Practice
(1/5)
1. Why do we need arrays in C programming?
easy
A. To create new data types
B. To store multiple values of the same type together
C. To perform mathematical calculations automatically
D. To store only one value at a time
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type in one place, making data management easier.
Step 2: Compare options with array purpose
Only To store multiple values of the same type together correctly describes storing multiple values together; others describe unrelated features.
Final Answer:
To store multiple values of the same type together -> Option B
Quick Check:
Arrays group same-type values = D [OK]
Hint: Arrays hold many same-type values together [OK]