I have to define a function to delete an element in an array, here is the code
void delete_element(int a[], int n, int pos)
{
if (pos>=n)
pos=n-1;
else if (pos<0)
pos=0;
for (int i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
--n;
}
and here is an example:
int n;
printf("Enter the length of the array: ");
scanf("%d", &n);
int A[n]
for (int i=0;i<n;i++)
scanf("%d", &A[i]);
delete_element(A,n,2);
suppose that n=5 and A = {1,2,3,4,5}, after running the above code, it will print out {1,3,4,5,5}
When I use int n as parameter, the function deletes the element I want but the last element will appear twice in the array. I searched and then found out that by using int &n, the problem will be solved but I don't understand the reasons here. I would really appreciate if you could help me with this!