4678-Assignment-1
4678-Assignment-1
aSSignment # 1
data StructureS and algorithmS
Source code:
#include <iostream>
using namespace std;
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 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) {
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;
return 0; }