Detailed Explanation of Two-Dimensional Arrays in C: From Tables to Matrix Operations

🧩 Detailed Explanation of Two-Dimensional Arrays in C: From Tables to Matrix Operations

Author: IoT Smart Academy

🧠 I. Why Learn Two-Dimensional Arrays?

In the previous section, we learned about one-dimensional arrays, which are used to store a set of data:

int a[5] = {10, 20, 30, 40, 50};

But what if we want to store a “table” or “matrix”? For example:

Student ID Score 1 Score 2 Score 3
01 85 90 78
02 92 88 81

At this point, a one-dimensional array is not sufficient. ✅ We need a two-dimensional array (Two-dimensional Array)!

✨ II. Definition and Structure of Two-Dimensional Arrays

DataType ArrayName[RowCount][ColumnCount];

For example:

int score[3][4]; // Integer matrix with 3 rows and 4 columns

📌 It can be understood as a “table”:

Index Row\Column 0 1 2 3
0 score[0][0] score[0][1] score[0][2] score[0][3]
1 score[1][0] score[1][1] score[1][2] score[1][3]
2 score[2][0] score[2][1] score[2][2] score[2][3]

🌟 III. Initialization and Access of Two-Dimensional Arrays

int a[2][3] = { {1, 2, 3}, {4, 5, 6} };

This is equivalent to:

int a[2][3] = {1, 2, 3, 4, 5, 6};

📌 Access Method:

printf("%d", a[1][2]); // Outputs the element in the second row and third column

🧮 IV. Case 1: Student Score Input and Output

📋 Problem Description

Input the scores of 3 students for 3 subjects and output the score table.

✅ Complete Code

#include <stdio.h>
int main() {
    int score[3][3];  // 3 students, each with 3 subjects
    int i, j;

    printf("Please enter the scores of 3 students for 3 subjects:\n");
    for (i = 0; i < 3; i++) {
        printf("Student %d: ", i + 1);
        for (j = 0; j < 3; j++) {
            scanf("%d", &score[i][j]);
        }
    }

    printf("\n=== Score Table ===\n");
    for (i = 0; i < 3; i++) {
        printf("Student %d: ", i + 1);
        for (j = 0; j < 3; j++) {
            printf("%4d", score[i][j]);
        }
        printf("\n");
    }
    return 0;
}

🧩 Example Output

Please enter the scores of 3 students for 3 subjects:
Student 1: 85 90 78
Student 2: 92 88 81
Student 3: 75 80 85

=== Score Table ===
Student 1:  85  90  78
Student 2:  92  88  81
Student 3:  75  80  85

📍Knowledge Points:

  • Nested loops for input and output
  • <span>i</span> controls rows (students), <span>j</span> controls columns (subjects)

🌟 V. Case 2: Calculate Each Student’s Average Score

✅ Complete Code

#include <stdio.h>
int main() {
    float score[3][3];
    float avg;
    int i, j;

    printf("Please enter the scores of 3 students for 3 subjects:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%f", &score[i][j]);
        }
    }

    printf("\n=== Average Scores of Each Student ===\n");
    for (i = 0; i < 3; i++) {
        avg = 0;
        for (j = 0; j < 3; j++) {
            avg += score[i][j];
        }
        avg /= 3;
        printf("Student %d Average Score: %.2f\n", i + 1, avg);
    }
    return 0;
}

🧩 Example Output

Please enter the scores of 3 students for 3 subjects:
85 90 78
92 88 81
75 80 85

=== Average Scores of Each Student ===
Student 1 Average Score: 84.33
Student 2 Average Score: 87.00
Student 3 Average Score: 80.00

📍Skill Summary:

  • A two-dimensional array can be seen as a collection of multiple one-dimensional arrays.
  • Average calculations are commonly used in IoT data aggregation (e.g., average node energy consumption).

🌟 VI. Case 3: Matrix Addition (Mathematical Operations Application)

📋 Problem Description

Input two 2×2 matrices, calculate and output their sum.

✅ Complete Code

#include <stdio.h>
int main() {
    int a[2][2], b[2][2], c[2][2];
    int i, j;

    printf("Input Matrix A (2×2):\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &a[i][j]);

    printf("Input Matrix B (2×2):\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &b[i][j]);

    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            c[i][j] = a[i][j] + b[i][j];

    printf("\nMatrix A + Matrix B = \n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            printf("%4d", c[i][j]);
        printf("\n");
    }
    return 0;
}

🧩 Example Output

Input Matrix A:
1 2
3 4
Input Matrix B:
5 6
7 8
Matrix A + Matrix B =
   6   8
  10  12

📍 Application Extension: Matrix operations are fundamental in image processing, AI calculations, and IoT signal filtering.

🌟 VII. Case 4: Find the Position of a Specified Element

📋 Problem Description

Input a 3×3 matrix, then input a number x, and find the row and column position of that number.

✅ Complete Code

#include <stdio.h>
int main() {
    int a[3][3], i, j, x, found = 0;

    printf("Please enter a 3×3 matrix:\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            scanf("%d", &a[i][j]);

    printf("Please enter the number to find:");
    scanf("%d", &x);

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (a[i][j] == x) {
                printf("Found %d at row %d column %d\n", x, i + 1, j + 1);
                found = 1;
            }
        }
    }

    if (!found)
        printf("Number not found.\n");

    return 0;
}

🧩 Example Output

Please enter a 3×3 matrix:
1 2 3
4 5 6
7 8 9
Please enter the number to find: 8
Found 8 at row 3 column 2

📍 Application Extension: This is the basis of “two-dimensional data localization”, used in IoT for “node coordinate search”, “grid data matching”, etc.

⚙️ VIII. Common Errors with Two-Dimensional Arrays

Error Type Example Description
Out-of-bounds access <span>a[3][3]</span> The maximum index is <span>[2][2]</span>
Incorrect input format <span>scanf("%d", a[i][j]);</span> Should be <span>&a[i][j]</span>
Forgot nested loops Single loop only inputs one row Two-dimensional arrays must use double loops

📌 Mnemonic:

Outer loop for rows, inner loop for columns, double loop for each row.

✅ IX. Summary

Content Mastery Level
Definition and initialization of two-dimensional arrays
Nested loop input and output
Row and column operations and matrix summation
Searching and logical judgment

🎯 You now have the ability to handle “table data” using C language!

🔜 Next Article Preview

📘 Advanced Topics in Two-Dimensional Arrays in C: Matrix Transposition and Row/Column Statistics

  • Matrix transposition operations
  • Calculating average values for each row and column
  • Summing the diagonal of a matrix
  • IoT matrix sensor data analysis case study

📚 IoT Smart Academy

Focusing on vocational IoT programming education with daily exercises to help you progress from “able to write” to “able to write well”!💪

Leave a Comment