C Programming • Arrays & Functions
C Programming / Arrays (2-D) — Exercises

Arrays (2-D) — Exercises

Practical 3 Arrays & Functions

Write a C program to store elements in a 2-D array of 2 rows and 3 columns and display the matrix.

Practical / Solution

2-D Array — Exercise 1

Problem

Write a C program to store elements in a 2-D array of 2 rows and 3 columns and display the matrix.

Program

#include <stdio.h> int main() { int arr[2][3]; int i, j; printf("Enter 6 elements: "); // Read elements row by row for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } printf("Matrix:\n"); // Display the matrix for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }

Explanation

A two-dimensional array stores data in rows and columns. Two indexes are used: the first represents the row and the second represents the column.

Expected Output

Enter 6 elements: 1 2 3 4 5 6

Matrix:

1 2 3

4 5 6

2-D Array — Exercise 2

Problem

Write a C program to calculate the sum of all elements of a 2-D array.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, sum = 0; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Calculate total for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { sum = sum + arr[i][j]; } } printf("Sum = %d\n", sum); return 0; }

Explanation

Nested loops are used to visit every row and column of the matrix. Each element is added to the running total.

Expected Output

Enter 9 elements: 1 2 3 4 5 6 7 8 9

Sum = 45

2-D Array — Exercise 3

Problem

Write a C program to find the sum of each row of a 2-D array.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, sum; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Calculate sum of each row for (i = 0; i < 3; i++) { sum = 0; for (j = 0; j < 3; j++) { sum = sum + arr[i][j]; } printf("Sum of row %d = %d\n", i + 1, sum); } return 0; }

Explanation

The outer loop selects a row, while the inner loop processes all elements belonging to that row.

Expected Output

Enter 9 elements: 1 2 3 4 5 6 7 8 9

Sum of row 1 = 6

Sum of row 2 = 15

Sum of row 3 = 24

2-D Array — Exercise 4

Problem

Write a C program to find the sum of each column of a 2-D array.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, sum; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Calculate sum of each column for (j = 0; j < 3; j++) { sum = 0; for (i = 0; i < 3; i++) { sum = sum + arr[i][j]; } printf("Sum of column %d = %d\n", j + 1, sum); } return 0; }

Explanation

Here the outer loop selects a column and the inner loop visits each row element in that column.

Expected Output

Enter 9 elements: 1 2 3 4 5 6 7 8 9

Sum of column 1 = 12

Sum of column 2 = 15

Sum of column 3 = 18

2-D Array — Exercise 5

Problem

Write a C program to find the largest element in a 2-D array.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, largest; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Start with the first element largest = arr[0][0]; // Search for the largest element for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (arr[i][j] > largest) { largest = arr[i][j]; } } } printf("Largest = %d\n", largest); return 0; }

Explanation

Every element of the matrix is compared with the current largest value. Whenever a larger value is found, it becomes the new largest value.

Expected Output

Enter 9 elements: 12 5 30 8 45 17 3 25 10

Largest = 45

2-D Array — Exercise 6

Problem

Write a C program to find the transpose of a 2-D matrix.

Program

#include <stdio.h> int main() { int arr[2][3]; int i, j; printf("Enter 6 elements: "); // Read original matrix for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } printf("Transpose:\n"); // Swap rows and columns while displaying for (j = 0; j < 3; j++) { for (i = 0; i < 2; i++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }

Explanation

The transpose of a matrix is obtained by converting its rows into columns and its columns into rows.

Expected Output

Enter 6 elements: 1 2 3 4 5 6

Transpose:

1 4

2 5

3 6

2-D Array — Exercise 7

Problem

Write a C program to add two 2 × 2 matrices.

Program

#include <stdio.h> int main() { int a[2][2], b[2][2], sum[2][2]; int i, j; printf("Enter elements of first matrix: "); // Read first matrix for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { scanf("%d", &a[i][j]); } } printf("Enter elements of second matrix: "); // Read second matrix for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { scanf("%d", &b[i][j]); } } // Add corresponding elements for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { sum[i][j] = a[i][j] + b[i][j]; } } printf("Sum matrix:\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { printf("%d ", sum[i][j]); } printf("\n"); } return 0; }

Explanation

Matrix addition is performed by adding corresponding elements of the two matrices.

Expected Output

Enter elements of first matrix: 1 2 3 4

Enter elements of second matrix: 5 6 7 8

Sum matrix:

6 8

10 12

2-D Array — Exercise 8

Problem

Write a C program to find the sum of the main diagonal elements of a square matrix.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, sum = 0; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Add main diagonal elements for (i = 0; i < 3; i++) { sum = sum + arr[i][i]; } printf("Main diagonal sum = %d\n", sum); return 0; }

Explanation

The main diagonal contains elements where the row index and column index are equal: arr[0][0], arr[1][1], and arr[2][2].

Expected Output

Enter 9 elements: 1 2 3 4 5 6 7 8 9

Main diagonal sum = 15

2-D Array — Exercise 9

Problem

Write a C program to check whether a square matrix is an identity matrix.

Program

#include <stdio.h> int main() { int arr[3][3]; int i, j, isIdentity = 1; printf("Enter 9 elements: "); // Read matrix elements for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } // Check identity matrix conditions for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (i == j) { if (arr[i][j] != 1) { isIdentity = 0; } } else { if (arr[i][j] != 0) { isIdentity = 0; } } } } if (isIdentity) printf("The matrix is an identity matrix.\n"); else printf("The matrix is not an identity matrix.\n"); return 0; }

Explanation

An identity matrix contains 1 on the main diagonal and 0 at every other position.

Expected Output

Enter 9 elements: 1 0 0 0 1 0 0 0 1

The matrix is an identity matrix.

2-D Array — Exercise 10

Problem

Write a C program to multiply two 2 × 2 matrices using 2-D arrays.

Program

#include <stdio.h> int main() { int a[2][2], b[2][2], result[2][2] = {0}; int i, j, k; printf("Enter elements of first matrix: "); // Read first matrix for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { scanf("%d", &a[i][j]); } } printf("Enter elements of second matrix: "); // Read second matrix for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { scanf("%d", &b[i][j]); } } // Multiply the two matrices for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { result[i][j] = result[i][j] + a[i][k] * b[k][j]; } } } printf("Result matrix:\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { printf("%d ", result[i][j]); } printf("\n"); } return 0; }

Explanation

Matrix multiplication uses three nested loops. For each result position, corresponding elements from a row of the first matrix and a column of the second matrix are multiplied and added.

Expected Output

Enter elements of first matrix: 1 2 3 4

Enter elements of second matrix: 5 6 7 8

Result matrix:

19 22

43 50