Dsa Lab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

SHARDA UNIVERSITY

Knowledge Park III, Greater Noida, Uttar Pradesh

Laboratory Report
Course Name: Object Oriented Programming using JAVA
Course Code: CSP242

Submitted by: Submitted to:


Name: Kushagra Singh Ms.kusum
lata
System ID: 2021507825 Assistant Prof,
CSE
Section/Group: Sec-B(G2)
Semester/Year: 3rd/2nd

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING,
SCHOOL OF ENGINEERING AND TECHNOLOGY,
SHARDA UNIVERSITY, GREATER NOIDA
INDEX
s.no Program Date Signature
remarks
Program -01
Write a program to perform operations on 2d matrix.
#include <stdio.h>
void input(int a[50][50], int row, int column)
{

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];
}

printf("minimum of array is : %d", min);


printf("\nmaximum of array is : %d", max);
}
int main()
{
int arr[50], n;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
tofindMaxandMin(arr, n);
}
Program-03
Write a menu driven program.
#include <stdio.h>
void main()
{
int num1, num2, ch;
printf("Enter the first Integer :");
scanf("%d", &num1);
printf("Enter the second Integer :");
scanf("%d", &num2);
printf("We have following operations to perform\n1-Addition.\n2-
Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
printf("Enter the choice of user: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("The Addition of %d and %d is: %d\n", num1, num2, num1 + num2);
break;

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;
}

// Recursively calling function twice


towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
2.Fibonaci Series
#include <stdio.h>
int fibonacci(int num)
{ //base condition
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return fibonacci(num - 1) + fibonacci(num - 2); // recursively call
}
}
int main()
{
int num;
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
printf("%d, ", fibonacci(i)); // calling fibonacci() function for each iteration
and printing the returned value
}
return 0;
}
Program-08
Write a program to implement stack:
1.using array
#include <stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
int peek();
void display(void);
int main()
{
int val;
top = -1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
printf("\n\t we have following operations to perform: ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t3.Peek\n\t 4.DISPLAY\n\t 5.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
val = peek(stack);
if (val != -1)
printf("\n The value stored at top of stack is %d", val);
break;
}
case 4:
{
display();
break;
}
case 5:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= n - 1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop()
{
if (top <= -1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d", stack[top]);
top--;
}
}
int peek(int stack[])
{
if (top == -1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (stack[top]);
}
void display()
{
if (top >= 0)
{
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Output:-
Program-09
Write a program to implement queue.
1.Using array
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
void insert();
void delete ();
void display();
int queue_array[MAX];
int rear = -1;
int front = -1;
void main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == -1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete ()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == -1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
2.Using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete ();
void display();
void main()
{
int choice;
while (choice != 4)
{
printf("We have following options: ");
printf("1.insert an element\n2.Delete an element\n3.Display the
queue\n4.Exit\n");
printf("Enter your choice ?");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *)malloc(sizeof(struct node));


if (ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{
front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if (front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if (front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values .....\n");
while (ptr != NULL)
{
printf("\n%d\n", ptr->data);
ptr = ptr->next;
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy