Untitled
unknown
plain_text
a year ago
1.4 kB
8
Indexable
#include <iostream>
using namespace std;
void insert (int arr[] , int& n, int capacity , int pos, int value)
{
if (n >= capacity)
{
cout << "Array is full. Cannot insert!" << endl;
return;
}
for (int i = n; i > pos; i--)
arr[i] = arr[i-1];
arr[pos] = value;
n++;
}
void remove (int arr[], int& n, int pos)
{
if (pos < 0 || pos >= n)
{
cout << "Invalid position!" << endl;
return;
}
for (int i = pos; i < n -1; i++)
arr[i] = arr[i+1];
n--;
}
int search(int arr[], int n, int value)
{
for (int i = 0; i < n; i++)
if (arr[i] == value)
return -1;
}
int main()
{
const int capacity = 10;
int n = 5;
int arr[capacity] = {10, 20 , 30 , 40 , 50};
cout << "Original Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
insert (arr, n, capacity, 4, 49);
cout << "After Insertion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
remove(arr, n, 1);
cout << "After Deletion: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
int pos = search(arr , n , 20);
if (pos != -1)
{
cout << "Search 20: Found at index" << pos << endl;
} else {
cout << "Search 20: Not Found" << endl;
}
return 0;
}Editor is loading...
Leave a Comment