0% found this document useful (0 votes)
9 views12 pages

Dsa Lab Assignment 03

The document contains a series of programming assignments related to data structures and algorithms, submitted by Rohan Nag. It includes code for matrix addition, multiplication, calculating the trace of a matrix, and using structures to manage student and employee data. Each assignment is accompanied by the relevant C code and output examples.

Uploaded by

Rohan Nag
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)
9 views12 pages

Dsa Lab Assignment 03

The document contains a series of programming assignments related to data structures and algorithms, submitted by Rohan Nag. It includes code for matrix addition, multiplication, calculating the trace of a matrix, and using structures to manage student and employee data. Each assignment is accompanied by the relevant C code and output examples.

Uploaded by

Rohan Nag
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/ 12

SCHOOL OF COMPUTER ENGINEERING

DATA STRUCTURE AND ALGORITHM LAB

Submitted By: Rohan Nag


Roll No.: 2105738
Branch: CSE
Section: CSE-32
ASSIGNMENT-2
Q1. Write a program to add two matrices and display it using function.
Code:
#include <stdio.h>
#include <stdio.h>
void readmatrix(int m[10][10], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("Enter element [%d,%d] : ", i + 1, j + 1);
scanf("%d", &m[i][j]);
}
}
}
void printmatrix(int m[10][10], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d ", m[i][j]);
}
printf("\n");
}
}

int main()
{
int a[10][10], b[10][10], result[10][10];
int i, j, rows1, cols1, rows2, cols2;

printf("Number of Rows of Matrix 1: ");


scanf("%d", &rows1);
printf("Number of Columns of Matrix 1: ");
scanf("%d", &cols1);

printf("\nEnter Elements of matrix 1:\n");


readmatrix(a, rows1, cols1);

printf("Number of Rows of Matrix 2: ");


scanf("%d", &rows2);
printf("Number of Columns of Matrix 2: ");
scanf("%d", &cols2);

printf("\nEnter Elements of Matrix 2: \n");


readmatrix(b, rows2, cols2);
if (rows1 == rows2 && cols1 == cols2)
{
for (i = 0; i < rows1; i++)
{
for (j = 0; j < cols1; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
printf("\nMatrix after adding:\n");
printmatrix(result, rows1, cols1);
}
else
{
printf("\nMatrix can not be added, Number of Rows & Columns are Different");
}
return 0;
}
Output:
Q2. Write a program to multiply two matrices and display it using function.
Code:
#include<stdio.h>
void multiply(int mat1[10][10],int mat2[10][10],int ,int ,int );

void main()
{
int mat1[10][10],mat2[10][10];
int i,j,k,m,n,p;
printf("Enter the number of rows and columns for 1st matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the elements of the 1st matrix: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the number of columns for 2nd matrix: ");
scanf("%d",&p);
printf("Enter the elements of the 2nd matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&mat2[i][j]);
}
}

printf("The 1st matrix:\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("The 2nd matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mat2[i][j]);
}
printf("\n");
}
multiply(mat1,mat2,m,n,p);
}
void multiply(int mat1[10][10],int mat2[10][10],int m,int n,int p)
{
int mul[10][10],i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}

printf("The resultant matrix formed on multiplying the two matrices:\n");


for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}

Output:
Q3. WAP to find the Trace(sum of the diagonal element) of a given mxn matrix using function
Code:
#include <stdio.h>
#include <stdlib.h>

int findTrace( int d, int** M){


int trace = 0;
for( int i = 0; i < d; i++ )
trace += M[i][i];

return trace;
}
int main(){
// Dimensions of matrix
int d;
printf("Enter row/col count of matrix: ");
scanf("%d", &d);

// Create a matrix
// Define resultant matrice
int **M = (int **)malloc(d * sizeof(int *));

// dynamically allocate memory of size d for each row


for (int i = 0; i < d; i++) {
M[i] = (int *)malloc(d * sizeof(int));
}

// Input
printf("Enter the matrix: \n");
for( int i = 0; i < d; i++ ){
printf("Row %d: ", i+1);
for( int j = 0; j < d; j++ )
scanf("%d", &M[i][j]);
}

printf("Trace value of matrix is %d.\n", findTrace(d, M));

// End
return 0;
}
Output:
Q4. Create a structure named student that has name, roll and mark as members. Assume appropriate
types and size of member. Write a program using structure to read and display the data entered by the
user by passing structure to a function.
Code:
#include <stdio.h>

struct Student {
char name[100];
int roll, totalMarks;
};
void input( struct Student *A ){
printf("Enter name: ");
scanf("%[^\n]s", A->name);

printf("Enter roll: ");


scanf("%d", &A->roll);

printf("Enter marks: ");


scanf("%d", &A->totalMarks);
}
void display( struct Student A ){
printf("Student details are: ");
printf("\nName: %s\nRoll: %d\nTotal marks: %d\n", A.name, A.roll, A.totalMarks);
}
int main(){
struct Student A;
input(&A);
display(A);
return 0;
}
Output:
Q5. WAP to store n employee’s data such as employee name, gender, designation, department, basic
pay. Calculate the gross pay of each employees as follows: Gross pay = basic pay + HR + DA HR=25% of
basic and DA=75% of basic by passing structure to a function.
Code:
#include <stdio.h>
struct employee
{
char name[10];
char gender[6];
char designation[10];
char department[10];
int basicpay;
};
float get_gross_pay(struct employee e)
{
float HRA = 0, DA = 0, gross = 0;
HRA = 0.25 * (e.basicpay);
DA = 0.75 * (e.basicpay);
gross = HRA + DA + e.basicpay;
return gross;
}
int main()
{
int n;
printf("Enter the number of employees = ");
scanf("%d", &n);
struct employee s[n];
for (int i = 0; i < n; i++)
{
printf("Enter employee name : ");
scanf("%s", s[i].name);
printf("Enter employee gender : ");
scanf("%s", s[i].gender);
printf("Enter employee designation : ");
scanf("%s", s[i].designation);
printf("Enter employee department : ");
scanf("%s", s[i].department);
printf("Enter employee basicpay : ");
scanf("%d", &s[i].basicpay);
}
for (int i = 0; i < n; i++)
{
float gross = get_gross_pay(s[i]);

printf("\nNAME : %s\nGENDER : %s\nDESIGNATION : %s\nDEPARTMENT : %s\nBASICPAY : %d\nGROSS : %.


2f\n", s[i].name, s[i].gender, s[i].designation, s[i].department, s[i].basicpay, gross);
gross = 0;
}

return 0;
}
Output:
Q6. Define a structure of student having data members: name, address, marks in C language, and marks in
information system. Take data for n students in an array and find the total marks obtained by passing
structure to a function.
Code:
#include <stdio.h>
struct student
{
char name[10];
char address[20];
int marks_C, marks_IS;
};
int get_total_marks(struct student s)
{
return s.marks_C + s.marks_IS;
}
int main()
{
int n;
printf("Enter the number of students = ");
scanf("%d", &n);
struct student s[n];
for (int i = 0; i < n; i++)
{
printf("\nEnter student name : ");
scanf("%s", s[i].name);
printf("Enter student address : ");
scanf("%s", s[i].address);
printf("Enter marks in C language : ");
scanf("%d", &s[i].marks_C);
printf("Enter marks in Information Systems : ");
scanf("%d", &s[i].marks_IS);
}
printf("LIST OF STUDENT DETAILS:\n");
for (int i = 0; i < n; i++)
{
int total = get_total_marks(s[i]);
printf("\nNAME : %s\nADDRESS : %s\nMARKS IN C LANGUAGE : %d\nMARKS IN SYSTEMS
INFORMATION : %d\nTOTAL MARKS: %d\n", s[i].name, s[i].address, s[i].marks_C, s[i].marks_IS, total);
total = 0;
}

return 0;
}
Output:
Q7. WAP to add two distances (in km-meter) by passing structure to a function.
Code:
#include <stdio.h>
struct Distance
{
int km;
int m;
};
void addDistances(struct Distance a, struct Distance b)
{
struct Distance result;
result.km = a.km + b.km;
result.m = a.m + b.m;
while (result.m >= 1000)
{
result.km++;
result.m -= 1000;
}
printf("\nSum of distances = %dkm and %dm", result.km, result.m);
}
int main()
{
struct Distance d1, d2;
printf("Enter 1st distance\n");
printf("Enter km: ");
scanf("%d", &d1.km);
printf("Enter meters: ");
scanf("%d", &d1.m);
printf("\nEnter 2nd distance\n");
printf("Enter km: ");
scanf("%d", &d2.km);
printf("Enter meters: ");
scanf("%d", &d2.m);
addDistances(d1, d2);
return 0;
}

Output:

Submitted By: Rohan Nag


Roll No.: 2105738
Branch: CSE
Section: CSE-32

You might also like