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

Assignment 01 Abdullah

The document outlines a Linux system programming assignment involving the creation of a static library for matrix operations. It includes instructions for creating header and source files, compiling them into an object file, and linking them in a main program to perform matrix addition, multiplication, determinant calculation, and inversion. The code provided demonstrates the implementation of these functionalities in C.
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)
14 views4 pages

Assignment 01 Abdullah

The document outlines a Linux system programming assignment involving the creation of a static library for matrix operations. It includes instructions for creating header and source files, compiling them into an object file, and linking them in a main program to perform matrix addition, multiplication, determinant calculation, and inversion. The code provided demonstrates the implementation of these functionalities in C.
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

Name: Abdullah Jahangir

Reg#931-FET/BSEE/F21

Section: A

Linux System Programming

Assignment#1(CLO2)

Readme.txt:

First Create Abdullah.h:

Second Create Abdullah.c:

Now, Compile Object File using Terminal: gcc -c Abdullah.c -o Abdullah.o

Create Static Library: ar rcs libAbdullah.a Abdullah.o

Create main.c and use static library:

Compile and link the main program: gcc main.c -L. -lAbdullah -o main

Run/Execute the Program and then enter two matrices to get the output.

1. Create Abdullah.h:
#ifndef ABDULLAH_H
#define ABDULLAH_H

void Abdullah_Get(double Mat[3][3]);


void Abdullah_Show(double Mat[3][3]);
void Abdullah_Add(double Mat_A[3][3], double Mat_B[3][3], double
Mat_C[3][3]);
void Abdullah_Mul(double Mat_A[3][3], double Mat_B[3][3], double
Mat_C[3][3]);
void Abdullah_Inv(double Mat_A[3][3], double Mat_B[3][3]);
double Abdullah_Mod(double Mat_A[3][3]);

#endif
2. Create Abdullah.c:
#include <stdio.h>
#include "Abdullah.h"

void Abdullah_Get(double Mat[3][3]) {

1
printf("Enter matrix elements (3x3):\n");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%lf", &Mat[i][j]);
}

void Abdullah_Show(double Mat[3][3]) {


printf("Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
printf("%.2lf ", Mat[i][j]);
printf("\n");
}
}

void Abdullah_Add(double Mat_A[3][3], double Mat_B[3][3], double


Mat_C[3][3]) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
Mat_C[i][j] = Mat_A[i][j] + Mat_B[i][j];
}

void Abdullah_Mul(double Mat_A[3][3], double Mat_B[3][3], double


Mat_C[3][3]) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
Mat_C[i][j] = 0;
for (int k = 0; k < 3; k++)
Mat_C[i][j] += Mat_A[i][k] * Mat_B[k][j];
}
}

double Abdullah_Mod(double Mat_A[3][3]) {


return Mat_A[0][0] * (Mat_A[1][1] * Mat_A[2][2] - Mat_A[1][2] *
Mat_A[2][1])
- Mat_A[0][1] * (Mat_A[1][0] * Mat_A[2][2] - Mat_A[1][2] *
Mat_A[2][0])
+ Mat_A[0][2] * (Mat_A[1][0] * Mat_A[2][1] - Mat_A[1][1] *
Mat_A[2][0]);
}
void Abdullah_Inv(double Mat_A[][3], double Mat_B[][3]) {
double det = Abdullah_Mod(Mat_A);
if (det == 0) {
printf("Matrix is singular, cannot find inverse.\n");
return;
}
double inv_det = 1.0 / det;

2
Mat_B[0][0] = inv_det * (Mat_A[1][1] * Mat_A[2][2] - Mat_A[1][2] *
Mat_A[2][1]);
Mat_B[0][1] = inv_det * (Mat_A[0][2] * Mat_A[2][1] - Mat_A[0][1] *
Mat_A[2][2]);
Mat_B[0][2] = inv_det * (Mat_A[0][1] * Mat_A[1][2] - Mat_A[0][2] *
Mat_A[1][1]);
Mat_B[1][0] = inv_det * (Mat_A[1][2] * Mat_A[2][0] - Mat_A[1][0] *
Mat_A[2][2]);
Mat_B[1][1] = inv_det * (Mat_A[0][0] * Mat_A[2][2] - Mat_A[0][2] *
Mat_A[2][0]);
Mat_B[1][2] = inv_det * (Mat_A[0][2] * Mat_A[1][0] - Mat_A[0][0] *
Mat_A[1][2]);
Mat_B[2][0] = inv_det * (Mat_A[1][0] * Mat_A[2][1] - Mat_A[1][1] *
Mat_A[2][0]);
Mat_B[2][1] = inv_det * (Mat_A[0][1] * Mat_A[2][0] - Mat_A[0][0] *
Mat_A[2][1]);
Mat_B[2][2] = inv_det * (Mat_A[0][0] * Mat_A[1][1] - Mat_A[0][1] *
Mat_A[1][0]);
}

3. Now, Compile Object File using Terminal:

gcc -c Abdullah.c -o Abdullah.o

4. Create Static Library:

ar rcs libAbdullah.a Abdullah.o

5. Create main.c and use static library:


#include <stdio.h>
#include "Abdullah.h"

int main() {
double A[3][3], B[3][3], C[3][3];

Abdullah_Get(A);
Abdullah_Show(A);

Abdullah_Get(B);
Abdullah_Show(B);

Abdullah_Add(A, B, C);

3
printf("Sum of matrices:\n");
Abdullah_Show(C);

Abdullah_Mul(A, B, C);
printf("Multiplication of matrices:\n");
Abdullah_Show(C);

printf("\nDeterminant of Matrix A: %lf\n", Abdullah_Mod(A));

Abdullah_Inv(A, C);
printf("\nInverse of Matrix A:\n");
Abdullah_Show(C);

return 0;
}

6. Compile and link the main program:

gcc main.c -L. -lAbdullah -o main

7. Run/Execute the Program:

You might also like