Lahore College For Women University LCWU
Name. Minahil Asif
Class. BSSE
Subject. Data structure and algorithm
Lab work 4
Output
Code:
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5, 2, 8, 1, 3};
int size = 5;
// Sorting the array
sort(arr, arr + size);
// Printing the sorted array
cout << "Sorted Array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
// Insertion (adding an element to the end)
int insertElement = 6;
arr[size] = insertElement;
size++;
// Printing the array after insertion
cout << "Array after Insertion: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
// Deletion (removing an element by index)
int deleteIndex = 2;
if (deleteIndex >= 0 && deleteIndex < size) {
for (int i = deleteIndex; i < size - 1; i++) {
arr[i] = arr[i + 1];
size--;
// Printing the array after deletion
cout << "Array after Deletion: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
// Search an element
int searchElement = 3;
bool found = false;
for (int i = 0; i < size; i++) {
if (arr[i] == searchElement) {
found = true;
break;
if (found) {
cout << searchElement << " found in the array." << endl;
} else {
scout << searchElement << " not found in the array." << endl;
}
return 0;