#include <conio.h>
#include <iostream>
using namespace std;
void insert_element(int* array, int arraySize, int pos, int element)
{
if (pos < 0 || pos >= arraySize)
{
cout << "Invalid insertion position: " << pos << endl;
return;
}
/* Shift array elements right starting from the $pos index */
for (int k = arraySize - 1; k > pos; k--)
array[k] = array[k - 1];
array[pos] = element;
}
void print_array(int* array, int arraySize)
{
for (int c = 0; c < arraySize; c++)
cout << " " << array[c];
cout << endl;
}
int main()
{
int array[5] = {3, 17, 24, 6, 0};
cout << "The inial array:" << endl;
print_array(array, 5);
cout << endl;
insert_element(array, 5, 3, -8);
cout << "The -8 element has been inserted into the position 3:" << endl;
print_array(array, 5);
_getch();
return 0;
}
Comments
Leave a comment