Dsa Lab File
Dsa Lab File
Dsa Lab File
Laboratory Report
Course Name: Object Oriented Programming using JAVA
Course Code: CSP242
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
{
printf("Enter element %d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
void sum(int a[50][50], int b[50][50], int c[50][50], int row, int column)
{
int i, j;
for (i = 0; i < row; ++i)
for (j = 0; j < column; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
void display(int a[50][50], int row, int column)
{
int i, j;
for (i = 0; i < row; ++i)
for (j = 0; j < column; ++j)
{
printf("%d ", a[i][j]);
if (j == column - 1)
{
printf("\n\n");
}
}
}
void subtract(int a[50][50], int b[50][50], int c[50][50], int row, int column)
{ int i, j;
for (i = 0; i < row; ++i)
for (j = 0; j < column; ++j)
{
c[i][j] = a[i][j] - b[i][j];
}
}
void multiply(int a[50][50], int b[50][50], int c[50][50], int row, int column)
{ int i, j, k;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
c[i][j] = 0;
for (k = 0; k < column; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void transpose(int a[50][50], int c[50][50], int row, int column)
{
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j)
{
c[j][i] = a[i][j];
}
}
int main()
{
int m, n, a[50][50], b[50][50], c[50][50];
char ch;
printf("Enter the number of rows (between 1 and 50): ");
scanf("%d", &m);
printf("Enter the number of columns (between 1 and 50): ");
scanf("%d", &n);
printf("Enter the elements in 1st matrix: \n");
input(a, m, n);
printf("Enter the elements in 2nd matrix: \n");
input(b, m, n);
printf("transpose of 1st matrix: \n");
transpose(a, c, m, n);
display(c, m, n);
printf("transpose of 2nd matrix: \n");
transpose(b, c, m, n);
display(c, m, n);
sum(a, b, c, m, n);
printf("Addition of two matrixs: \n");
display(c, m, n);
subtract(a, b, c, m, n);
printf("subtraction of two matrixs: \n");
display(c, m, n);
multiply(a, b, c, m, n);
printf("Multiplication operation: \n");
display(c, m, n);
return 0;
}
Output:-
Program-02
Write a program to find maximum and minimum element in array.
#include <stdio.h>
int tofindMaxandMin(int a[], int n)
{
int min, max, i;
min = max = a[0];
for (i = 1; i < n; i++)
{
if (min > a[i])
min = a[i];
if (max < a[i])
max = a[i];
}
case 2:
printf("The Substraction of %d and %d is: %d\n", num1, num2, num1 -
num2);
break;
case 3:
printf("The Multiplication of %d and %d is: %d\n", num1, num2, num1 *
num2);
break;
case 4:
if (num2 == 0)
{
printf("Division error occured.\n");
}
else
{
printf("division of %d and %d = %d", num1, num2, num1 / num2);
}
break;
default:
printf("Input correct option\n");
break;
}
}
Program -04
write a program to implement dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, prod = 1;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for (i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
prod += *(ptr + i);
}
printf("product of array=%d\n", prod);
printf("Memory ptr is freed.");
free(ptr);
return 0;
}
Program-05
Write a program to check whether matrix is sparse
or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row, col, i, j, arr[10][10], count = 0;
printf("Enter number of rows between(1-10): ");
scanf("%d", &row);
printf("Enter number of columns between(1-10): ");
scanf("%d", &col);
printf("Enter Element of Matrix:\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("Enter element %d%d: ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
}
printf("Elements are: \n");
for (i = 0; i < row; ++i)
for (j = 0; j < col; ++j)
{
printf("%d ", arr[i][j]);
if (j == col - 1)
{
printf("\n\n");
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == 0)
count++;
}
}
if (count > ((row * col) / 2))
printf("Matrix is a sparse matrix ");
else
printf("Matrix is not sparse matrix");
}
Program-06
Write a program using recursive function.
1.Tower of Hanoi.
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
// Base Condition if no of disks are
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}