You are on page 1of 6

NATIONAL UNIVERSTY OF MODERN

LANGUAGES ISLAMABAD

DEPARTMENT OF SOFTWARE ENGINEERING

Data Structure and Algorithm

LAB REPORT# 5

NAME: M.Haseeb Qureshi

CLASS: BSSE31-3A

REG.NO: S22-14979

Roll no: SP-21287

INSTRUCTOR: Dr. Javeria Kanwal

Date: 14-03-2023
Question No.01
Binary Search using recursion:

Program:
#include<iostream>
using namespace std;
int arr[5]={1,2,3,4,5};
int BinarySearch(int item,int beg,int end)
{
if (beg>end)
{
return -1;
}
else
{
int mid=(beg+end)/2;
if (arr[mid]==item)
{
return mid;
}
if(arr[mid]<item)
{
BinarySearch( item, mid+1, end);
}
else if(arr[mid]>item)
{
BinarySearch(item, beg,mid-1);

}
}
}
main()
{
int beg=0;
int end=4;
int result;
int value;
cout<<"enter value to search\n";
cin>>value;
result=BinarySearch(value,beg, end);
if (result==-1)
{
cout<<"not found\n";
}
else {

cout<<"this value at index at :"<<result;


}
}

Output:
Question No.2
Stack:
#include<iostream>
using namespace std;
int stack[5];
int size=5;
int top=-1;
void push(int item)
{
if(top==size-1)
{
cout<<"stack is full\n";
}
else{

top++;
stack[top]=item;
}
}
int pop()
{
if (top==-1)
{
cout<<"stack is empty\n";
}
else
{
cout<<stack[top]<<endl;
top--;
}
}
main()
{
push(5);
push(4);
push(2);
push(8);
pop();
pop();
pop();
pop();
}
Output:

You might also like