0% found this document useful (0 votes)
18 views3 pages

Danyal Nasir 5112124018

The document describes a C++ program that performs sequential and binary searches on an array of 13 elements. The program prompts the user to enter elements to search for, then uses both sequential and binary search algorithms on the array to find the positions of the elements.

Uploaded by

danyalkhattak739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Danyal Nasir 5112124018

The document describes a C++ program that performs sequential and binary searches on an array of 13 elements. The program prompts the user to enter elements to search for, then uses both sequential and binary search algorithms on the array to find the positions of the elements.

Uploaded by

danyalkhattak739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Danyal Nasir 5112124018

Write a C++ program to perform sequential and binary searches on array of


13 elements.

CODE:

#include <iostream>
using namespace std;
int main() {
int arr[13] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26};
int target;
cout << "Enter the element to search sequentially: ";
cin >> target;
bool foundSequentially = false;
for (int i = 0; i < 13; ++i) {
if (arr[i] == target) {
foundSequentially = true;
cout << "Element found at position " << i << " (Sequential Search)" << endl;
break;
}
}
if (!foundSequentially)
cout << "Element not found (Sequential Search)" << endl;
cout << "Enter the element to search using binary search: ";
cin >> target;
int start = 0, end = 12;
bool foundBinary = false;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) {
foundBinary = true;
cout << "Element found at position " << mid << " (Binary Search)" << endl;
break;
} else if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
if (!foundBinary)
cout << "Element not found (Binary Search)" << endl;

return 0;
}

OUTPUT :

You might also like