📖 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.
Practice
(1/5)
1. What does the following code snippet do? for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
easy
A. Prints only the last element of the array
B. Prints all elements of the array from end to start
C. Prints only the first element of the array
D. Prints all elements of the array from start to end
Solution
Step 1: Understand the loop range
The loop starts at 0 and runs while i is less than n, incrementing i by 1 each time.
Step 2: Analyze the print statement inside the loop
Each iteration prints the element at index i, so it prints elements from arr[0] to arr[n-1].
Final Answer:
Prints all elements of the array from start to end -> Option D
Quick Check:
Loop from 0 to n-1 prints all elements [OK]
Hint: Loop from 0 to n-1 prints array forward [OK]
Common Mistakes:
Confusing loop direction and thinking it prints backward
Assuming it prints only one element
Misreading the loop condition
2. Which of the following is the correct syntax to traverse an array arr of size n backwards in C?
easy
A. for (int i = 0; i < n; i++) { printf("%d", arr[i]); }
B. for (int i = n; i > 0; i++) { printf("%d", arr[i]); }
C. for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); }
D. for (int i = 1; i <= n; i++) { printf("%d", arr[i]); }
Solution
Step 1: Identify correct loop start and end for backward traversal
Backward traversal starts from index n-1 down to 0.
Step 2: Check loop syntax correctness
for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); } uses i = n-1; i >= 0; i-- which correctly traverses backward.
Final Answer:
for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); } -> Option C
Quick Check:
Backward loop uses i = n-1 to 0 with decrement [OK]
Hint: Backward loop starts at n-1 and decrements to 0 [OK]
Common Mistakes:
Starting loop at n instead of n-1
Using i++ instead of i-- for backward traversal
Accessing arr[i] with out-of-range indices
3. What is the output of the following code?
int arr[] = {5, 10, 15, 20};
int sum = 0;
for (int i = 1; i < 4; i++) {
sum += arr[i];
}
printf("%d", sum);
medium
A. 50
B. 45
C. 30
D. Syntax Error
Solution
Step 1: Identify loop indices and elements summed
The loop runs i from 1 to 3, summing arr[1], arr[2], arr[3] which are 10, 15, and 20.
Step 2: Calculate sum of selected elements
sum = 10 + 15 + 20 = 45
Final Answer:
45 -> Option B
Quick Check:
Sum of arr[1..3] = 45 [OK]
Hint: Sum elements from index 1 to 3 inclusive [OK]
Common Mistakes:
Starting sum from arr[0] mistakenly
Adding wrong indices or missing last element
Confusing loop range boundaries
4. Find the error in the following code that attempts to print all elements of an array arr of size n:
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
medium
A. Loop condition should be i < n, not i <= n
B. Array indexing should start from 1, not 0
C. printf statement syntax is incorrect
D. No error, code is correct
Solution
Step 1: Analyze loop condition and array indexing
Loop runs from i = 0 to i = n inclusive, accessing arr[n] which is out of bounds.
Step 2: Correct loop condition to avoid out-of-bounds access
Change condition to i < n so indices go from 0 to n-1, valid for array of size n.
Final Answer:
Loop condition should be i < n, not i <= n -> Option A
Quick Check:
Array indices valid from 0 to n-1 [OK]
Hint: Use i < n to avoid out-of-bounds errors [OK]
Common Mistakes:
Using <= instead of < in loop condition
Starting index at 1 instead of 0
Ignoring array size limits
5. Given an integer array arr of size n, which code snippet correctly prints every second element starting from the first element?
hard
A. for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); }
B. for (int i = 1; i < n; i += 2) { printf("%d ", arr[i]); }
C. for (int i = 0; i <= n; i += 2) { printf("%d ", arr[i]); }
D. for (int i = n-1; i >= 0; i -= 2) { printf("%d ", arr[i]); }
Solution
Step 1: Understand requirement to print every second element starting at first
Start at index 0 and increment index by 2 each time to skip every other element.
Step 2: Check loop correctness and bounds
for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); } uses i = 0; i < n; i += 2 which correctly prints arr[0], arr[2], arr[4], etc.
Final Answer:
for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); } -> Option A
Quick Check:
Increment by 2 starting at 0 prints every second element [OK]
Hint: Increment index by 2 starting at 0 for every second element [OK]
Common Mistakes:
Starting at index 1 instead of 0
Using <= n causing out-of-bounds
Looping backward when forward is needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.