Abdurahman Lab 2 Dsa
Abdurahman Lab 2 Dsa
Abdurahman Lab 2 Dsa
Write a C++ program to implement all the above-described algorithms and display
the following menu and ask the user for the desired operation.
CODE
#include <iostream>
using namespace std;
int getsize() {
int size;
cout << "Enter number of elements you want to insert: ";
cin >> size;
return size;
}
if (loc != -1)
cout << element << " found at position " << loc + 1 << endl;
else
cout << element << " not found in array.\n";
}
if (loc != -1) {
for (int i = loc; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
size--;
cout << element << " deleted from array.\n";
} else {
cout << element << " not found in array.\n";
}
}
int main() {
int element, pos, loc;
int size = getsize();
int nums[100]; // Static array with maximum size
getarray(nums, size);
int choice;
bool exit = false;
while (!exit) {
// Displaying the menu
cout << "\n\n********* MAIN MENU *********\n";
cout << "1 --- For Insert at start\n";
cout << "2 --- For Insert at end\n";
cout << "3 --- For Insert at specific position\n";
cout << "4 --- Linear Search\n";
cout << "5 --- For Deletion at specific position\n";
cout << "6 --- For Deletion of specific number\n";
cout << "7 --- Display array\n";
cout << "8 --- Exit\n";
cout << "PLEASE ENTER YOUR CHOICE :- ";
cin >> choice;
switch (choice) {
case 1:
startinsert(nums, size, element);
displayarray(nums, size);
break;
case 2:
endinsert(nums, size, element);
displayarray(nums, size);
break;
case 3:
custominsert(nums, size, element, pos);
displayarray(nums, size);
break;
case 4:
linearsearch(nums, size, element, loc);
break;
case 5:
delposition(nums, size, pos);
displayarray(nums, size);
break;
case 6:
delelement(nums, size, element, loc);
displayarray(nums, size);
break;
case 7:
displayarray(nums, size);
break;
case 8:
exit = true;
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
}
return 0;
}
Algorithm
4. Linear Search
Step 1: Begin
Step 2: Read the element to search
Step 3: Set found := false
Step 4: Set i := 0
Step 5: Repeat steps 6 to 7 while (i < size and found == false):
Step 6: If array[i] == element, set found = true and location = i
Step 7: Set i = i + 1
Step 8: If found = true, display the position of the element
Step 9: If found = false, display "element not found"
Step 10: End
Conclusion: