0% found this document useful (0 votes)
4 views25 pages

2d Array Example

The document provides a comprehensive overview of 2D arrays in C, including their declaration, initialization, and various operations such as printing, summing elements, transposing, and matrix multiplication. It includes multiple examples demonstrating these concepts, such as accessing specific elements, calculating row and column sums, and creating identity matrices. Additionally, it covers more advanced topics like checking for symmetric matrices and performing matrix addition.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

2d Array Example

The document provides a comprehensive overview of 2D arrays in C, including their declaration, initialization, and various operations such as printing, summing elements, transposing, and matrix multiplication. It includes multiple examples demonstrating these concepts, such as accessing specific elements, calculating row and column sums, and creating identity matrices. Additionally, it covers more advanced topics like checking for symmetric matrices and performing matrix addition.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

A 2D array in C is essentially an array of arrays, allowing you to store data in rows and

columns. Here's a basic example:

c
CopyEdit
#include <stdio.h>

int main() {
// Declare a 2D array with 3 rows and 4 columns
int matrix[3][4] = {
{1, 2, 3, 4}, // Row 1
{5, 6, 7, 8}, // Row 2
{9, 10, 11, 12} // Row 3
};

// Print the 2D array


for (int i = 0; i < 3; i++) { // Iterate through rows
for (int j = 0; j < 4; j++) { // Iterate through columns
printf("%d ", matrix[i][j]);
}
printf("\n"); // Newline after each row
}

return 0;
}

Explanation:

 Declaration: int matrix[3][4] declares a 2D array with 3 rows and 4 columns.


 Initialization: The array is initialized with values for each element, arranged in a grid
format.
 Nested Loops: Two for loops are used to iterate through the array: the outer loop for
rows (i) and the inner loop for columns (j).
 Output: Each element of the 2D array is printed, and a newline is added after each
row for better readability.

4o

Example 1: Simple Initialization and Printing


#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Example 2: Uninitialized 2D Array


#include <stdio.h>
int main() {
int arr[3][3]; // Uninitialized 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = i + j; // Filling with values
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Example 3: Sum of Elements in a 2D Array


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][2] = {{1, 2}, {3, 4}};
int sum = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum += arr[i][j];
}
}
printf("Sum = %d\n", sum);
return 0;
}

Example 4: Row-wise Sum


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += arr[i][j];
}
printf("Sum of row %d = %d\n", i, rowSum);
}
return 0;
}

Example 5: Column-wise Sum


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int j = 0; j < 3; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += arr[i][j];
}
printf("Sum of column %d = %d\n", j, colSum);
}
return 0;
}

Example 6: Transpose of a Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 3; j++) {
transpose[j][i] = arr[i][j];
}
}

// Print transpose
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

Example 7: Matrix Multiplication


c
CopyEdit
#include <stdio.h>

int main() {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int c[2][2] = {0};

// Multiplying matrices a and b


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

// Print result matrix c


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

Example 8: Identity Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int identity[3][3] = {0};
for (int i = 0; i < 3; i++) {
identity[i][i] = 1; // Set diagonal elements to 1
}

// Print identity matrix


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", identity[i][j]);
}
printf("\n");
}
return 0;
}

Example 9: Check Symmetric Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 2, 3}, {2, 4, 5}, {3, 5, 6}};
int isSymmetric = 1;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = 0;
break;
}
}
}

if (isSymmetric)
printf("Matrix is symmetric\n");
else
printf("Matrix is not symmetric\n");

return 0;
}

Example 10: Diagonal Elements Sum


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


sum += arr[i][i]; // Sum of primary diagonal elements
}

printf("Sum of diagonal elements = %d\n", sum);


return 0;
}

These examples demonstrate various ways to use and manipulate 2D arrays in C.

4o

You said:

2d array example in python


ChatGPT said:

Here’s a basic example of a 2D array (which is essentially a list of lists) in Python:

Example 1: Creating and Printing a 2D Array


python
CopyEdit
# Create a 2D array (list of lists)
matrix = [
[1, 2, 3], # Row 1
[4, 5, 6], # Row 2
[7, 8, 9] # Row 3
]

# Print the 2D array


for row in matrix:
for element in row:
print(element, end=" ")
print() # For new line after each row

Example 2: Accessing Specific Elements


python
CopyEdit
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

# Access and print an element (e.g., element at 2nd row, 3rd column)
print("Element at [1][2]:", matrix[1][2])
Example 3: Sum of All Elements in a 2D Array
python
CopyEdit
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

total_sum = 0
for row in matrix:
total_sum += sum(row)

print("Sum of all elements:", total_sum)

Example 4: Transpose of a Matrix


python
CopyEdit
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

# Transposing the matrix


transpose = [[matrix[j][i] for j in range(len(matrix))] for i in
range(len(matrix[0]))]

# Print the transposed matrix


for row in transpose:
print(row)

Example 5: Create a 2D Array with Zeros (n x m Matrix)


python
CopyEdit
# Create a 3x4 matrix filled with zeros
rows, cols = 3, 4
matrix = [[0 for _ in range(cols)] for _ in range(rows)]

# Print the 2D array


for row in matrix:
print(row)

These examples cover creating, accessing, and manipulating 2D arrays (lists of lists) in
Python, which can be used for various matrix operations.

1/2
4o

You said:

Give me 2d array basic example in n c at least 20 example


ChatGPT said:
Here are 20 basic examples of 2D arrays in C, showing various ways to declare, initialize,
and manipulate them.

Example 1: Simple Initialization and Printing


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Example 2: Uninitialized 2D Array


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = i + j;
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Example 3: Access Specific Elements


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][2] = {{1, 2}, {3, 4}};
printf("Element at [1][1]: %d\n", arr[1][1]);
return 0;
}

Example 4: Sum of All Elements


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][2] = {{1, 2}, {3, 4}};
int sum = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum += arr[i][j];
}
}
printf("Sum of all elements: %d\n", sum);
return 0;
}

Example 5: Sum of Row Elements


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += arr[i][j];
}
printf("Sum of row %d: %d\n", i, rowSum);
}
return 0;
}

Example 6: Sum of Column Elements


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int j = 0; j < 3; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += arr[i][j];
}
printf("Sum of column %d: %d\n", j, colSum);
}
return 0;
}

Example 7: Transpose of a Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 3; j++) {
transpose[j][i] = arr[i][j];
}
}

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 2; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

Example 8: Matrix Addition


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

Example 9: Matrix Multiplication


c
CopyEdit
#include <stdio.h>

int main() {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int c[2][2] = {0};

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

for (int i = 0; i < 2; i++) {


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

Example 10: Identity Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int identity[3][3] = {0};
for (int i = 0; i < 3; i++) {
identity[i][i] = 1;
}

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
printf("%d ", identity[i][j]);
}
printf("\n");
}
return 0;
}

Example 11: Check Symmetric Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 2, 3}, {2, 4, 5}, {3, 5, 6}};
int isSymmetric = 1;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = 0;
break;
}
}
}

if (isSymmetric)
printf("Matrix is symmetric\n");
else
printf("Matrix is not symmetric\n");

return 0;
}

Example 12: Diagonal Elements Sum


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


sum += arr[i][i];
}

printf("Sum of diagonal elements: %d\n", sum);


return 0;
}

Example 13: Lower Triangular Matrix


c
CopyEdit
#include <stdio.h>

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

printf("Lower Triangular Matrix:\n");


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

return 0;
}

Example 14: Upper Triangular Matrix


c
CopyEdit
#include <stdio.h>

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

printf("Upper Triangular Matrix:\n");


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

return 0;
}

Example 15: Searching for an Element


c
CopyEdit
#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7,
4o

You said:

Continue
ChatGPT said:

c
CopyEdit
8, 9}};
int searchElement = 5;
int found = 0;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] == searchElement) {
printf("Element %d found at position [%d][%d]\n",
searchElement, i, j);
found = 1;
break;
}
}
}

if (!found) {
printf("Element %d not found in the matrix.\n", searchElement);
}

return 0;
}
Example 16: Checking if a Matrix is a Zero Matrix
c
CopyEdit
#include <stdio.h>

int main() {
int matrix[2][2] = {{0, 0}, {0, 0}};
int isZeroMatrix = 1;

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
if (matrix[i][j] != 0) {
isZeroMatrix = 0;
break;
}
}
}

if (isZeroMatrix) {
printf("Matrix is a Zero Matrix\n");
} else {
printf("Matrix is not a Zero Matrix\n");
}

return 0;
}

Example 17: Finding Maximum Element in a 2D Array


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] > maxElement) {
maxElement = matrix[i][j];
}
}
}

printf("Maximum element in the matrix: %d\n", maxElement);


return 0;
}

Example 18: Finding Minimum Element in a 2D Array


c
CopyEdit
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 5, 3}, {9, 7, 2}, {6, 4, 8}};
int minElement = matrix[0][0];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] < minElement) {
minElement = matrix[i][j];
}
}
}

printf("Minimum element in the matrix: %d\n", minElement);


return 0;
}

Example 19: Checking if a Matrix is a Diagonal Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}};
int isDiagonalMatrix = 1;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (i != j && matrix[i][j] != 0) {
isDiagonalMatrix = 0;
break;
}
}
}

if (isDiagonalMatrix) {
printf("Matrix is a Diagonal Matrix\n");
} else {
printf("Matrix is not a Diagonal Matrix\n");
}

return 0;
}

Example 20: Multiplying Each Element by a Constant


c
CopyEdit
#include <stdio.h>

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

printf("Matrix after multiplying by %d:\n", multiplier);


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

return 0;
}

These 20 examples cover a wide range of 2D array operations in C, such as basic


initialization, matrix manipulation, searching, and element-wise operations. They provide a
strong foundation for understanding how 2D arrays work in C and how they can be applied to
various computational problems.

4o

You said:

Give me again different basic 20 example of 2d array in c not repeat from above example
ChatGPT said:

Here are 20 more examples of 2D arrays in C that are different from the previous ones:

Example 21: Initialize All Elements to the Same Value


c
CopyEdit
#include <stdio.h>

int main() {
int arr[3][3] = {{5, 5, 5}, {5, 5, 5}, {5, 5, 5}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}

Example 22: 2D Array with User Input


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][2];
printf("Enter 4 elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &arr[i][j]);
}
}

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

return 0;
}

Example 23: Checking if Two 2D Arrays are Equal


c
CopyEdit
#include <stdio.h>

int main() {
int arr1[2][2] = {{1, 2}, {3, 4}};
int arr2[2][2] = {{1, 2}, {3, 4}};
int isEqual = 1;

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
if (arr1[i][j] != arr2[i][j]) {
isEqual = 0;
break;
}
}
}

if (isEqual) {
printf("Arrays are equal\n");
} else {
printf("Arrays are not equal\n");
}

return 0;
}

Example 24: Reverse Rows of a 2D Array


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

for (int i = 0; i < 2; i++) {


for (int j = 2; j >= 0; j--) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

Example 25: Rotate 2D Array by 90 Degrees


c
CopyEdit
#include <stdio.h>

int main() {
int arr[2][2] = {{1, 2}, {3, 4}};
int rotated[2][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
rotated[j][1-i] = arr[i][j];
}
}

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

return 0;
}

Example 26: Copying One 2D Array to Another


c
CopyEdit
#include <stdio.h>

int main() {
int arr1[2][2] = {{1, 2}, {3, 4}};
int arr2[2][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
arr2[i][j] = arr1[i][j];
}
}

printf("Copied array:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr2[i][j]);
}
printf("\n");
}
return 0;
}

Example 27: Create a Sparse Matrix


c
CopyEdit
#include <stdio.h>

int main() {
int sparseMatrix[3][3] = {{5, 0, 0}, {0, 8, 0}, {0, 0, 7}};

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

return 0;
}

Example 28: Find the Trace of a Matrix (Sum of Diagonal Elements)


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


trace += matrix[i][i];
}

printf("Trace of the matrix: %d\n", trace);

return 0;
}

Example 29: Mirror Image of 2D Array Horizontally


c
CopyEdit
#include <stdio.h>

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

printf("Mirror image horizontally:\n");


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

return 0;
}

Example 30: Mirror Image of 2D Array Vertically


c
CopyEdit
#include <stdio.h>

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

printf("Mirror image vertically:\n");


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

return 0;
}

Example 31: Lower Triangular Sum


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


for (int j = 0; j <= i; j++) {
sum += matrix[i][j];
}
}

printf("Sum of lower triangular elements: %d\n", sum);

return 0;
}

Example 32: Upper Triangular Sum


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


for (int j = i; j < 3; j++) {
sum += matrix[i][j];
}
}

printf("Sum of upper triangular elements: %d\n", sum);

return 0;
}

Example 33: Initialize a 2D Array with Random Numbers


c
CopyEdit
#include <stdio.h>
#include <stdlib.h>

int main() {
int matrix[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
matrix[i][j] = rand() % 100;
}
}

printf("Matrix with random numbers:\n");


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

return 0;
}

Example 34: Swapping Two Rows in a 2D Array


c
CopyEdit
#include <stdio.h>

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

// Swapping row 0 and row 2


for (int j = 0; j < 3; j++) {
temp = matrix[0][j];
matrix
4o
You said:

Continue
ChatGPT said:

c
CopyEdit
[0][j] = matrix[2][j];
matrix[2][j] = temp;
}

printf("Matrix after swapping row 0 and row 2:\n");


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

return 0;
}

Example 35: Swapping Two Columns in a 2D Array


c
CopyEdit
#include <stdio.h>

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

// Swapping column 0 and column 2


for (int i = 0; i < 3; i++) {
temp = matrix[i][0];
matrix[i][0] = matrix[i][2];
matrix[i][2] = temp;
}

printf("Matrix after swapping column 0 and column 2:\n");


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

return 0;
}

Example 36: Check if Matrix is Symmetric


c
CopyEdit
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {2, 5, 6}, {3, 6, 9}};
int isSymmetric = 1;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = 0;
break;
}
}
}

if (isSymmetric) {
printf("The matrix is symmetric.\n");
} else {
printf("The matrix is not symmetric.\n");
}

return 0;
}

Example 37: Finding the Sum of Diagonal Elements (Both Diagonals)


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


primaryDiagonalSum += matrix[i][i];
secondaryDiagonalSum += matrix[i][2-i];
}

printf("Sum of primary diagonal: %d\n", primaryDiagonalSum);


printf("Sum of secondary diagonal: %d\n", secondaryDiagonalSum);

return 0;
}

Example 38: Multiply Two Matrices


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

printf("Resultant matrix after multiplication:\n");


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

return 0;
}

Example 39: Count Zero Elements in a 2D Array


c
CopyEdit
#include <stdio.h>

int main() {
int matrix[3][3] = {{0, 2, 0}, {4, 0, 6}, {7, 8, 0}};
int zeroCount = 0;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
zeroCount++;
}
}
}

printf("Number of zero elements: %d\n", zeroCount);

return 0;
}

Example 40: Sum of Elements in Each Row


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


int rowSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += matrix[i][j];
}
printf("Sum of elements in row %d: %d\n", i+1, rowSum);
}
return 0;
}

Example 41: Sum of Elements in Each Column


c
CopyEdit
#include <stdio.h>

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

for (int j = 0; j < 3; j++) {


int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += matrix[i][j];
}
printf("Sum of elements in column %d: %d\n", j+1, colSum);
}

return 0;
}

Example 42: Find Row with Maximum Sum


c
CopyEdit
#include <stdio.h>

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

for (int i = 0; i < 3; i++) {


int rowSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += matrix[i][j];
}
if (rowSum > maxSum) {
maxSum = rowSum;
rowIndex = i;
}
}

printf("Row %d has the maximum sum: %d\n", rowIndex+1, maxSum);

return 0;
}

Example 43: Find Column with Maximum Sum


c
CopyEdit
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int maxSum = 0, colIndex = 0;

for (int j = 0; j < 3; j++) {


int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += matrix[i][j];
}
if (colSum > maxSum) {
maxSum = colSum;
colIndex = j;
}
}

printf("Column %d has the maximum sum: %d\n", colIndex+1, maxSum);

return 0;
}

You might also like