0% found this document useful (0 votes)
6 views

4678-Assignment-1

The document contains a C++ program that implements an ArrayHandler class for managing an array, allowing operations such as inputting values, displaying the array, inserting and deleting elements, sorting the array using bubble sort, and searching for elements using linear and binary search. It includes a main function that provides a menu for users to interact with the array functionalities. The program is designed for educational purposes in a data structures and algorithms course.

Uploaded by

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

4678-Assignment-1

The document contains a C++ program that implements an ArrayHandler class for managing an array, allowing operations such as inputting values, displaying the array, inserting and deleting elements, sorting the array using bubble sort, and searching for elements using linear and binary search. It includes a main function that provides a menu for users to interact with the array functionalities. The program is designed for educational purposes in a data structures and algorithms course.

Uploaded by

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

Submitted by: noor Fatima

Submitted to: dr. nadeem


reg no. 4678-Foc/bScS/F22

aSSignment # 1
data StructureS and algorithmS
Source code:
#include <iostream>
using namespace std;

#define MAX_SIZE 100

class ArrayHandler {
private: int
arr[MAX_SIZE]; int
size;
public:
ArrayHandler() {
size = 0;
}
void inputInitialValues() {
cout << "Enter the number of elements: ";
cin >> size;
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) { cin
>> arr[i];
}
}
void displayArray() {
cout << "Elements in the array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
void insertElement(int element, int position) {
if (size >= MAX_SIZE) {
cout << "Array is full. Cannot insert more elements." << endl;
return;
}
if (position < 1 || position > size + 1) {
cout << "Invalid position. Please provide a position between 1 and " <<
size + 1 << endl;
return;
}
if (position <= size) {
for (int i = size - 1; i >= position - 1; i--) {
arr[i + 1] = arr[i];
}
arr[position - 1] = element;
cout << "Inserted successfully" << endl;
size++; } else {
arr[size++] = element;
cout << "Inserted successfully" << endl;

}
}

void deleteElement(int delinfo) {


bool found = false; for (int i =
0; i < size; i++) { if
(arr[i] == delinfo) {
found = true; if (i ==
(size - 1)) { size--
; } else
{
for (int j = i; j < (size - 1); j++) {
arr[j] = arr[j + 1];
}
size--; }
cout << "Element deleted successfully!" << endl;
break;
}
} if (!found)
{
cout << "The element does not exist!" << endl;
}
}

void bubbleSort() {
int counter = 1; while
(counter < size) {
for (int i = 0; i < size - counter; i++) {
if (arr[i] > arr[i + 1]) { int
temp = arr[i]; arr[i] = arr[i +
1]; arr[i + 1] = temp;
}
}
counter++;
}
cout << "The sorted array is ";
for (int i = 0; i < size; i++) {
cout << arr[i]<<" ";
}
cout << endl << endl;
}
int linearSearch(int key) {

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


if (arr[i] == key) {
return i;
}
} return -
1;
}

int binarySearch(int key) {


bubbleSort(); int start
= 0; int end = size -
1; int mid;

while (start <= end) {


mid = start + (end - start) / 2;
if (arr[mid] == key) {
return mid;
}
else if (arr[mid] < key) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
};
int main() {
ArrayHandler arrHandler;
int choice, element, position, delinfo, searchKey, index;

arrHandler.inputInitialValues();
do { cout << "\nMenu:\n1. Display Array\n2. Insert Element\n3. Delete
Element\n4. Bubble Sort\n5. Linear Search\n6. Binary Search\n7. Exit\nEnter your
choice: "; cin >> choice;

switch (choice) { case


1:
arrHandler.displayArray();
break; case 2:
cout << "Enter the element to insert: ";
cin >> element;
cout << "Enter the position to insert: ";
cin >> position;
arrHandler.insertElement(element, position);
break; case 3:
cout << "Enter the element to delete: ";
cin >> delinfo;
arrHandler.deleteElement(delinfo);
break; case 4:
arrHandler.bubbleSort(); break;
case 5:
cout << "Enter the key to search: ";
cin >> searchKey;
index = arrHandler.linearSearch(searchKey);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found in the array." << endl;
break; case 6: cout << "Enter the key to
search: "; cin >> searchKey;
index = arrHandler.binarySearch(searchKey);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found in the array." << endl;
break; case 7: cout << "Exiting program.\n";
break; default: cout << "Invalid choice.
Please try again.\n";
}
} while (choice != 7);

return 0; }

You might also like