đ Introduction to C Language Arrays: Storing Multiple Data at Once!
Author: IoT Smart Academy
đ§ 1. Why Do We Need Arrays?
In previous programs, we could only use single variables:
int a, b, c, d, e;
What if we need to store the scores of 100 students? Should we write 100 variables?đ°
â Solution: Array allows us to store a group of data of the same type under one name.
⨠2. Definition and Initialization of Arrays
data_type array_name[element_count];
For example:
int score[5]; // Define 5 integers
float temp[10]; // Define 10 floats
char name[20]; // Define a string array
đ Memory Representation:
| Index | Element | Description |
|---|---|---|
| 0 | score[0] | First element |
| 1 | score[1] | Second element |
| … | … | … |
| 4 | score[4] | Fifth element |
â Initialization Methods
int a[5] = {1, 2, 3, 4, 5};
float b[] = {3.5, 4.2, 1.8};
â If the length is omitted, the system automatically calculates the number of elements.
đ 3. Array Input and Output (Using for Loop)
Arrays are often used in conjunction with loops:
#include <stdio.h>
int main() {
int score[5];
int i;
printf("Please enter the scores of 5 students:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &score[i]);
}
printf("The entered scores are:\n");
for (i = 0; i < 5; i++) {
printf("%d ", score[i]);
}
printf("\n");
return 0;
}
â Output Result:
Please enter the scores of 5 students:
80 90 75 85 95
The entered scores are:
80 90 75 85 95
đ Key Points:
- Index starts from 0
- Use
<span>&score[i]</span>for input, and<span>score[i]</span>for output
đ§Ž 4. Example 1: Calculate Average Score and Highest Score
#include <stdio.h>
int main() {
int score[5];
int i, max = 0;
float sum = 0;
printf("Please enter 5 scores:");
for (i = 0; i < 5; i++) {
scanf("%d", &score[i]);
sum += score[i];
if (score[i] > max) max = score[i];
}
printf("Average score: %.2f\n", sum / 5);
printf("Highest score: %d\n", max);
return 0;
}
đ Knowledge Points:
<span>sum</span>accumulates the total<span>max</span>updates the maximum value- Note type conversion:
<span>sum/5</span>â decimal output
đ 5. Example 2: Count Pass and Fail Students
#include <stdio.h>
int main() {
int score[10];
int i, pass = 0, fail = 0;
printf("Please enter 10 scores:");
for (i = 0; i < 10; i++) {
scanf("%d", &score[i]);
if (score[i] >= 60)
pass++;
else
fail++;
}
printf("Number of pass: %d\n", pass);
printf("Number of fail: %d\n", fail);
return 0;
}
â Thought Process:
- Use two counters to count the numbers
- Utilize
<span>if</span>for conditional branching - Commonly used in IoT sensor data statistics (e.g., temperature limit detection)
đ˘ 6. Example 3: Reverse Output of Array Elements
đ Scenario: Display historical records of sensor sampling (view in reverse)
#include <stdio.h>
int main() {
int data[5];
int i;
printf("Please enter 5 numbers:");
for (i = 0; i < 5; i++) {
scanf("%d", &data[i]);
}
printf("Reverse output result:\n");
for (i = 4; i >= 0; i--) {
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
â Output Example:
Input: 10 20 30 40 50
Output: 50 40 30 20 10
đ 7. Example 4: Find Maximum and Minimum Values in an Array
#include <stdio.h>
int main() {
int a[8];
int i, max, min;
printf("Please enter 8 integers:");
for (i = 0; i < 8; i++) {
scanf("%d", & a[i]);
}
max = min = a[0];
for (i = 1; i < 8; i++) {
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}
printf("Maximum value: %d\n", max);
printf("Minimum value: %d\n", min);
return 0;
}
â Skill Points:
- Initial value set to the first element
- Start comparing from the second element
- Commonly used in IoT scenarios like “finding maximum temperature” or “finding brightest light intensity”
đ§Š 8. Example 5: Calculate Average of Sensor Data (IoT Application)
Simulate temperature sampling 5 times to find the average temperature:
#include <stdio.h>
int main() {
float temp[5];
float sum = 0;
int i;
printf("Please enter 5 temperature sampling values (unit: â):\n");
for (i = 0; i < 5; i++) {
scanf("%f", &temp[i]);
sum += temp[i];
}
printf("Average temperature: %.2fâ\n", sum / 5);
return 0;
}
đ Extended Applications:
- Can further calculate the temperature fluctuation range
- Detect if it exceeds the safety threshold
âď¸ 9. Common Errors Summary
| Error Type | Error Example | Description |
|---|---|---|
| Out of Bounds Access | <span>a[5]</span> |
Maximum index is 4 |
| Uninitialized | <span>int a[5]; printf("%d", a[2]);</span> |
Value is undefined |
Forgot to Add <span>&</span> |
<span>scanf("%d", a[i]);</span> |
Should be <span>&a[i]</span> |
| Index Type Error | <span>for(float i=0;i<5;i++)</span> |
Index must be an integer |
đ Mnemonic:
Index starts from zero, last one is less by one. Input with address, out of bounds is most fatal.
â 10. Classroom Exercises (Solutions to be published tomorrow)
1ď¸âŁ Input 10 numbers, find the average and count how many are greater than the average. 2ď¸âŁ Input 5 student scores, output the results in reverse order. 3ď¸âŁ Input a set of temperature data, output the maximum, minimum, and average temperature. 4ď¸âŁ Write a program to count the number of odd and even numbers in the array.
đ Next Article Preview
đ Advanced C Language Arrays – Array Sorting and Searching
- Bubble Sort, Selection Sort
- Array Searching (Sequential, Binary)
- Practical Project: IoT Sampling Data Sorting Analysis
đ IoT Smart Academy
A programming learning column designed for vocational IoT students. Ten minutes a day, from zero to project practice đ