0% found this document useful (0 votes)
3 views

2D Arrays Matrix Examples

The document contains multiple C programs demonstrating operations on matrices, including inputting a 2D array, summing two matrices, finding the transpose of a matrix, and multiplying two matrices. Each program includes initialization of matrices, performing the respective operation, and displaying the results. The code is structured with comments explaining each step for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2D Arrays Matrix Examples

The document contains multiple C programs demonstrating operations on matrices, including inputting a 2D array, summing two matrices, finding the transpose of a matrix, and multiplying two matrices. Each program includes initialization of matrices, performing the respective operation, and displaying the results. The code is structured with comments explaining each step for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Program to get the input of 2d array from the user and display it

#include <stdio.h>

int main()

int rows, cols;

// Ask the user for the number of rows and columns

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

// Declare a 2D array

int array[rows][cols];

// Input elements into the array

printf("Enter the elements of the array:\n");

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

printf("Element [%d][%d]: ", i, j);

scanf("%d", &array[i][j]);

// Display the array

printf("The array is:\n");

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

printf("%d 2",array[i][j] );

printf("\n");
}

return 0;

}
2. C Program to find the sum of 2 matrices

#include <stdio.h>

int main()

// Initialize two matrices

int matrix1[2][2] = {{1, 2}, {3, 4}};

int matrix2[2][2] = {{5, 6}, {7, 8}};

int sum[2][2]; // Matrix to store the result

// Perform matrix addition

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

for (int j = 0; j < 2; j++)

sum[i][j] = matrix1[i][j] + matrix2[i][j];

// Display the resultant matrix

printf("Sum of Matrix 1 and Matrix 2:\n\n");

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

for (int j = 0; j < 2; j++)

printf("%d ", sum[i][j]);

printf("\n");

return 0;

}
3. C program to find tranpose of matrix
#include <stdio.h>

int main()

// Initialize a 3x3 matrix

int matrix[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

int transpose[3][3]; // Array to store the transpose

// Calculate the transpose

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


{
for (int j = 0; j < 3; j++)
{
transpose[j][i] = matrix[i][j];
}
}
// Display the original matrix

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

// Display the transpose

printf("\nTranspose Matrix:\n");

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

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

printf("%d ", transpose[i][j]);

printf("\n");

return 0;

}
4. Matrix multiplication in C

#include <stdio.h>

int main() {

// Initialize two matrices

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] = {0}; // Resultant matrix initialized to 0

// Perform matrix multiplication

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

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

for (int k = 0; k < 3; k++)

result[i][j] += matrix1[i][k] * matrix2[k][j];


}
}

// Display the result

printf("Resultant Matrix:\n");

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

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

printf("%d ", result[i][j]);

printf("\n");

return 0;

You might also like