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 :((
Advance thanks! :))

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#include<conio.h>
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
1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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;
}
@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


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;
   }
}
Last edited on
@vlad from moscow

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


How are you going to say the function what element you want to remove?
@vlad from moscow

Oh, nevermind. I just made it work lol.

All i did was:(yeah i know the code's really sloppy. I'll try better next time. THANKS ANYWAYS!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void del( int a[], int n, int pos )
{
int r = 0;
for(n = 0; n<pos; n++)
   {
     r = n;
      if(n==1 || n ==5)
      {
      while ( n<pos)
      {
         a[n] = a[n+1]
         n++;
      }
      n=r+1;
      }
     } 
Topic archived. No new replies allowed.