Advanced Topics in C Language Two-Dimensional Arrays: Detailed Explanation of Matrix Transposition and Row-Column Operations

🧩 Advanced Topics in C Language Two-Dimensional Arrays: Detailed Explanation of Matrix Transposition and Row-Column Operations

Author: IoT Smart Academy

🧠 I. What Else Can Two-Dimensional Arrays Do?

In the previous section, we learned how to store and output two-dimensional arrays (such as student score tables). Today, we will make them dynamic: ✅ Swap rows and columns (matrix transposition) ✅ Calculate the average of each row and column ✅ Calculate the diagonal sum (matrix analysis)

These operations are not just mathematical problems; they are also very practical in IoT scenarios:

  • Rows represent different sensors, and columns represent sampling values at different times;
  • We often need to calculate the average value for each sensor or fluctuations over time;
  • Or we may need to “transpose the matrix” to facilitate algorithm processing.

📚 II. Review of Two-Dimensional Array Structure

A two-dimensional array can be understood as a “table”, for example:

int data[3][4];  // 3 rows and 4 columns
Row\Column 0 1 2 3
0 data[0][0] data[0][1] data[0][2] data[0][3]
1 data[1][0] data[1][1] data[1][2] data[1][3]
2 data[2][0] data[2][1] data[2][2] data[2][3]

Row number = the index of the sensorColumn number = the index of the time point

🌟 Example 1: Matrix Transposition (Row-Column Swap)

📋 Problem Statement

Input a 3×3 matrix and output its transposed matrix. Transposition means: rows become columns, and columns become rows.

💡 Thought Process

If the original matrix is:

1 2 3
4 5 6
7 8 9

After transposition, it becomes:

1 4 7
2 5 8
3 6 9

📍 Rule: The position after transposition <span>[i][j]</span><span>[j][i]</span>.

✅ Complete Code

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

    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]);
        }
    }

    // Matrix transposition
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            t[j][i] = a[i][j];
        }
    }

    printf("\nThe transposed matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%4d", t[i][j]);
        }
        printf("\n");
    }
    return 0;
}

🧩 Example Output

Please enter a 3×3 matrix:
1 2 3
4 5 6
7 8 9

The transposed matrix is:
   1   4   7
   2   5   8
   3   6   9

📍 Extended Understanding:

  • In IoT data analysis, transposed matrices can convert “rows → sensors” into “columns → time”, facilitating subsequent statistics or plotting.

🌟 Example 2: Calculate the Average of Each Row and Column

📋 Problem Statement

Input a 3×3 score table and calculate:

  • The average score for each student (row);
  • The average score for each subject (column).

💡 Thought Process

📌 Average for each row: fix the row number and loop through columns to sum. 📌 Average for each column: fix the column number and loop through rows to sum.

✅ Complete Code

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

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

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

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

🧩 Example Output

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

=== Average Score of Each Student ===
Student1 average score: 84.33
Student2 average score: 87.00
Student3 average score: 80.00

=== Average Score of Each Subject ===
Subject1 average score: 84.00
Subject2 average score: 86.00
Subject3 average score: 81.33

📍 IoT Application Analogy:

  • Row average → stability of each sensor
  • Column average → overall system status at a certain moment

🌟 Example 3: Sum of the Main Diagonal of the Matrix

📋 Problem Statement

Input a 3×3 matrix and calculate the sum of the main diagonal elements. (The main diagonal consists of <span>[0][0], [1][1], [2][2]</span>.)

💡 Thought Process

Whenever <span>i == j</span>, it indicates that the element is on the main diagonal.

✅ Complete Code

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

    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]);

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (i == j)
                sum += a[i][j];
        }
    }

    printf("Sum of the main diagonal elements: %d\n", sum);
    return 0;
}

🧩 Example Output

Please enter a 3×3 matrix:
1 2 3
4 5 6
7 8 9
Sum of the main diagonal elements: 15

📍 Extended Task: You can further calculate the “sum of the secondary diagonal” (<span>i + j == n - 1</span>). For example, in image matrices, it can be used to analyze symmetry.

⚙️ IV. Summary of Common Techniques for Two-Dimensional Arrays

Task Type Technique
Input/Output Double for loop: outer loop controls rows, inner loop controls columns
Matrix Transposition Element position swap <span>[i][j] ↔ [j][i]</span>
Row Average Outer loop fixes the row, inner loop sums columns
Column Average Outer loop fixes the column, inner loop sums rows
Diagonal <span>i==j</span> or <span>i+j==n-1</span>

📌 Mnemonic:

Outer loop for rows, inner loop for columns, the direction of operations is clear. Summation statistics are not complicated, symmetrical rules help to remember clearly.

✅ V. Conclusion and Expansion

Through this section, you have mastered:

✅ The concept of matrices in two-dimensional arrays ✅ Transposition (row-column swap) ✅ Row and column averages and diagonal analysis ✅ The logical structure of double loops

In IoT projects:

  • Two-dimensional arrays are often used for “nodes × time” data storage;
  • Matrix operations can be used for “signal fusion, data smoothing, anomaly detection”;
  • Diagonal analysis is common in “symmetrical data detection and image analysis”.

🔜 Next Article Preview

📘 Comprehensive Applications of C Language Arrays: Sorting + Searching + Data Analysis Examples

  • Bubble sort, selection sort
  • Sequential search and binary search
  • Practical case: sorting temperature data and detecting anomalies

📚 IoT Smart Academy

Every little bit helps, programming becomes easier. Let C language be your first language to understand the intelligent world.🚀

Leave a Comment