DAA Lab
DAA Lab
LABORATORY
PRACTICAL FILE
(LPCCS-108)
BACHELOR OF TECHNOLOGY
D3 CSE C-1
CRN: 2215184
URN: 2203575
Ludhiana,141006
INDEX
S.No. Date Name of the Practical Page Remarks
No.
PRACTICAL-1
Title : Write a program to find out a roll number from college database using
binary search algorithm.
Code:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int rollNumber) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == rollNumber)
return mid;
if (arr[mid] < rollNumber)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main() {
int n;
cout<<"Name: Vansh Aggarwal"<<endl;
cout<<"Class:
= D-3 CSE C-1"<<endl;
cout<<"CRN:
= 2215184"<<endl;
cout<<"URN:
= 2203575"<<endl;
cout <<"Enter the number of roll numbers: ";
cin >> n;
int* arr = new int[n];
cout <<"Enter the roll numbers in ascending order:\n";
for (int i = 0; i < n; ++i) {
cout << "Roll number " << i + 1 << ": ";
cin >> arr[i];
}
int rollNumber;
cout <<"Enter roll number to search: ";
cin >> rollNumber;
int result = binarySearch(arr, n, rollNumber);
if (result != -1) {
cout <<"Roll number " << rollNumber << " found at index " << result << "." << endl;
} else {
cout <<"Roll number " << rollNumber << " not found in the database." << endl;
}
delete[] arr;
return 0;
}
Output: