0% found this document useful (0 votes)
7 views6 pages

Array Operations in C++ Code

The document contains a C++ program that implements basic array operations including traversal, searching, insertion, and updating elements. It allows the user to interactively choose operations on an array of integers. The program continues to run until the user decides to exit.

Uploaded by

shehzaibmirza428
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)
7 views6 pages

Array Operations in C++ Code

The document contains a C++ program that implements basic array operations including traversal, searching, insertion, and updating elements. It allows the user to interactively choose operations on an array of integers. The program continues to run until the user decides to exit.

Uploaded by

shehzaibmirza428
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

PC

S2024332016

DSA Lab
#include <iostream>
using namespace std;

// Function to traverse and display the array elements


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

// Function to search for a specific element in the array


void search( int arr[] , int size) {
cout << endl;
cout << "Enter target = ";
int target ;
cin >> target ;

int condition = 0;
for (int i = 0 ; i < size ; i++){
if (arr[i]== target ){
cout << "Target found at " << i << " Index";
condition = 1 ;
break;
}

}
if (condition == 0){
cout << "Target Not Found " ;
}
}

// Function to insert a new element at a specified position


void insert( int arr [] , int size) {

cout << "Current array: ";


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

int insertions;
cout << "How many values do you want to insert? ";
cin >> insertions;

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


int value, position;

cout << "Enter the value to insert: ";


cin >> value;

cout << "Enter the position to insert (0 to " << size << "): ";
cin >> position;

for (int j = size; j > position; j--) {


arr[j] = arr[j - 1];
}

arr[position] = value;
size++;
}

cout << "Updated array: ";


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

// Function to update the value of an existing element


void update(int arr[] , int size) {

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


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

cout << "\nUpdating the array \n" ;


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

}
}

int main() {
int size = 6 ;
int arr[] = {10, 20 ,30 , 40 ,50 ,60};
int choice;

do {
cout << "\nOperations:\n";
cout << "1. Traverse\n";
cout << "2. Search\n";
cout << "3. Insert\n";
cout << "4. Update\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
traverse( arr , size );
break;
case 2:
search( arr , size );
break;
case 3:
insert(arr , size);
break;
case 4:
update( arr , size );
break;
case 5:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 6);
return 0;
}

Outputs:

You might also like