You are on page 1of 25

SEMSTER - 4

ADA LAB FILE

SUBMITED BY DEEPAK YADAV


BCA -4 | A5050489015
INDEX
S NO. PRACTICAL REMARKS

1) WAP TO DO LINEAR SEARCH IN AN


ARRAY
2) WAP TO DO BINARY SEARCH IN AN
ARRAY
3) WAP TO MERGE SORT AN ARRAY
4) WAP TO QUICK SORT AN ARRAY
5) WAP TO DO STRASSEN’S MATRIX
MULTIPLICATION
6) WAP FOR FRACTIONAL KNAPSACK
PROBLEM
7) WAP TO ADJACENCY MATRIX
REPRESENTATION OF GRAPH
8) WAP TO IMPLEMENT GRAPH
TRAVERSAL USING BFS
9) PROGRAM TO IMPLEMENT GRAPH
TRAVERSAL USING DFS
1.WRITE A PROGRAM TO DO LINEAR SEARCH
IN AN ARRAY.

CODE:-
#include <iostream>
using namespace std;
int linear_serch(int a[],int n,int x);
int main()
{
int len,pos,x,i;
int a[4]={2,4,6,5};
len=sizeof(a)/sizeof(a[0]);
cout<<"The Array is:";
for(i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
cout<<"\nEnter the number to search:";
cin>>x;
pos=linear_serch(a,len,x);
if(pos!=-1)
{
cout<<"Element is present at position "<<pos+1<<" from left.";
}
else
{
cout<<"Element is not present in the Array.";
}
return 0;
}
int linear_serch(int a[],int n,int x)
{
int i,y;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
y=i;

break;
}
else
{
y=-1;
}
}
return y;
}

OUTPUT:-
2.WRITE A PROGRAM TO DO BINARY
SEARCH IN AN ARRAY.

CODE:-
#include <iostream>
using namespace std;
int binary(int a[],int left,int right,int x);
int main()
{
int len,pos,x,i;
int a[6]={1,2,3,4,5,6};
len=sizeof(a)/sizeof(a[0]);
cout<<"The Array is:";
for(i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
cout<<"\nEnter the number to search:";
cin>>x;
pos=binary(a,0,len,x);
if(pos!=-1)
{
cout<<"Element is present at position "<<pos+1<<" from left.";
}
else
{
cout<<"Element is not present in the Array.";
}
return 0;
}
int binary(int a[],int left,int right,int x)
{
int mid;
if(left<=right)
{
int mid=(left+right)/2;
if(x==a[mid])
{
return mid;
}
else if(x>a[mid])
{
return binary(a,mid+1,right,x);
}
else if(x<a[mid])
{
return binary(a,left,mid-1,x);
}
}
return -1;
}

OUTPUT:-
3.WRITE A PROGRAM TO MERGE SORT AN
ARRAY.

CODE:-
#include <iostream>
using namespace std;
void merge_(int arr[],int l,int mid,int r)
{
int a1=mid-l+1,b1=r-mid;
int a[a1],b[b1],i,j,k;
for(i=0;i<a1;i++)
{
a[i]=arr[l+i];
}
for(i=0;i<b1;i++)
{
b[i]=arr[mid+1+i];
}
i=0;
j=0;
k=l;
while(i<a1 && j<b1)
{
if(a[i]<b[j])
{
arr[k]=a[i];
k++;
i++;
}
else
{

arr[k]=b[j];
k++;
j++;

}
}
while(i<a1)
{
arr[k]=a[i];
k++;
i++;
}
while(j<b1)
{
arr[k]=b[j];
k++;
j++;
}
}
void mergesort(int arr[],int l,int r)
{
if(l<r)
{
int mid=(l+r)/2;
mergesort(arr,l,mid);
mergesort(arr,mid+1,r);
merge_(arr,l,mid,r);
}
}
int main()
{
int i;
int a[]={9,7,5,3,1};
cout<<"The Array is:";
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
mergesort(a,0,4);
cout<<"\nThe Sorted Array is:";
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}

OUTPUT:-
4.WRITE A PROGRAM TO QUICK SORT AN
ARRAY.

CODE:-
#include <iostream>
using namespace std;
void swap_no(int arr[],int x,int y)
{
int z=arr[x];
arr[x]=arr[y];
arr[y]=z;
}
int separate(int arr[],int l,int r)
{
int pivot,i,j;
pivot=arr[r];
i=l-1;
for(j=l;j<r;j++)
{
if(arr[j]<pivot)
{
i++;
swap_no(arr,i,j);
}
}
swap_no(arr,i+1,r);
return i+1;
}
void quicksort(int arr[],int l,int r)
{
if(l<r)
{
int pivot;
pivot=separate(arr,l,r);
quicksort(arr,l,pivot-1);
quicksort(arr,pivot+1,r);
}
}
int main()
{
int i;
int a[]={10,30,20,50,40};
cout<<"The Array is:";
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
quicksort(a,0,4);
cout<<"\nThe Sorted Array is:";
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}

OUTPUT:-
5.WRITE A PROGRAM TO DO STRASSEN’S
MATRIX MULTIPLICATION.

CODE:-
#include<iostream>
using namespace std;
double a[4][4];
double b[4][4];

void insert(double x[4][4])


{
double val;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cin>>val;
x[i][j]=val;
}
}
}
double cal_11(double x[4][4])
{
return (x[1][1] * x[1][2])+ (x[1][2] * x[2][1]);
}
double cal_21(double x[4][4])
{
return (x[3][1] * x[4][2])+ (x[3][2] * x[4][1]);
}
double cal_12(double x[4][4])
{
return (x[1][3] * x[2][4])+ (x[1][4] * x[2][3]);
}
double cal_22(double x[4][4])
{
return (x[2][3] * x[1][4])+ (x[2][4] * x[1][3]);
}
int main() {
double a11,a12,a22,a21,b11,b12,b21,b22,a[4][4],b[4][4];
double p,q,r,s,t,u,v;
cout<<"\nEnter the 16 elements of the 1st matrix: \n";
insert(a);
cout<<"\nEnter the 16 elements of the 2nd matrix: \n";
insert(b);
a11=cal_11(a);
a12=cal_12(a);
a21=cal_21(a);
a22=cal_22(a);
b11=cal_11(b);
b12=cal_12(b);
b21=cal_21(b);
b22=cal_22(b);
p=(a11+a22)*(b11+b22);
q=(a21+a22)*b11;
r=a11*(b12-b22);
s=a22*(b21-b11);
t=(a11+a12)*b22;

u=(a11-a21)*(b11+b12);
v=(a12-a22)*(b21+b22);
cout<<"\nFinal matrix";
cout<<"\n"<<p+s-t+v<<" "<<r+t;
cout<<"\n"<<q+s<<" "<<p+r-q+u;
return 0;
}
OUTPUT:-
6.WRITE A PROGRAM FOR FRACTIONAL
KNAPSACK PROBLEM.

CODE:-
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int value;
int weight;
float density;
}Item;

void input(Item items[],int sizeOfItems){


cout << "Enter the "<< sizeOfItems <<" item's values and
weights:"<< endl;
for(int i=0; i<sizeOfItems; i++){
cout << "Enter Value for Item number "<<i+1<<":";
cin >> items[i].value;
cout << "Enter Weight for Item number "<<i+1<<":";
cin >> items[i].weight;
cout<<endl;
}
}
void display(Item items[],int sizeOfItems){
cout << "(Value,Weight) format: ";
for(int i=0; i<sizeOfItems; i++){
cout <<"{"<< items[i].value <<","<<items[i].weight<<"}"<< " ";
}
cout << endl;
}
bool compare(Item i1, Item i2)
{
return (i1.density > i2.density);
}
float knapsack(Item items[],int sizeOfItems, int W){
float totalValue=0, totalWeight=0;

for(int i=0; i<sizeOfItems; i++){


items[i].density = items[i].value/items[i].weight;
}
sort(items, items+sizeOfItems,compare);
for(int i=0; i<sizeOfItems; i++){
if(totalWeight + items[i].weight<= W)
{
totalValue += items[i].value ;
totalWeight += items[i].weight;
}
else
{
int wt = W-totalWeight;
totalValue += (wt * items[i].density);
totalWeight += wt;
break;
}

}
return totalValue;
}
int main()
{
int W;
Item items[5];

input(items,5);
cout << "Entered data in ";
display(items,5);
cout<< "Enter Knapsack weight:";
cin >> W;
float mxVal = knapsack(items,5,W);
cout << "Max value for "<< W <<" weight is:"<< mxVal;
return 0;
}

OUTPUT:-
7.WRITE A PROGRAM TO ADJACENCY
MATRIX REPRESENTATION OF GRAPH.

CODE:-
#include<iostream>
#define V 5
using namespace std;
void init(int arr[][V])
{
int i,j;
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
arr[i][j] = 0;
}
void addEdge(int arr[][V],int row, int col)
{
arr[row][col] = 1;
arr[col][row] = 1;
}
void printAdjMatrix(int arr[][V])
{
int i, j;
cout<<"Adjacency Matrix of the Undirected Graph is:"<<endl;
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
int main() {
int adjMatrix[V][V];
init(adjMatrix);
addEdge(adjMatrix,1,1);
addEdge(adjMatrix,1,2);
addEdge(adjMatrix,1,3);
addEdge(adjMatrix,2,1);
addEdge(adjMatrix,2,2);
addEdge(adjMatrix,2,3);
addEdge(adjMatrix,3,1);
addEdge(adjMatrix,3,2);
addEdge(adjMatrix,3,3);
printAdjMatrix(adjMatrix);
return 0;
}

OUTPUT:-
8.WRITE A PROGRAM TO IMPLEMENT
GRAPH TRAVERSAL USING BFS.

CODE:-
#include<iostream>
#include <list>
using namespace std;
class Graph
{
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}
void Graph::BFS(int s)
{
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty())
{
s = queue.front();
cout << s << " ";
queue.pop_front();
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
int main()
{
Graph g(5);
g.addEdge(1,1);
g.addEdge(1,2);
g.addEdge(1,3);
g.addEdge(2,1);
g.addEdge(2,2);
g.addEdge(2,3);
g.addEdge(3,1);
g.addEdge(3,2);
g.addEdge(3,3);
cout << "Breadth First Search Traversal (Starting from Vertex1):";

g.BFS(1);
return 0;
}
OUTPUT:-
9.WRITE A PROGRAM TO IMPLEMENT
GRAPH TRAVERSAL USING DFS.

CODE:-
#include<iostream>
#include <list>
using namespace std;
class Graph {
int V;
list<int>* adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}
void Graph::DFSUtil(int v, bool visited[])
{
visited[v] = true;
cout << v << " ";
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
void Graph::DFS(int v)
{
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
DFSUtil(v, visited);
}
int main()
{
Graph g(5);
g.addEdge(1,1);
g.addEdge(1,2);
g.addEdge(1,3);
g.addEdge(2,1);
g.addEdge(2,2);
g.addEdge(2,3);
g.addEdge(3,1);
g.addEdge(3,2);
g.addEdge(3,3);
cout << "Depth First Search Traversal (starting from vertex 1):";
g.DFS(1);
return 0;

}
OUTPUT:-

You might also like