### Help me on FOR LOOPS, PLEASE! :(

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 :((

oh, and here's the code i tried to make (so far, i keep on failing):

 1234567891011121314151617181920 #include #include int main() { void del(int*, int); int array[5] = {23,24,25,26,27}; int z = 5; del(array, z); getch(); } void del(arr, p) { int a = 0; for(a = 0; a < p; a++) { if ((a %2) == 0) printf("%d/n", a); } }
Last edited on
 1234567891011 void del( int a[], int n ) { int j = 0; for ( int i = 0; i < n; i++ ) { if ( a[i] % 2 == 0 ) a[j++] = a[i]; } for ( ; j < n; j++ ) a[j] = 0; }
Last edited on
a couple of points here...

when you say "z=5" i presume you mean the fifth element (no pun intended) which is index 4

declaring your array as an int* is perfectly valid but you lose its array properties so cant calculate its length to know when you are at the end

 12345678910111213141516171819202122 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
 @Jaybob66

You are totally wrong.

z = 5; means in this case the size of the array. It is not the fourth element of the array.

This expression

int p = sizeof(arr)/sizeof(int); // element count

does not calculate the number of elements. It is equivalent to

int p = sizeof( int * ) / sizeof( int );

So if for example sizeof( int * ) is equal to sizeof( int ) you will get 1.

This loop

for (int j=n; j<p; j++)
arr[j] = arr[j+1];

is invalid because you are trying to access memory beyond the array that is you are trying to access element with index n.

The same is valid for this invalid code.

arr[p] = 0;

See my previous post where I showed the correct realization of the function.
Last edited on
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

 12345678 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; } }
Last edited on

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?

 12345678910111213 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; } } }
 @ven132 What's the pointer for "int pos"?

How are you going to say the function what element you want to remove?