📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to perform some common operations on this list to understand the data better.
🎯 Goal: Build a C program that stores daily temperatures in an array, calculates the average temperature, finds the highest temperature, and prints the results.
📋 What You'll Learn
Create an integer array with exactly 7 elements representing daily temperatures.
Create an integer variable to hold the sum of temperatures.
Use a for loop with the variable i to iterate over the array elements.
Calculate the average temperature as a float.
Find the highest temperature using a variable called max_temp.
Print the average temperature and the highest temperature using printf.
💡 Why This Matters
🌍 Real World
Analyzing temperature data helps in weather forecasting and understanding climate patterns.
💼 Career
Knowing how to work with arrays and loops is essential for many programming jobs, especially in data processing and embedded systems.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with exactly 7 elements: 23, 25, 19, 22, 24, 20, 21.
C
Hint
Use the syntax int arrayName[size] = {values}; to create the array.
2
Create a sum variable
Create an integer variable called sum and set it to 0 to hold the total of temperatures.
C
Hint
Initialize sum to zero before adding temperatures.
3
Calculate the sum and find the max temperature
Use a for loop with the variable i from 0 to 6 to add each element of temperatures to sum. Also, create an integer variable max_temp initialized to the first element of temperatures. Inside the loop, update max_temp if the current temperature is higher.
C
Hint
Use for (int i = 0; i < 7; i++) to loop through the array.
4
Calculate average and print results
Create a float variable called average and set it to sum / 7.0f. Then use printf to print the average temperature with one decimal place and the highest temperature as an integer.
C
Hint
Use float average = sum / 7.0f; to get a decimal average.
Use printf("Average temperature: %.1f\n", average); to print one decimal place.
Practice
(1/5)
1. What does the following C code do? int arr[5] = {1, 2, 3, 4, 5};
easy
A. Creates an array of 5 integers with values 1 to 5
B. Creates a pointer to an integer
C. Declares a single integer variable
D. Creates a 2D array of integers
Solution
Step 1: Understand the declaration
The code declares an integer array named arr with size 5.
Step 2: Check the initialization
The array is initialized with values 1, 2, 3, 4, and 5 in order.
Final Answer:
Creates an array of 5 integers with values 1 to 5 -> Option A
Quick Check:
Array declaration with initialization = Creates an array of 5 integers with values 1 to 5 [OK]
Hint: Array size and values match initialization count [OK]
Common Mistakes:
Confusing array with pointer declaration
Thinking it's a single variable, not an array
Assuming it's a 2D array
2. Which of the following is the correct syntax to access the third element of an array named arr?
easy
A. arr(3)
B. arr[2]
C. arr{3}
D. arr[3]
Solution
Step 1: Recall array indexing in C
Arrays in C use zero-based indexing, so the first element is arr[0].
Step 2: Identify the third element index
The third element is at index 2, so arr[2] accesses it.
Final Answer:
arr[2] -> Option B
Quick Check:
Third element index = 2 [OK]
Hint: Remember C arrays start at index 0 [OK]
Common Mistakes:
Using parentheses instead of brackets
Using 1-based indexing (arr[3])
Using curly braces for access
3. What is the output of this C code?
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
printf("%d", sum);
medium
A. 100
B. 90
C. Syntax error
D. 40
Solution
Step 1: Sum all elements in the array
The loop adds arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40 to sum.
Step 2: Calculate the total sum
10 + 20 + 30 + 40 = 100.
Final Answer:
100 -> Option A
Quick Check:
Sum of array elements = 100 [OK]
Hint: Add all elements using loop index 0 to size-1 [OK]
Common Mistakes:
Off-by-one error in loop causing wrong sum
Confusing sum with last element value
Syntax errors from missing semicolons
4. Identify the error in this code snippet:
int arr[3] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
printf("%d ", arr[i]);
}
medium
A. Array size should be 4
B. printf format specifier is wrong
C. Loop condition should be i < 3, not i <= 3
D. Variable i should be declared outside the loop
Solution
Step 1: Analyze loop condition
The loop runs from i=0 to i=3 inclusive, so it accesses arr[3].
Step 2: Check array bounds
Array arr has size 3, valid indices are 0,1,2. Accessing arr[3] causes out-of-bounds error.
Final Answer:
Loop condition should be i < 3, not i <= 3 -> Option C
Quick Check:
Array index must be less than size [OK]
Hint: Loop index must be less than array size [OK]
Common Mistakes:
Using <= instead of < in loop condition
Assuming array size auto-expands
Misunderstanding printf format specifier
5. You want to find the largest number in an integer array arr of size n. Which code snippet correctly does this?
hard
A. int max = arr[n]; for(int i = 0; i < n; i++) { if(arr[i] > max) max = arr[i]; }
B. int max = 0; for(int i = 0; i <= n; i++) { if(arr[i] > max) max = arr[i]; }
C. int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] < max) max = arr[i]; }
D. int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] > max) max = arr[i]; }
Solution
Step 1: Initialize max with first element
Set max = arr[0] to start comparison from first element.
Step 2: Loop from second element to end
Loop i from 1 to n-1, compare arr[i] with max, update max if arr[i] is larger.
Step 3: Check other options for errors
int max = 0; for(int i = 0; i <= n; i++) { if(arr[i] > max) max = arr[i]; } uses max=0 which fails if all numbers are negative; loop condition i <= n causes out-of-bounds; int max = arr[n]; for(int i = 0; i < n; i++) { if(arr[i] > max) max = arr[i]; } initializes max with arr[n] which is invalid index; int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] < max) max = arr[i]; } finds minimum instead of maximum.
Final Answer:
int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] > max) max = arr[i]; } -> Option D
Quick Check:
Initialize max with first element and compare correctly [OK]
Hint: Start max with arr[0], loop i from 1 to n-1 [OK]
Common Mistakes:
Using wrong loop bounds causing out-of-bounds access
Initializing max with 0 ignoring negative values
Confusing max with min logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.