Open im-sardarAmmarkhan opened 2 years ago
#include <iostream>
using namespace std;
// Function to insert an element at a specific position in an array
void insertElement(int arr[], int& n, int pos, int value)
{
// Shift all elements after the position to the right
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
// Insert the value at the specified position
arr[pos] = value;
// Increment the size of the array
n++;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int pos = 2; // position to insert the element
int value = 9; // value of the element to insert
cout << "Array before insertion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
insertElement(arr, n, pos, value);
cout << "\nArray after insertion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
#include <iostream>
using namespace std;
// Function to delete an element at a specific position in an array
void deleteElement(int arr[], int& n, int pos)
{
// Shift all elements after the position to the left
for (int i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
// Decrement the size of the array
n--;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int pos = 2; // position to delete the element from
cout << "Array before deletion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
deleteElement(arr, n, pos);
cout << "\nArray after deletion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
C++