Dsa Lab Assignment 03
Dsa Lab Assignment 03
int main()
{
int a[10][10], b[10][10], result[10][10];
int i, j, rows1, cols1, rows2, cols2;
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]);
}
}
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>
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 *));
// 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]);
}
// 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);
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: