0% found this document useful (0 votes)
13 views16 pages

Dsa Lab 1

The document contains multiple C programs that perform various matrix operations, including calculating the determinant, displaying the matrix, finding the adjoint, and determining if a matrix is triangular. It also includes examples of using char and void pointers, as well as demonstrating overflow in an 8-byte processor. Each section provides code snippets along with expected outputs for user inputs.

Uploaded by

patwadivya787
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)
13 views16 pages

Dsa Lab 1

The document contains multiple C programs that perform various matrix operations, including calculating the determinant, displaying the matrix, finding the adjoint, and determining if a matrix is triangular. It also includes examples of using char and void pointers, as well as demonstrating overflow in an 8-byte processor. Each section provides code snippets along with expected outputs for user inputs.

Uploaded by

patwadivya787
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
You are on page 1/ 16

TO CALCULATE THE DETERMINANT OF THE MATRIX divya Patwa

/24U030070

#include <stdio.h>

int main() {

int matrix[3][3];

int determinant;

printf("give input to the matrix by pressing space\n");


// taking the input for 3x3 matrix

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

scanf("%d", &matrix[i][j]);

// Calculate the determinant using the formula:

determinant = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2]


* matrix[2][1])

- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] *


matrix[2][0])

+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] *


matrix[2][0]);

// Display the result

printf("\nThe determinant of the given matrix is: %d\n", determinant);

return 0;

OUTPUT

give input to the matrix by pressing space

123456789

The determinant of the given matrix is : 0


TO TAKE INPUT AND DISPLAY THE MATRIX divya Patwa /24U030070

int main() {

int rows=3;

int cols=3;

int matrix[3][3];

printf("give input to the matrix by pressing space\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix[i][j]);

}
printf("\nThe entered matrix is:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

return 0;

OUTPUT

give input to the matrix by pressing space

123456789

The entered matrix is:

123

456

789
TO FIND THE ADJOINT OF THE MATRIX divya Patwa /24U030070

#include <stdio.h>

void calculateCofactor(int matrix[3][3], int cofactor[3][3]) {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

// Calculate minor matrix elements for cofactor[i][j]

int minor[2][2];

int m = 0, n = 0;

for (int row = 0; row < 3; row++) {

for (int col = 0; col < 3; col++) {

if (row != i && col != j) {

minor[m][n] = matrix[row][col];

n++;

if (n == 2) {

n = 0;

m++;

// Cofactor element

cofactor[i][j] = (minor[0][0] * minor[1][1] - minor[0][1] * minor[1]


[0]);

if ((i + j) % 2 != 0) {

cofactor[i][j] = -cofactor[i][j];

}
void calculateAdjoint(int matrix[3][3], int adjoint[3][3]) {

int cofactor[3][3];

calculateCofactor(matrix, cofactor);

// Transpose of cofactor matrix gives adjoint

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

adjoint[i][j] = cofactor[j][i];

int main() {

int matrix[3][3];

int adjoint[3][3];

printf("give input to the matrix by pressing space\n");

// Input matrix

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("Element at [%d][%d]: ", i, j);

scanf("%d", &matrix[i][j]);

// Calculate adjoint

calculateAdjoint(matrix, adjoint);

// Print adjoint

printf("\nThe adjoint of the matrix is:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("%d ", adjoint[i][j]);


}

printf("\n");

return 0;

}
TO FIND INVERSE OF THE MATRIX divya Patwa /24U030070

#include <stdio.h>

#define N 3

void getCof(int mat[N][N], int cof[N][N], int p, int q, int n) {

int i = 0, j = 0;

for (int row = 0; row < n; row++) {

for (int col = 0; col < n; col++) {

if (row != p && col != q) {

cof[i][j++] = mat[row][col];

if (j == n - 1) {

j = 0;

i++;

int getDet(int mat[N][N], int n) {

if (n == 1) return mat[0][0];

int det = 0;

int cof[N][N];

int sign = 1;

for (int f = 0; f < n; f++) {

getCof(mat, cof, 0, f, n);

det += sign * mat[0][f] * getDet(cof, n - 1);

sign = -sign;

}
return det;

void adjoint(int mat[N][N], double adj[N][N]) {

if (N == 1) {

adj[0][0] = 1;

return;

int sign = 1;

int cof[N][N];

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

getCof(mat, cof, i, j, N);

sign = ((i + j) % 2 == 0) ? 1 : -1;

adj[j][i] = sign * getDet(cof, N - 1);

int inverse(int mat[N][N], double inv[N][N]) {

int det = getDet(mat, N);

if (det == 0) {

printf("Singular matrix, can't find its inverse\n");

return 0;

double adj[N][N];

adjoint(mat, adj);

for (int i = 0; i < N; i++)

for (int j = 0; j < N; j++)

inv[i][j] = adj[i][j] / det;

return 1;
}

int main() {

int mat[N][N] = { { 5, 3,6 },

{ 1, 0, 2 },

{ -3, 2,-4 }, };

double adj[N][N];

double inv[N][N];

printf("Input matrix is:\n");

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

printf("%d ", mat[i][j]);

printf("\n");

printf("\nThe Adjoint is:\n");

adjoint(mat, adj);

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

printf("%lf ", adj[i][j]);

printf("\n");

printf("\nThe Inverse is:\n");

if (inverse(mat, inv)) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

printf("%lf ", inv[i][j]);

printf("\n");
}

return 0;

}
TO CHECK THE MATRIX WHETHER IT IS TRIANGULAR OR NOT divya Patwa
/24U030070

#include <stdio.h>

#include <stdbool.h>

bool isLowerTriangular(int matrix[3][3], int size) {

for (int i = 0; i < size; i++) {

for (int j = i + 1; j < size; j++) {

if (matrix[i][j] != 0) {

return false;

return true;

bool isUpperTriangular(int matrix[3][3], int size) {

for (int i = 1; i < size; i++) {

for (int j = 0; j < i; j++) {

if (matrix[i][j] != 0) {

return false;

return true;

int main() {

int matrix[3][3];

printf("Enter the elements of the 3x3 matrix:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {


printf("Element at [%d][%d]: ", i, j);

scanf("%d", &matrix[i][j]);

if (isLowerTriangular(matrix, 3)) {

printf("\nThe matrix is a Lower Triangular Matrix.\n");

} else if (isUpperTriangular(matrix, 3)) {

printf("\nThe matrix is an Upper Triangular Matrix.\n");

} else {

printf("\nThe matrix is neither Lower Triangular nor Upper Triangular.\


n");

return 0;

}
TO USE CHAR POINTER divya Patwa /24U030070

#include<stdio.h>

// Overflow in char pointer

int main(){

int a=500;

char *p=&a;

printf("%d",*p);

return 0;

TO USE VOID POINTER divya Patwa /24U030070

include<stdio.h>

int main(){

int a=500;

void *p=&a;

printf("%d",*p);

return 0;

TO CONFIRM AND FIND THE NO. THAT OVERFLOW IN 8 BYTE PROCESSOR


divya Patwa /24U030070

#include<stdio.h>

// Overflow in 8 Byte

int main(){

int a=43000000000;

printf("%d",a*a/a);

return 0;

}
OUTPUT

OVERFLOW 50327040
for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("Element at [%d][%d]: ", i, j);

scanf("%d", &matrix[i][j]);

// Calculate adjoint

calculateAdjoint(matrix, adjoint);

// Print adjoint

printf("\nThe adjoint of the matrix is:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("%d ", adjoint[i][j]);

printf("\n");

return 0;

You might also like