How The Technical Term Derived
How The Technical Term Derived
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurn around Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
Output:
Experiment : 2
Write a program to Implement SJF
Source Code :
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
Output :
Experiment: 5
Write a program to implement Stack Operation using Link List.
i. Push
ii. Pop
iii. Search
Source Code:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
while(choice != 4)
{
printf("Choose option(1-4): \n");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice!\n ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Not able to push the element!!");
}
else
{
printf("Enter the value: ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed!!!\n\n");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped...\n");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty!!!\n");
}
else
{
printf("Printing Stack elements: \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Output:
Push: Display: Pop:
Experiment: 6
Write a Program to implement Queue operation using Link List
i. Enqueue
ii. Dequeue
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void enqueue(int value) {
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node *temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
}
}
void display() {
struct node *temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n\n");
}
}
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("Dequeued element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
Output:
Enqueue: Display:
Dequeue:
Experiment-7
Write a program using linked list find sum of element on odd index number
Source code
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** root, int item)
{
Node *ptr = *root, *temp = new Node;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
Output-
Experiment-8
Write a program to print an array and size n to rotate by D either(calculate)
Source Code
#include <stdio.h>
int main()
{
int arr[] = {6, 5, 2, 8, 9};
int length = sizeof(arr)/sizeof(arr[0]);
int n = 3;
printf("Original array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
for(int i = 0; i < n; i++)
{
int j, first;
first = arr[0];
for(j = 0; j < length-1; j++)
{
arr[j] = arr[j+1];
}
arr[j] = first;
}
printf("\n");
printf("Array after left rotation: \n");
for(int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output-
Experiment No-9
#include <iostream>
using namespace std;
struct adjNode {
int val, cost;
adjNode* next;
};
struct graphEdge {
int start_ver, end_ver, weight;
};
class DiaGraph{
adjNode* getAdjListNode(int value, int weight, adjNode* head) {
adjNode* newNode = new adjNode;
newNode->val = value;
newNode->cost = weight;
newNode->next = head;
return newNode;
}
int N;
public:
adjNode **head;
DiaGraph(graphEdge edges[], int n, int N) {
head = new adjNode*[N]();
this->N = N;
for (int i = 0; i < N; ++i)
head[i] = nullptr;
for (unsigned i = 0; i < n; i++) {
int start_ver = edges[i].start_ver;
int end_ver = edges[i].end_ver;
int weight = edges[i].weight;
adjNode* newNode = getAdjListNode(end_ver, weight, head[start_ver]);
head[start_ver] = newNode;
}}
~DiaGraph() {
for (int i = 0; i < N; i++)
delete[] head[i];
delete[] head;
}};
void display_AdjList(adjNode* ptr, int i)
{
while (ptr != nullptr) {
cout << "(" << i << ", " << ptr->val
<< ", " << ptr->cost << ") ";
ptr = ptr->next;
}
cout << endl;
}
int main()
{
graphEdge edges[] = {
{0,1,2},{0,2,4},{1,4,3},{2,3,2},{3,1,4},{4,3,3}
};
int N = 6;
int n = sizeof(edges)/sizeof(edges[0]);
DiaGraph diagraph(edges, n, N);
cout<<"-----Mrinal Dev------"<<endl;
cout<<"Graph adjacency list "<<endl<<"(start_vertex, end_vertex, weight):"<<endl;
for (int i = 0; i < N; i++)
{
display_AdjList(diagraph.head[i], i);
}
return 0;
}
Output:-
Experiment No-10
Write a program to print check two string of Anagram
Source Code:-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int checkAnagram(char *str1, char *str2);
int main()
{
char str1[100], str2[100];
printf("\n\n Function : whether two given strings are anagram :\n");
printf("\n\n Example : pears and spare, stone and tones :\n");
printf("-------------------------------------------------------\n");
printf(" Input the first String : ");
fgets(str1, sizeof str1, stdin);
printf(" Input the second String : ");
fgets(str2, sizeof str2, stdin);
if(checkAnagram(str1, str2) == 1)
{
str1[strlen(str1)-1] = '\0';
str2[strlen(str2)-1] = '\0';
printf(" %s and %s are Anagram.\n\n",str1,str2);
}
else
{
str1[strlen(str1)-1] = '\0';
str2[strlen(str2)-1] = '\0';
printf(" %s and %s are not Anagram.\n\n",str1,str2);
}
return 0;
}
#include<stdio.h>
int fib(int n)
{
int f[n+2];
int i;
f[0] = 0;
f[1] = 1;
printf("<______Mrinal Dev_______>\n\n");
for (i = 2; i <= n; i++)
{
Output:-
Experiment No-12
Write a program to print longest common subsequence between two strings.
Source Code:-
#include <stdio.h>
#include <string.h>
int i, j, m, n, LCS_table[20][20];
char S1[20] = "ACADB", S2[20] = "CBDA", b[20][20];
void lcsAlgo() {
m = strlen(S1);
n = strlen(S2);
for (i = 0; i <= m; i++)
LCS_table[i][0] = 0;
for (i = 0; i <= n; i++)
LCS_table[0][i] = 0;
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
if (S1[i - 1] == S2[j - 1]) {
LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;
} else if (LCS_table[i - 1][j] >= LCS_table[i][j - 1]) {
LCS_table[i][j] = LCS_table[i - 1][j];
} else {
LCS_table[i][j] = LCS_table[i][j - 1];
}
}
int index = LCS_table[m][n];
char lcsAlgo[index + 1];
lcsAlgo[index] = '\0';
int i = m, j = n;
while (i > 0 && j > 0) {
if (S1[i - 1] == S2[j - 1]) {
lcsAlgo[index - 1] = S1[i - 1];
i--;
j--;
index--;
}
else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
i--;
else
j--;
}
printf("S1 : %s \nS2 : %s \n", S1, S2);
printf("LCS: %s", lcsAlgo);
}
int main() {
lcsAlgo();
printf("\n");
return 0;
}
Output:-
Experiment No-13
Write a program to print longest common subsequence between two string using
recursion
Source code:-
class Main {
static int lcs(String X, String Y, int m, int n) {
if (m == 0 || n == 0) {
return 0;
}
if (X.charAt(m - 1) == Y.charAt(n - 1)) {
return 1 + lcs(X, Y, m - 1, n - 1);
} else {
return Math.max(lcs(X, Y, m, n - 1),
lcs(X, Y, m - 1, n));
}
}
public static void main(String[] args) {
String X = "AGGTAB";
String Y = "GXTXAYB";
int m = X.length();
int n = Y.length();
System.out.println("Length of LCS: " + lcs(X, Y, m, n));
}
}
Output:-
Experiment: 14
Source Code:
#include <stdio.h>
void check(char ch)
{
else
printf("\n%c is not an aplhabetic character",ch);
}
int main()
{
char ch;
ch = 'A';
check(ch);
ch = 'a';
check(ch);
ch = '0';
check(ch);
return 0;
}
Output:
Experiment No. 15
Write a program to find minimum no. of Coin Require to given value.
Source Code:-
class Main
{
static int minCoins(int coins[], int m, int sum)
{
if (sum == 0) return 0;
Output:-