Professional Documents
Culture Documents
Name: Mehedi Hasan Moni Admission Number: - 19SCSE1140001 B.tech CSE With CNCS, Sem: V
Name: Mehedi Hasan Moni Admission Number: - 19SCSE1140001 B.tech CSE With CNCS, Sem: V
(a) SJF:-
Code:-
#include <iostream>
using namespace std;
int mat[10][6];
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "Final Result...\n";
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n";
}
}
OUTPUT:-
(b) FCFS:-
Code:-
#include<iostream>
using namespace std;
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT:-
(c) PRIORITY:-
#include<bits/stdc++.h>
using namespace std;
struct Process
{
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};
findavgTime(proc, n);
}
// Driver code
int main()
{
Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}
OUTPUT:-
// Driver code
int main()
{
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
OUTPUT:
(e) SRTF:-
Code:-
#include<iostream>
int main()
int a[10],b[10],x[10];
int waiting[10],turnaround[10],completion[10];
int i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
cin>>n;
cin>>a[i];
cin>>b[i];
x[i]=b[i];
b[9]=9999;
smallest=9;
for(i=0; i<n; i++)
smallest=i;
b[smallest]--;
if(b[smallest]==0)
count++;
end=time+1;
completion[smallest] = end;
cout<<"p"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<<"\t\t"<<turnaround[i]<<"\t\t"<<
completion[i]<<endl;
tt = tt + turnaround[i];
Code:-
#include <stdio.h>
#include
<stdlib.h> int
mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces""item %d",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer""\n2. Press 2 for Consumer""\n3. Press 3 for
Exit");#pragma omp critical
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");
}
break
;case 3:
exit(0);
break
;
}
}
}
OUTPUT:-
3. Write a program to implement stack andqueue data structure
using arrays and linked list with all the operations applicableon
these.
(a) STACK:-
Code:-
#include <bits/stdc++.h>
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
//print all elements in stack :
cout<<"Elements present in stack : ";
while(!s.isEmpty())
{
// print top element in stack
cout<<s.peek()<<" ";
// remove top element in stack
s.pop();
}
return 0;
}
OUTPUT:-
(b) QUEUE:-
queue=[]
def enqueue(element):
queue.append(element)
OUTPUT:-
4. Write a program to implement queueusing stack and stack
using queue data structures.
#include <bits/stdc++.h>
using namespace std;
struct Queue {
stack<int> s1, s2;
void enQueue(int x)
{
// Move all elements from s1 to s2
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
// Return top of s1
int x = s1.top();
s1.pop();
return x;
}
};
// Driver code
int main()
{
Queue q;
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
return 0;
}
OUTPUT:-
#include <bits/stdc++.h>
class Stack {
// Two inbuilt queues
queue<int> q1, q2;
public:
Stack()
{
curr_size = 0;
}
void push(int x)
{
curr_size++;
void pop()
{
int top()
{
if (q1.empty())
return -1;
return q1.front();
}
int size()
{
return curr_size;
}
};
// Driver code
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
OUTPUT:-
5. Write a program to implement linked list with
operation.
(a) Traversing:-
Code:-
class Node:
'''This class is used for linked list'''
def _init_(self,value):
self.data=valu
e
self.next=Non
e
node_a=Node(10)
node_b=Node(20)
node_c=Node(45)
node_a.next=node_b
node_b.next=node_c
head=node_a
start=head
while start!=None:
print(start.data,'| next ------- >' ,end=' ')
start=start.nex
tprint('None')
OUTPUT:-
(b) Searching an element in a linked list:-Code:-
#include <bits/stdc++.h>
using namespace std;
#include <stdlib.h>
#include <iostream>
using namespace std;
// Create a node
struct Node {
int data;
struct Node* next;
};
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
last->next = new_node;
return;
}
// Delete a node
void deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
free(temp);
}
// Search a node
bool searchNode(struct Node** head_ref, int key) {
struct Node* current = *head_ref;
if (head_ref == NULL) {
return;
} else {
while (current != NULL) {
// index points to the node next to current
index = current->next;
// Driver program
int main() {
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
int item_to_find = 3;
if (searchNode(&head, item_to_find)) {
cout << endl << item_to_find << " is found";
} else {
cout << endl << item_to_find << " is not found";
}
sortLinkedList(&head);
cout << "\nSorted List: ";
printList(head);
}
OUTPUT:-
(d) Deleting an element in a linked list:-
Code:-
#include <stdlib.h>
#include <iostream>
using namespace std;
// Create a node
struct Node {
int data;
struct Node* next;
};
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
last->next = new_node;
return;
}
// Delete a node
void deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
free(temp);
}
// Search a node
// Driver program
int main() {
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
OUTPUT:-
(e) Doubly Linked List:-
Code:-
class Node:
# 1. Allocates node
# 2. Put the data in it
new_node = Node(new_data)
# 1. Allocates node
# 2. Put in the data
new_node = Node(new_data)
return
# Insert 8, after 7.
# So linked list becomes 1->7->8->6->4->None
llist.insertAfter(llist.head.next, 8)
llist.printList(llist.head)
OUTPUT:-
6. Reverse the linked list in pairs.
Code:-
class Node:
# Constructor to initialize
the node object
def init (self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize
head
def init (self):
self.head = None
# Function to pairwise
swap elements of a linked
list
def pairwiseSwap(self):
temp = self.head
# Traverse furthethr
only if there are at least two
# left
while(temp and
temp.next):
temp.next.data):
# Swap data of
node with its next node's
data
temp.data,
temp.next.data =
temp.next.data, temp.data
# Move temp by 2 to
the next pair
temp =
temp.next.next
# Driver program
llist = LinkedList()
llist.push(5)
llist.push(4)
llist.push(3)
llist.push(2)
llist.push(1)
llist.pairwiseSwap()
Code:-
Code:-
class newNode:
def _init_(self,data):
self.data = data
self.left = self.right =
Nonedef prInorder(node):
if (node == None)
:return
prInorder(node.left)
print(node.data, end = "
")prInorder(node.right)
def constructBinaryTreeUtil(pre, preM, preIndex,l, h,
size):if (preIndex >= size or l > h) :
return None , preIndex
root =
newNode(pre[preIndex])
preIndex += 1
if (l == h):
return root, preIndex
i=0
for i in range(l, h + 1):
if (pre[preIndex] == preM[i]):
break
if (i <= h):
root.left, preIndex = constructBinaryTreeUtil(pre, preM, preIndex,i, h, size)
root.right, preIndex = constructBinaryTreeUtil(pre, preM, preIndex, l + 1, i -
size 1,
)
preIndex = 0
preMIndex = 0
prInorder(root)
if _name_ =="_main_":
preOrder = [1, 2, 4, 5, 3, 6, 7]
preOrderMirror = [1, 3, 7, 6, 2, 5, 4]
size = 7
root = newNode(0)
constructBinaryTree(root, preOrder,preOrderMirror, size)
OUTPUT:-
9. Maximum length subsequence such that adjacent elements in the
subsequence have a common factor. Given an array arr[], the task
is to find the maximum length of a subsequence such that the
adjacentelements in the subsequence have a common factor.
Output: 5
Output: 6
Input: arr[] = { 1, 2, 2, 3, 3, 1 }
Output: 2
Code:-
import math as mt
N = 100005
MAX = 1000002
lpd = [0 for i in range(MAX)]
def preCompute():
lpd[0], lpd[1] = 1, 1
for i in range(2,
mt.ceil(mt.sqrt(MAX))):for j in
range(2 * i, MAX, i):
if (lpd[j] == 0):
lpd[j] = i
for i in range(2,
MAX):if (lpd[i] ==
0):
lpd[i] = i
def maxLengthSubsequence(arr,
n):dp = [1 for i in range(N +
1)] pos = dict()
for i in range(0, n):
while (arr[i] > 1):
p = lpd[arr[i]]
if (p in pos.keys()):
dp[i] = max(dp[i], 1 +
dp[pos[p]])pos[p] = i
while (arr[i] % p == 0):
arr[i] //= p
ans = 1
for i in range(0, n + 1):
ans = max(ans,
dp[i])
return ans
arr = [13, 2, 8, 6, 3, 1, 9]
n = len(arr)
preCompute()
print(maxLengthSubsequence(arr, n))
OUTPUT:-
10.(i)Display details of jobs where theminimum salary is
greater than 10000.
Ans:-
(ii) Display the first name and join dateof the employees who joined
between 2002 and 2005.
Ans:-
(iii)Display first name and join date of the employees who is either IT
Programmer or Sales Man.
Ans:-
SELECTFIRST_NAME,HIRE_DATEFROM EMPLOYEES WHERE JOB_ID IN ('IT_PR',
'SA_MAN')
Ans:-
Ans:-
(vi).Display first name, salary, commission pct, and hire date for
employees with salary less than 10000.
Ans:-
SELECT FIRST_NAME, SALARY, COMMISSION_PCT, HIRE_DATE FROM
EMPLOYEESWHERE SALARY<10000
(vii) Display job Title, the difference between minimumand maximum salaries
Ans:-
Ans:-
Ans:-
(x) Display employees where the firstname or last name starts with
S.
Ans:-
Ans:-
Ans:-
Ans:-
SELECTFIRST_NAME,HIRE_DATE,FLOOR((SYSDATE-HIRE_DATE)/365)FROM
EMPLOYEES.
Ans:-
'YYYY')=2001.
(xvi) Display first name and last name after converting the first letter of each
Ans:-
1) FROM JOBS.
(xviii) Display the length of first name foremployees where last name
contain character ‘b’ after 3rd position.
Ans:-
INSTR(LAST_NAME,'B') > 3.
(xix).Display first name in upper case and email address in lower case for
employees where the first name and email address are same irrespective of the
case.
Ans:-
UPPER(FIRST_NAME)= UPPER(EMAIL).
Ans:-
SELECT*FROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'YYYY')=TO_CHAR(SYSDA
TE, 'YYYY').
(xxi).Display the number of days between system date and 1st January 2011.
Ans:-
Ans:-
(xxiii).Display employee ID and the dateon which he ended his previous job.
Ans:-
EMPLOYEE_ID.
Ans:-
Ans:-
SELECT COUNTRY_ID, COUNT(*) FROM LOCATIONS GROUP BY COUNTRY_ID.
Ans:-
Ans:-
AVG(SALARY)>10000.
Ans:-
NULLGROUP BY DEPARTMENT_IDHAVINGCOUNT(COMMISSION_PCT)>5.
(xxxi) Display employee ID for employees who did more than one job
in the past.
Ans:-
COUNT(*) > 1.
(xxxii) Display job ID of jobs that were done by more than 3 employees
for more than 100 days.
Ans:-
Ans:-
SELECTDEPARTMENT_ID,TO_CHAR(HIRE_DATE,'YYYY'),
COUNT(EMPLOYEE_I
D) FROM EMPLOYEES GROUP BY DEPARTMENT_ID, TO_CHAR(HIRE_DATE,
'YYYY') ORDER BY DEPARTMENT_ID.
Ans:-
Ans:-
Ans:-
Ans:-
Ans:-
Ans:-
Ans:-
Ans:-
SELECT DEPARTMENT_NAME, FIRST_NAME, SALARY
FROM DEPARTMENTS D JOIN EMPLOYEES E ON
(D.MANAGER_ID=E.MANAGER_ID) WHERE (SYSDATE-HIRE_DATE) / 365 > 5.
Ans:-
(xxxix).Display employee name, job title for the jobs employee did in the past where the job
was done less than six months.
Ans:-
Ans:-
select salary
from employees main
where 2 = (select count( distinct salary
)from employees
where salary > main.salary).
Ans:-
Ans:-
Ans:-
Ans:-
(xlvi) Display employee name, job title,start date, and end date of past
jobs ofall employees with commissionpercentage
null.
Ans:-
Ans:-
Ans:-
(xlix) Display job title and average salary for employees who did
a job in the past.
Ans:-
(l) Display employees who did notdo any job in the past.
Ans:-
Ans:-
SELECT TO_CHAR(HIRE_DATE,'MON-YY')
FROM EMPLOYEES JOIN DEPARTMENTS USING (DEPARTMENT_ID)
JOINLOCATIONS USING (LOCATION_ID)
WHERE CITY = 'Seattle'
GROUP BY TO_CHAR(HIRE_DATE,'MON-YY')
HAVING COUNT(*) > 5.
Ans:-
Ans:-
Ans:-
The code should also return false if the graph cannot be colored with m
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
// if coloring is safe
if (isSafe(graph, color)) {
color[i] = 0;
}
return false;
}
/* A utility function to print solution */
void printSolution(int color[])
{
cout << "Solution Exists:" " Following are the assigned colors \n";
for (int i = 0; i < V; i++)
cout << " " << color[i];
cout << "\n";
}
// Driver code
int main()
{
/* Create following graph and
test whether it is 3 colorable
(3)---(2)
|/|
|/|
|/|
(0)---(1)
*/
bool graph[V][V] = {
{ 0, 1, 1, 1 },
{ 1, 0, 1, 0 },
{ 1, 1, 0, 1 },
{ 1, 0, 1, 0 },
};
int m = 3; // Number of colors
if (!graphColoring(graph, m, 0, color))
cout << "Solution does not exist";
return 0;
}
OUTPUT:-
2.PL/SQL COMMANDS.
(a)
Create a program that accepts two numbers from substitution variables, display
one of the following messages:-
second.
Ans:-
DECLARE
l_number1 NUMBER := &1;
l_number2 NUMBER := &2;
BEGIN
IF l_number1 > l_number2 THEN
DBMS_OUTPUT.put_line('first is greater than
second'); ELSIF l_number1 < l_number2 THEN
DBMS_OUTPUT.put_line('first is less than second');
ELSE
DBMS_OUTPUT.put_line('first is same as
second'); END IF;
END;
(b)
Create a program that accepts a single number. Display the message Hello
World X times, where X is the number entered.
Ans:-
DECLARE
l_times NUMBER := &1;
BEGIN
FOR l_loop IN
1..l_timesLOOP
DBMS_OUTPUT.put_line('Hello World');
END LOOP;
END;
(c)
Try running the above program and entering a negative number, what happens?
Change the code to print a message if a number less than 1 is entered that informs
the user they must enter a number greater than or equal to 1.
Ans:-
DECLARE
l_times NUMBER := &1;
BEGIN
IF l_times < 1 THEN
DBMS_OUTPUT.put_line('Number must be at least
1'); ELSE
FOR l_loop IN
1..l_timesLOOP
DBMS_OUTPUT.put_line('Hello World');
END LOOP;
END
IF;
END;
(a)
Write a program that gives all employees in department 10 a 15% pay increase.
Display a messagedisplaying how many employees were awarded the increase.
Ans:-
BEGIN
UPDATE emp
SET sal = sal * 1.15
WHERE deptno =
10;
DBMS_OUTPUT.put_line(TO_CHAR(SQL%ROWCOUNT)||
' employee(s)
updated'); END;
(B)Create a PL/SQL block that accepts a new job title and an old job title. Find all
employees in the old job and give them the new job. Ensure a valid message is given
back to the user even if no employees changed job.
Ans:-
DECLARE
l_old_job emp.job%TYPE :=
'&1'; l_new_job emp.job%TYPE
:= '&2'; BEGIN
UPDATE emp
SET job = l_new_job
WHERE job =
l_old_job;IF
SQL%FOUND THEN
DBMS_OUTPUT.put_line(TO_CHAR(SQL%ROWCOUNT)||
' employee(s) changed
job'); ELSE
DBMS_OUTPUT.put_line('No employee found with
job'|| ' of '||l_old_job);
END
IF;
END;
EXPLICIT CURSORS
(a)
Create a program that mimics selecting all columns and rows from the dept table.
There is no need to format the output, just select all columns and all rows.Use a
cursor FOR loop.
Ans:-
DECLARE
CURSOR
dept_curIS
SELECT deptno
, dname
, loc
FROM
dept;
BEGIN
FOR r_dept in
dept_curLOOP
DBMS_OUTPUT.put_line(r_dept.deptno
);
DBMS_OUTPUT.put_line(r_dept.dname
);DBMS_OUTPUT.put_line(r_dept.loc);
END LOOP;
END;
(b)
Create a program that copies all departments to a table called old_dept. Do not
use a cursor FOR loop. Display how many rows were copied.
Ans:-
DECLARE
CURSOR
dept_curIS
SELECT deptno
, dname
, loc
FROM dept;
r_dept
dept_cur%ROWTYPE;
BEGIN
OPEN dept_cur;
LOOP
FETCH dept_cur INTO r_dept;
EXIT WHEN dept_cur%NOTFOUND;
INSERT INTO old_dept
(deptno,dname,loc) VALUES
(r_dept.deptno,r_dept.dname,r_dept.lo
c);END LOOP;
DBMS_OUTPUT.put_line(TO_CHAR(dept_cur%ROWCOUN
T)|| ' department(s) copied');
CLOSE
dept_cur;END;
PROCEDURES/FUNCTIONS
(a) Create a procedure that deletes rows from the old_emp table. It should accept 1
parameter, job; only delete the employee’s with that job. Display how many
employees were deleted. Write a program to invoke the procedure.
Ans:-
(B)Change the above procedure so that it returns the number of employees removed via
an OUT parameter. Write a program to invoke the procedure and displayhow many
employees were deleted.
Ans:-
(c) Convert the above program to a function. Instead of using an OUT parameter for the
number of employeesdeleted, use the functions return value. Write a program to
invoke the function and display how many employees were deleted.
Ans:-