Hey guys! I need help! I'm kinda new here so i'll cut the to the crap.
I need to make a FOR loop for an array of 5 elements. (array[5]). The loop should pick the positions [1] and [4] and removes the integers positioned in it and the integers next to it should fill in the space like this:
Elements in Array:
23 24 25 26 27
Elements after deletion:
23 25 26 0 0
I'm having a hard time making one :((
Advance thanks! :))
oh, and here's the code i tried to make (so far, i keep on failing):
int main()
{
int array[5] = {23,24,25,26,27};
del(array,4);
del(array,1);
}
void del (int arr[], int n)
{
int p = sizeof(arr)/sizeof(int); // element count
p -= 1; // index of last element
n -= 1; // index of item to delete
for (int j=n; j<p; j++)
arr[j] = arr[j+1];
arr[p] = 0;
}
By the way I also understood the task incorrectly. I thought that he need to remova elements with even values. If it is required to remove some element specified by its index then the function can look the following way
1 2 3 4 5 6 7 8
void del( int a[], int n, int pos )
{
if ( pos < n )
{
for ( int i = pos; ++i < n; pos = i ) a[pos] = a[i];
a[pos] = 0;
}
}
What's the pointer for "int pos"? I'm assuming that int a[] is the array and int n is the limit or the final element in the array. What's the initial value for pos?
Is this what you were trying to show me? Wouldn't that be an infinite loop?
1 2 3 4 5 6 7 8 9 10 11 12 13
void del( int a[], int n, int pos )
{
if ( pos < n )
{
for ( int i = pos; ++i < n; pos = i )
{
a[pos] = a[i];
a[pos] = 0;
}
}
}