🧮 Detailed Explanation of C Language Array Exercises (Including Complete Code)
Author: IoT Smart Academy
🧠 Section Overview
In the previous section, we learned about the definition of arrays, input and output, summation, and finding the maximum value. Today, we will master the application scenarios of arrays through 4 practical exercises.
🌟 Exercise 1: Calculate Average and Count Numbers Greater than Average
📋 Problem Requirements
Input 10 numbers, calculate the average of these numbers, and count how many numbers are greater than the average.
💡 Thought Analysis
1️⃣ Use an array to store the 10 input numbers. 2️⃣ Use a loop to calculate the total sum. 3️⃣ Calculate the average. 4️⃣ Loop again to check which numbers are greater than the average and count them.
✅ Complete Code
#include <stdio.h>
int main() {
float num[10], sum = 0, avg;
int count = 0;
printf("Please enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / 10;
for (int i = 0; i < 10; i++) {
if (num[i] > avg)
count++;
}
printf("Average: %.2f\n", avg);
printf("Count of numbers greater than average: %d\n", count);
return 0;
}
🧩 Example Output
Enter 10 numbers:
80 90 60 70 100 85 95 50 75 65
Average: 77.00
Count of numbers greater than average: 5
📍 Knowledge Point Review:
- Use
<span>float</span>to retain decimal precision. - Nested loops are very common in data statistics programs.
🌟 Exercise 2: Output Student Scores in Reverse Order
📋 Problem Requirements
Input 5 student scores and display them in reverse order of input.
💡 Thought Analysis
1️⃣ Input array elements (scores). 2️⃣ Use a loop to output the array content in reverse.
✅ Complete Code
#include <stdio.h>
int main() {
int score[5];
printf("Please enter 5 student scores:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &score[i]);
}
printf("Scores in reverse order:\n");
for (int i = 4; i >= 0; i--) {
printf("%d ", score[i]);
}
printf("\n");
return 0;
}
🧩 Example Output
Enter 5 student scores:
70 85 90 60 75
Scores in reverse order:
75 60 90 85 70
📍 Tip:
- Array indexing starts from 0, so the last element is
<span>[4]</span>. - Looping from high to low achieves “reverse output”.
🌟 Exercise 3: Temperature Data Statistics (Max, Min, Average)
📋 Problem Requirements
Input several temperature data (e.g., 5 sampling values) and output:
- Maximum temperature
- Minimum temperature
- Average temperature
💡 Thought Analysis
1️⃣ Input temperature data into an array. 2️⃣ Use variables <span>max</span>, <span>min</span>, and <span>sum</span> to track calculations. 3️⃣ Update max, min, and sum in each iteration of the loop.
✅ Complete Code
#include <stdio.h>
int main() {
float temp[5];
float max, min, sum = 0;
int i;
printf("Please enter 5 temperature sampling values (unit: °C):\n");
for (i = 0; i < 5; i++) {
scanf("%f", &temp[i]);
sum += temp[i];
}
max = min = temp[0];
for (i = 1; i < 5; i++) {
if (temp[i] > max) max = temp[i];
if (temp[i] < min) min = temp[i];
}
printf("Average temperature: %.2f°C\n", sum / 5);
printf("Maximum temperature: %.2f°C\n", max);
printf("Minimum temperature: %.2f°C\n", min);
return 0;
}
🧩 Example Output
Enter 5 temperature sampling values:
24.5 26.0 23.8 25.6 27.2
Average temperature: 25.42°C
Maximum temperature: 27.20°C
Minimum temperature: 23.80°C
📍 Knowledge Extension:
- This can be used in IoT projects for real-time monitoring of temperature fluctuations.
- Combined with “threshold judgment”, it can further implement an alarm system.
🌟 Exercise 4: Count Odd and Even Numbers
📋 Problem Requirements
Input a set of integers and count the number of odd and even numbers separately.
💡 Thought Analysis
1️⃣ Input 10 integers into an array. 2️⃣ Use <span>%2</span> to determine odd or even. 3️⃣ Use counters to record the counts.
✅ Complete Code
#include <stdio.h>
int main() {
int num[10];
int odd = 0, even = 0;
printf("Please enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &num[i]);
if (num[i] % 2 == 0)
even++;
else
odd++;
}
printf("Count of even numbers: %d\n", even);
printf("Count of odd numbers: %d\n", odd);
return 0;
}
🧩 Example Output
Enter 10 integers:
1 2 3 4 5 6 7 8 9 10
Count of even numbers: 5
Count of odd numbers: 5
📍 Extended Application:
- In IoT, this can be used for classification logic such as “even numbers for normal device sampling, odd numbers for abnormal events”.
🧾 V. Summary and Reflection
| Skill | Mastery Level |
|---|---|
| Array Input and Output | ✅ |
| Statistics and Counting Logic | ✅ |
| Maximum and Minimum Value Calculation | ✅ |
| Conditional Branching and Looping | ✅ |
| Practical Data Analysis Thinking | ✅ |
🎯 You are now able to write complete “data processing programs”! Next, we will learn advanced applications of arrays—Sorting and Searching Algorithms.
🔜 Next Article Preview
📘 Advanced C Language Arrays: Sorting and Searching
- Bubble Sort and Selection Sort
- Sequential Search and Binary Search
- Example: Automatic Sorting and Analysis of IoT Sampling Data
📚 IoT Smart Academy
A programming learning platform designed for higher vocational IoT majors, ensuring every lesson is “understandable, learnable, and applicable” 🚀