in this C++ function to erase element from array, after erasing it I am getting a garbage value. why is that? and how show resolve it?
void del(int arrDel[], int size)
{
int temp, pos;
cout<<"old array is : "<<endl;
for (int i = 0; i < size; i++)
{
cout<<arrDel[i]<<" ";
}
cout << "\nenter the position of element to delete : ";
cin >> pos;
--pos;
temp = arrDel[pos];
for (int i = pos; i < size; i++)
{
arrDel[i] = arrDel[i + 1];
}
cout<<"element successfully deleted from the array\n";
cout<<"new array is : "<<endl;
for (int i = 0; i < size; i++)
{
cout<<arrDel[i]<<" ";
}
}
OUTPUT ->
old array is : 1 2 3 4 5 enter the position of element to delete : 2 element successfully deleted from the array new array is : 1 3 4 5 6422224
Here I am passing a reference to the array as well as the size of the array in arguments. I am unable to understand why I am getting this 6422224.