Bird
0
0
DSA Cprogramming~30 mins

Array Traversal Patterns in DSA C - Build from Scratch

Choose your learning style9 modes available
Array Traversal Patterns
📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to analyze these temperatures to find out which days were warmer than a certain threshold.
🎯 Goal: Build a program that traverses an array of temperatures, checks each temperature against a threshold, and collects the warmer temperatures into a new array.
📋 What You'll Learn
Create an array called temperatures with exactly these values: 23, 27, 21, 30, 25, 28, 22
Create an integer variable called threshold and set it to 25
Use a for loop with an index variable i to traverse the temperatures array
Inside the loop, check if the current temperature is greater than threshold
If yes, add that temperature to a new array called warmer_days
Keep track of the number of warmer days in an integer variable called count
Print the contents of warmer_days array separated by spaces
💡 Why This Matters
🌍 Real World
Analyzing temperature data to find days warmer than a certain limit is common in weather forecasting and climate studies.
💼 Career
Understanding array traversal and conditional selection is fundamental for data processing tasks in software development and data analysis roles.
Progress0 / 4 steps
1
Create the temperatures array
Create an integer array called temperatures with exactly these values: 23, 27, 21, 30, 25, 28, 22
DSA C
Hint

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

2
Set the threshold value
Create an integer variable called threshold and set it to 25
DSA C
Hint

Use int threshold = 25; to create and set the variable.

3
Traverse the array and collect warmer days
Create an integer array called warmer_days with size 7, an integer variable count set to 0, and use a for loop with index i from 0 to 6 to check if temperatures[i] is greater than threshold. If yes, add temperatures[i] to warmer_days[count] and increment count
DSA C
Hint

Use a for loop from 0 to 6 and an if statement inside it to check each temperature.

4
Print the warmer days
Use a for loop with index j from 0 to count - 1 to print each value in warmer_days separated by spaces
DSA C
Hint

Use printf("%d ", warmer_days[j]); inside the loop to print each temperature followed by a space.