C - Programs on 2D Arrays

Here are some example C programs that demonstrate operations on two-dimensional (2D) arrays, along with their expected outputs:

1. Matrix Addition:


#include <stdio.h>

int main() {
    int matrix1[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int matrix2[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
    int result[3][3];

    printf("Matrix 1:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix1[i][j]);
        }
        printf("\n");
    }

    printf("Matrix 2:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix2[i][j]);
        }
        printf("\n");
    }

    printf("Result of Matrix Addition:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}
  

Output:


Matrix 1:
1 2 3 
4 5 6 
7 8 9 

Matrix 2:
9 8 7 
6 5 4 
3 2 1 

Result of Matrix Addition:
10 10 10 
10 10 10 
10 10 10 
  

2. Matrix Multiplication:


#include <stdio.h>

int main() {
    int matrix1[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int matrix2[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
    int result[3][3];

    printf("Matrix 1:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix1[i][j]);
        }
        printf("\n");
    }

    printf("Matrix 2:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix2[i][j]);
        }
        printf("\n");
    }

    printf("Result of Matrix Multiplication:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            result[i][j] = 0;
            for (int k = 0; k < 3; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}
    

Output:


Matrix 1:
1 2 3 
4 5 6 
7 8 9 

Matrix 2:
9 8 7 
6 5 4 
3 2 1 

Result of Matrix Multiplication:
30 24 18 
84 69 54 
138 114 90 
    

3. Transposing a Matrix:


#include <stdio.h>

int main() {
    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int transpose[3][3];

    printf("Original Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    printf("Transpose of the Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            transpose[j][i] = matrix[i][j];
            printf("%d ", transpose[j][i]);
        }
        printf("\n");
    }

    return 0;
}
    

Output:


Original Matrix:
1 2 3 
4 5 6 
7 8 9 

Transpose of the Matrix:
1 4 7 
2 5 8 
3 6 9 
    

4. Searching in a Matrix:


#include <stdio.h>

int main() {
    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int target = 5;
    int found = 0;

    printf("Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
            if (matrix[i][j] == target) {
                found = 1;
            }
        }
        printf("\n");
    }

    if (found) {
        printf("\nElement %d is found in the matrix.\n", target);
    } else {
        printf("\nElement %d is not found in the matrix.\n", target);
    }

    return 0;
}
    

Output:


Matrix:
1 2 3 
4 5 6 
7 8 9 

Element 5 is found in the matrix.

5. Finding the Sum of Diagonals:


#include <stdio.h>

int main() {
    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int sumMainDiagonal = 0;
    int sumSecondaryDiagonal = 0;

    printf("Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
            if (i == j) {
                sumMainDiagonal += matrix[i][j];
            }
            if (i + j == 2) {
                sumSecondaryDiagonal += matrix[i][j];
            }
        }
        printf("\n");
    }

    printf("\nSum of the main diagonal: %d\n", sumMainDiagonal);
    printf("Sum of the secondary diagonal: %d\n", sumSecondaryDiagonal);

    return 0;
}
    

Output:


Matrix:
1 2 3 
4 5 6 
7 8 9 

Sum of the main diagonal: 15
Sum of the secondary diagonal: 15