0
0
Cprogramming~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 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use printf("Student %d passed with score %d\n", i, scores[i]); inside the if block.