Lab 19
Lab 19
md 2024-11-21
1/4
lab19.md 2024-11-21
#include <stdio.h>
#define SIZE 2 // Matrix size
int main()
{
int A[SIZE][SIZE];
int row, col;
long det;
/* Input elements in matrix A from user */
printf("Enter elements in matrix of size 2x2: \n");
for (row = 0; row < SIZE; row++)
{
for (col = 0; col < SIZE; col++)
{
scanf("%d", &A[row][col]);
}
}
/* det(A) = ad - bc, a = A[0][0], b = A[0][1], c = A[1][0], d = A[1]
[1] */
det = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
printf("Determinant of matrix A = %ld", det);
return 0;
}
2/4
lab19.md 2024-11-21
int A[N][N];
int row, col, sum = 0;
/* Input elements in matrix from user */
printf("Enter elements in matrix of N %dx%d: \n", N, N);
for (row = 0; row < N; row++)
{
for (col = 0; col < N; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Find sum of minor diagonal elements */
for (row = 0; row < N; row++)
{
for (col = 0; col < N; col++)
{
/* If it is minor/opposite diagonal of matrix
* Since array elements starts from 0 hence i+j == (N - 1)
*/
if (row + col == ((N - 1)))
{
sum += A[row][col];
}
}
}
printf("\nSum of minor diagonal elements = %d", sum);
return 0;
}
3/4
lab19.md 2024-11-21
Example 04: C program to check sparse matrix.Sparse matrix is a special matrix with most of its
elements are zero.We can also assume that if (M * N) / 2 elements are zero then it is a sparse
matrix.
#include <stdio.h>
int main()
{
printf("Enter Matrix dimention: ");
int M, N;
scanf("%d%d", &M, &N);
int A[M][N];
int row, col, total = 0;
/* Input elements in matrix from user */
printf("Enter elements in matrix of size 3x3: \n");
for (row = 0; row < M; row++)
{
for (col = 0; col < N; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Count total number of zero elements in the matrix */
for (row = 0; row < M; row++)
{
for (col = 0; col < N; col++)
{
/* If the current element is zero */
if (A[row][col] == 0)
{
total++;
}
}
}
if (total >= (row * col) / 2)
{
printf("\nThe given matrix is a Sparse matrix.");
}
else
{
printf("\nThe given matrix is not Sparse matrix.");
}
return 0;
}
4/4