0% found this document useful (0 votes)
8 views4 pages

Matrix Multiplication

The document contains C code implementations for three matrix operations: multiplication, addition, and transposition. Each section includes user input for matrix elements, the logic for performing the operation, and output of the resulting matrix. The code is structured to handle 3x3 matrices for multiplication and addition, while the transposition section can accommodate matrices of varying dimensions.

Uploaded by

sankapalmaruti5
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)
8 views4 pages

Matrix Multiplication

The document contains C code implementations for three matrix operations: multiplication, addition, and transposition. Each section includes user input for matrix elements, the logic for performing the operation, and output of the resulting matrix. The code is structured to handle 3x3 matrices for multiplication and addition, while the transposition section can accommodate matrices of varying dimensions.

Uploaded by

sankapalmaruti5
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

Matrix Multiplication:

#include <stdio.h>

int main()
{
int mat1[3][3], mat2[3][3], result[3][3];
int i, j, k;

// Input for the first matrix


printf("Enter elements of the first 3x3 matrix:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("mat1[%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}

// Input for the second matrix


printf("Enter elements of the second 3x3 matrix:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("mat2[%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}

// Initialize result matrix to zero


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
result[i][j] = 0;
}
}

// Matrix multiplication logic


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 3; k++)
{
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
// Display the resulting matrix
printf("Resultant Matrix:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\t", result[i][j]);
}
printf("\n");
}

return 0;
}

MATRIX ADDITION:

#include <stdio.h>

int main()
{
int r, c;
int a[100][100], b[100][100], sum[100][100];

printf("Enter number of rows (1-100): ");


scanf("%d", &r);
printf("Enter number of columns (1-100): ");
scanf("%d", &c);

printf("\nEnter elements of first matrix:\n");


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("a[%d][%d]: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}

printf("\nEnter elements of second matrix:\n");


for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("b[%d][%d]: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}

// Matrix addition
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}

// Display result
printf("\nSum of the two matrices:\n");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

TRANSPOSE OF A MATRIX:
#include <stdio.h>

int main()
{
int a[10][10], transpose[10][10], r, c;

// Input the number of rows and columns


printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Input the elements of the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}

// Print the entered matrix


printf("\nEntered matrix:\n");
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
}

// Compute the transpose of the matrix


for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}
}

// Print the transpose of the matrix


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
{
for (int j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
}

return 0;
}

You might also like