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

Lab 19

Uploaded by

sadmannsulibrary
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)
4 views4 pages

Lab 19

Uploaded by

sadmannsulibrary
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/ 4

lab19.

md 2024-11-21

CSE115L Week:11(2D Array)


Name:
ID:
Section:

Example 01: C Program to print the transpose of a Squre matrics.


#include <stdio.h>
int main()
{
int N;
printf("Enter Natrix dimention : ");
scanf("%d", &N);
int A[N][N], B[N][N];
printf("Enter eleNents in Matrix of size %dx%d: \n", N, N);
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
scanf("%d", &A[row][col]);
}
}
// Compute Natrix B: the transpose of Natrix A
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
B[row][col] = A[col][row]; // Store each row of A to each
column of Natrix B
}
}
// Prints the original Natrix A
printf("\nOriginal Natrix: \n");
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
printf("%d ", A[row][col]);
}
printf("\n");
}
// Prints the transpose of Natrix A

1/4
lab19.md 2024-11-21

printf("Transpose of Natrix A: \n");


for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
printf("%d ", B[row][col]);
}
printf("\n");
}
return 0;
}

Example 2 : wtite a C Program to calculate determinant of 2×2 matrix.The Determinant of a


matrix is a special number that can be calculated from the elements of a square matrix. The
determinant of a matrix A is denoted by det (A), det A or |A|.
A = [
{a,b},
{c,d}
]
|A| = ad - bc

#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

Example 03 : C program to find sum of opposite(minor) diagonal elements of a Squre matrix


#include <stdio.h>
int main()
{
printf("Enter dimention of a Squre Matrix: ");
int N;
scanf("%d", &N);

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

You might also like