SRI RAM ENGINEERING COLLEGE
Unit 3
Various types of operators in array
Student
[Pick the date]
[Type the abstract of the document here. The abstract is typically a short summary of the contents of
the document. Type the abstract of the document here. The abstract is typically a short summary of the
contents of the document.]
C++ program to implement various
operations on arrays such as insertion,
deletion, searching, and displaying
elements.
#include <iostream>
using namespace std;
int main() {
int arr[100], n, choice, pos, value, i;
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter array elements:\n";
for(i = 0; i < n; i++) {
cin >> arr[i];
do {
cout << "\n--- Array Operations Menu ---\n";
cout << "1. Display Array\n";
cout << "2. Insert Element\n";
cout << "3. Delete Element\n";
cout << "4. Search Element\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: // Display
cout << "Array elements are: ";
for(i = 0; i < n; i++) {
cout << arr[i] << " ";
cout << endl;
break;
case 2: // Insert
cout << "Enter position to insert: ";
cin >> pos;
cout << "Enter value to insert: ";
cin >> value;
for(i = n; i >= pos; i--) {
arr[i] = arr[i-1];
arr[pos-1] = value;
n++;
cout << "Element inserted successfully.\n";
break;
case 3: // Delete
cout << "Enter position to delete: ";
cin >> pos;
for(i = pos-1; i < n-1; i++) {
arr[i] = arr[i+1];
}
n--;
cout << "Element deleted successfully.\n";
break;
case 4: // Search
cout << "Enter element to search: ";
cin >> value;
for(i = 0; i < n; i++) {
if(arr[i] == value) {
cout << "Element found at position " << i+1 << endl;
break;
if(i == n)
cout << "Element not found\n";
break;
case 5:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice!\n";
} while(choice != 5);
return 0;
Output:
Enter number of elements: 5
Enter array elements:
10 20 30 40 50
--- Array Operations Menu ---
1. Display Array
2. Insert Element
3. Delete Element
4. Search Element
5. Exit
Enter your choice: 1
Array elements are: 10 20 30 40 50
--- Array Operations Menu ---
Enter your choice: 2
Enter position to insert: 3
Enter value to insert: 25
Element inserted successfully
--- Array Operations Menu ---
Enter your choice: 1
Array elements are: 10 20 25 30 40 50
--- Array Operations Menu ---
Enter your choice: 3
Enter position to delete: 2
Element deleted successfully
--- Array Operations Menu ---
Enter your choice: 1
Array elements are: 10 25 30 40 50
--- Array Operations Menu ---
Enter your choice: 4
Enter element to search: 30
Element found at position 3
--- Array Operations Menu ---
Enter your choice: 5
Exiting program
[Link] Elements of an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
cout << "Array elements are:\n";
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
Array elements are:
10 20 30 40 50
2. Insertion in an Array
#include <iostream>
using namespace std;
int main() {
int arr[100] = {10, 20, 30, 40};
int n = 4, pos = 3, value = 25;
for(int i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = value;
n++;
cout << "Array after insertion:\n";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
Array after insertion:
10 20 25 30 40
3. Deletion from an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int n = 5, pos = 2;
for(int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
cout << "Array after deletion:\n";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
Array after deletion:
10 30 40 50
4. Searching an Element in an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int key = 30;
int found = 0;
for(int i = 0; i < 5; i++) {
if(arr[i] == key) {
cout << "Element found at position " << i + 1;
found = 1;
break;
}
}
if(found == 0) {
cout << "Element not found";
}
return 0;
}
Output:
Element found at position 3
5. Finding Largest Element in an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {12, 45, 23, 67, 34};
int max = arr[0];
for(int i = 1; i < 5; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
cout << "Largest element is: " << max;
return 0;
}
Output:
Largest element is: 67