0% found this document useful (0 votes)
3 views5 pages

Task 2

The document is a C++ program that demonstrates array operations including initialization, accessing, updating, inserting, deleting, and searching for elements. It showcases how to manipulate an array by inserting and deleting values at both the beginning and end, as well as performing a linear search for a specific value. The program outputs the array at various stages to illustrate the changes made.

Uploaded by

musawarabbas716
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)
3 views5 pages

Task 2

The document is a C++ program that demonstrates array operations including initialization, accessing, updating, inserting, deleting, and searching for elements. It showcases how to manipulate an array by inserting and deleting values at both the beginning and end, as well as performing a linear search for a specific value. The program outputs the array at various stages to illustrate the changes made.

Uploaded by

musawarabbas716
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

#include <iostream>

using namespace std;


int main() {
const int a = 10;
int arr[a]={10,20,30,40,50};
int size = 5;

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


cout << arr[i] << " ";
}
cout << endl;

// Accessing elements
cout << "Value of index 2: " << arr[2] << endl;

// Updating elements
arr[2] = 99;
cout << "After updating index: ";
for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;

// 4. Insertion at end
arr[size] = 60;
size++;

cout << "After insert value: ";


for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;

// Insertion at beginning
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = 5;
size++;

cout << "After insert value at first: ";


for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;

// Delete last value


if (size > 0) {
size--;
}

cout << "After delete last value: ";


for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;

// Delete first value


for (int i = 0; i < size ; i++) {
arr[i] = arr[i + 1]; // shift left
}
size--;
cout << "After delete first value: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;

// Linear search
int val = 40;
int index ;
for (int i = 0; i < size; i++) {
if (arr[i] == val) {
index = i;
break;
}
}
if (index > 0){
cout << "Found " << val << " at index " << index << endl;
}
else{
cout << val << " not found" << endl;
}
// Final display
cout << "Final array: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}

You might also like