ADA File
ADA File
ADA File
Lab Manual
Semester: III
Section: CS-K
VISION
MISSION
Page | 1
INDEX
Page | 2
EXPERIMENT 1
AIM : Write a program for Iterative and Recursive Binary Search.
Code :
Iterative binary search
#include<iostream>
using namespace std;
int main(){
int n,ans,data;
cout<<"enter size of array :";
cin>>n;
Page | 3
int arr[n];
cout<<"enter elements of array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"enter the data to be searched \n";
cin>>data;
ans= binarysearch(arr,0,n-1,data);
(ans==-1)?cout<<"Element is not present":cout<<"Element is present at
index "<<ans;
return 0;
}
else if(data<arr[mid])
return binarysearch(arr,start,mid-1,data);
Page | 4
else
return binarysearch(arr,mid+1,end,data);
}
return -1;
}
int main(){
int n,ans,data;
cout<<"enter size of array :";
cin>>n;
int arr[n];
cout<<"enter elements of array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"enter the data to be searched \n";
cin>>data;
ans= binarysearch(arr,0,n-1,data);
(ans==-1)?cout<<"Element is not present":cout<<"Element is present at
index "<<ans;
return 0;
}
Page | 5
OUTPUT:
Page | 6
EXPERIMENT 2
AIM : Write a program for Merge Sort.
Code :
#include<iostream>
using namespace std;
int L[n1],M[n2];
for(int i=0;i<n1;i++)
L[i]=arr[p+i];
for(int j=0;j<n2;j++)
M[j]=arr[m+1+j];
int i=0,j=0,k=p;
while(i<n1 && j<n2){
if(L[i]<=M[j]){
Page | 7
arr[k]=L[i];
i++;
}
else{
arr[k]=M[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=L[i];
i++;
k++;
}
while(j<n2){
arr[k]=M[j];
j++;
k++;
}
}
void mergesort(int arr[],int l,int h){
int m;
if(l<h){
// m= (l+(h-l))/2;
m=(l+h)/2;
Page | 8
mergesort(arr,l,m);
mergesort(arr,m+1,h);
merge(arr,l,m,h);
}
int main(){
int arr[]={4,9,8,12,45,3,7,2};
int n= sizeof(arr)/sizeof(arr[0]);
mergesort(arr,0,n-1);
cout<<"Sorted array: "<<endl;
print(arr,n);
return 0;
}
OUTPUT:
Page | 9
EXPERIMENT 3
AIM : Write a program for Quick Sort.
Code :
#include <iostream>
using namespace std;
Page | 10
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
if (start >= end)
return;
Page | 11
OUTPUT:
Page | 12
EXPERIMENT 4
AIM : Write a program for Strassen Matrix’s Multiplication.
Code :
#include<iostream>
using namespace std;
cout<<p5+p4-p2+p6<<" "<<p1+p2<<endl;
cout<<p3+p4<<" "<<p1+p5-p3-p7<<endl;
}
void print(int A[2][2]){
for(int i=0;i<2;i++){
cout<<endl;
for(int j=0;j<2;j++)
Page | 13
cout<<A[i][j]<<" ";
}
}
int main(){
int A[2][2],B[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cin>>A[i][j];
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cin>>B[i][j];
}
cout<<"A= ";
print(A);
cout<<"\nB= ";
print(B);
cout<<"\nC= ";
SMM(A,B);
return 0;
}
Page | 14
OUTPUT:
Page | 15
EXPERIMENT 5
AIM : Write a program to perform all the operations on stack and
deque.
Code :
For stack:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
Page | 16
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
Page | 17
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
For Deque:
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
Page | 18
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1) {
cout<<"Overflow"<<endl;
} else {
if(f==-1) {
f++;
r++;
} else
r=r+1;
a[r]=i;
cout<<"Inserted item is "<<a[r]<<endl;
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i;
Page | 19
cout<<"inserted element is: "<<i<<endl;
} else if(f!=0) {
a[--f]=i;
cout<<"inserted element is:"<<i<<endl;
} else
cout<<"Overflow";
}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty"<<endl;
return;
}
else {
cout<<"the deleted element is:"<<a[f]<<endl;
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty"<<endl;
return;
Page | 20
}
else {
cout<<"the deleted element is:"<<a[r]<<endl;
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty"<<endl;
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
}
int main() {
int c,i;
dequeue d;
cout<<"1.insert at beginning\n";
cout<<"2.insert at end\n";
cout<<"3.show\n";
cout<<"4.deletion from front\n";
Page | 21
cout<<"5.deletion from rear\n";
cout<<"6.exit\n";
do{
cout<<"enter your choice:";
cin>>c;
switch(c) {
case 1:{
cout<<"enter the element to be inserted"<<endl;
cin>>i;
d.insert_at_beg(i);
break;
}
case 2:{
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_end(i);
break;
}
case 3:{
d.show();
break;
}
case 4:{
d.delete_fr_front();
break;
}
Page | 22
case 5:{
d.delete_fr_rear();
break;
}
case 6:{
cout<<"Exit";
break;
}
default:{
cout<<"invalid choice";
break;
}
}
}while(c!=6);
}
Page | 23
OUTPUT:
Page | 24
EXPERIMENT 6
AIM : Write a program to perform all the operations on linked list.
Code :
#include <stdlib.h>
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Page | 25
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
Page | 26
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
Page | 27
cout << "Linked list: ";
printList(head);
OUTPUT:
Page | 28
EXPERIMENT 7
AIM : Write a program for Floyd-Warshall Problem.
Code :
#include <bits/stdc++.h>
using namespace std;
#define V 4
#define INF 99999
void printSolution(int dist[][V]);
void floydWarshall(int dist[][V])
{
int i, j, k;
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] > (dist[i][k] + dist[k][j])
&& (dist[k][j] != INF
&& dist[i][k] != INF))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V]){
cout << "The following matrix shows the shortest distances between
every pair of vertices \n";
Page | 29
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF"
<< " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}
int main(){
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
floydWarshall(graph);
return 0;
}
OUTPUT:
Page | 30
EXPERIMENT 8
AIM : Write a program for Hamiltonian Cycle Problem.
Code :
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define V 5
using namespace std;
void printSolution(int path[]);
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
if (graph [path[pos-1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
bool hamCycleUtil(bool graph[V][V], int path[], int pos){
if (pos == V) {
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}
Page | 31
for (int v = 1; v < V; v++){
if (isSafe(v, graph, path, pos)) {
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
bool hamCycle(bool graph[V][V]){
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false) {
cout<<"\nSolution does not exist"<<endl;
return false;
}
printSolution(path);
return true;
}
void printSolution(int path[]){
cout<<"Solution Exists:";
cout<<" Following is one Hamiltonian Cycle \n"<<endl;
for (int i = 0; i < V; i++)
Page | 32
cout<<path[i]<<" ";
cout<< path[0]<<endl;
}
int main(){
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
hamCycle(graph1);
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}
OUTPUT:
Page | 33