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

C Programs for 2D Array Operations

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)
15 views4 pages

C Programs for 2D Array Operations

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

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
[Link] 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 [Link] 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
[Link] 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
[Link] 2024-11-21

Example 04: C program to check sparse [Link] matrix is a special matrix with most of its
elements are [Link] 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

Common questions

Powered by AI

The program calculates the determinant of a 2x2 matrix A = [{a,b}, {c,d}] using the formula |A| = ad - bc, where a, b, c, and d are elements of the matrix. This method is based on the mathematical property that relates the determinant of a small matrix as a product and difference of its diagonal elements .

Matrix determinants, calculated as ad-bc for 2x2 matrices in the program, provide essential insights into properties such as matrix invertibility (non-zero determinant signifies invertibility), volume scaling, and solutions to linear equations, making determinants crucial in algebraic transformations and system solvability .

The program uses explicit prompts and limitations (e.g., fixed dimensions for determinant evaluation), simple loops for input acceptance, and consistency checks (e.g., sum of indices for diagonals). These elements ensure user inputs are properly managed, minimizing errors in matrix operations .

Transposing a matrix reorganizes data which can be crucial for operations like matrix multiplication and solving linear equations where orientation of data impacts performance. The program illustrates this by methodically swapping rows and columns, thereby facilitating these operations in computational applications .

The program reads matrix dimensions and elements, counts zero entries, and checks if they constitute at least half the matrix size. Identifying sparse matrices aids computational efficiency as operations can ignore vast zero entries, reducing complexity from O(M*N) operations to significant savings in storage and computation .

The program involves reading matrix dimension, inputting values, computing transpose by swapping indices (A[col][row] = B[row][col]), and finally printing the matrices. Each step ensures data integrity and correctness in matrix transformation critical for further mathematical operations .

The program handles input by using scanf to read elements into the matrix from standard input, guided by prompts for dimensions and element input. For output, it uses printf to display matrices, ensuring clear communication of matrix configurations and results, such as determinants or transposes, to the user .

The program calculates the sum of the minor diagonal elements by iterating through the matrix and checking if each element at position (row, col) satisfies the condition row + col = N - 1, which mathematically represents the minor diagonal in an N-dimensional square matrix .

The program takes an input for the dimension of a square matrix N, then reads the elements of matrix A. It computes the transpose matrix B by assigning B[row][col] = A[col][row] for each element position. This effectively flips A's rows into columns in B .

The program defines a matrix as sparse if the number of zero elements in the matrix is equal to or greater than half of the total elements, calculated as (M * N) / 2. This condition is significant because sparse matrices save space and computation time by focusing on non-zero elements in operations .

You might also like