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

Lab Task 4

This document contains the code for a C++ program that performs sorting, insertion, deletion, and searching operations on an integer array. The program sorts an initial array, inserts a new element at the end, deletes an element by index, and searches for a given element, printing the results of each operation. It uses standard algorithms and loops to implement the array manipulations and demonstrates basic data structure operations.

Uploaded by

Minahil
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)
14 views

Lab Task 4

This document contains the code for a C++ program that performs sorting, insertion, deletion, and searching operations on an integer array. The program sorts an initial array, inserts a new element at the end, deletes an element by index, and searches for a given element, printing the results of each operation. It uses standard algorithms and loops to implement the array manipulations and demonstrates basic data structure operations.

Uploaded by

Minahil
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/ 4

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;

You might also like