C PROGRAM ON MATRIX
Matrix Multiplication in C
/*
* C program to perform matrix multiplication using iterative
*/
#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
return 0;
}
Runtime Testcases
Testcase 1: In this case, the columns of the first matrix must be equal to the rows of the second matrix.
Enter number of rows for First Matrix:
2
Enter number of columns for First Matrix:
2
Enter number of rows for Second Matrix:
2
Enter number of columns for Second Matrix:
2
Enter first matrix elements
1 3
5 6
Enter Second matrix elements
2 1
4 4
Multiplied matrix
14 13
34 29
Testcase 2: In this case, the columns of the first matrix are not equal to the rows of the second matrix.
Enter number of rows for First Matrix:
3
Enter number of columns for First Matrix:
4
Enter number of rows for Second Matrix:
1
Enter number of columns for Second Matrix:
2
Matrices Can't be multiplied together
C Program to Find Determinant of a Matrix
#include<stdio.h>
int main(){
int a[3][3], i, j;
long determinant;
printf("Enter the 9 elements of matrix: ");
for(i = 0 ;i < 3;i++)
for(j = 0;j < 3;j++)
scanf("%d", &a[i][j]);
printf("\nThe matrix is\n");
for(i = 0;i < 3; i++){
printf("\n");
for(j = 0;j < 3; j++)
printf("%d\t", a[i][j]);
}
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
printf("\nDeterminant of 3X3 matrix: %ld", determinant);
return 0;
}
$ gcc determinant.c -o determinant
$ ./determinant
Enter the 9 elements of matrix: 1 2 3 4 5 1 2 3 4
The matrix is
1 2 3
4 5 1
2 3 4
Determinant of 3X3 matrix: -5
C Program to Interchange Any Two Rows and Columns in the Matrix
/*
* C program to accept a matrix of given order and interchange
* any two rows and columns in the original matrix
*/
#include <stdio.h>
void main()
{
static int array1[10][10], array2[10][10];
int i, j, m, n, a, b, c, p, q, r;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficents of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d,", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("Enter the numbers of two rows to be exchanged \n");
scanf("%d %d", &a, &b);
for (i = 0; i < m; ++i)
{
/* first row has index is 0 */
c = array1[a - 1][i];
array1[a - 1][i] = array1[b - 1][i];
array1[b - 1][i] = c;
}
printf("Enter the numbers of two columns to be exchanged \n");
scanf("%d %d", &p, &q);
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
for (i = 0; i < n; ++i)
{
/* first column index is 0 */
r = array2[i][p - 1];
array2[i][p - 1] = array2[i][q - 1];
array2[i][q - 1] = r;
}
printf("The matix after interchanging the two rows(in the original matrix) \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("The matix after interchanging the two columns(in the original matrix) \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
}
$ cc pgm59.c
$ a.out
Enter the order of the matrix
2 2
Enter the co-efficents of the matrix
34 70
45 90
Enter the numbers of two rows to be exchanged
1 2
Enter the numbers of two columns to be exchanged
1 2
The given matrix is
34 70
45 90
The matix after interchanging the two rows(in the original matrix)
45 90
34 70
The matix after interchanging the two columns(in the original matrix)
70 34
90 45