0

How can I delete every nth element of an array?
For exp: i have and array a[]={2,12,4,5,67,33,24,45,6,1};
and i want to delete every 3rd element so that resultant array is a[]={2,12,5,67,24,45,1};
I know how to delete one element at one place but i can't find a way to delete the element on every 3rd place.
So i tried something like this:

int n,i;
double *a;  
printf("Enter number of elements: ");
scanf_s("%d",&n);

a = (double*)malloc(100*sizeof(n));
printf("Enter the elements: ");
for( i=0;i<n;i++) {
    scanf_s("%d",&a[i]);

}

for( i=0;i<n;i++) {
    printf("%d ",a[i]);

}

for(i=0;i<n;i++){
    for ( i = 3 - 1 ; i < n - 1 ; i++ ){
         a[i] = a[i+1];
    }
}

  printf("\nResultant array is\n");

  for( i = 0 ; i < n - 1 ; i++ )
     printf("%d\n", a[i]);

and there isn't any particular way that i need to do this, it can be in the same array or in a new array.

whoidn
  • 21
  • 1
  • 3
  • 1
    Show us your code please :-) – moffeltje Jul 02 '15 at 12:18
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](//stackoverflow.com/questions/how-to-ask) – Eregrith Jul 02 '15 at 12:18
  • Welcome to stack overflow. Please show us what you've tried so far. (You may also want to mention if you need to do this in place or if copying to a new array is acceptable) – Foon Jul 02 '15 at 12:19
  • 1
    You cant not delete a array element ,you can replace it with another value keeping last elements of array unused. OR you can create new array of desired size. – Vagish Jul 02 '15 at 12:22
  • @moffeltje there, i edited my post :) – whoidn Jul 02 '15 at 12:31

5 Answers5

5

No, I won't write the code for you, but can surely help you with some pointers.

First, if you try to modify the same array, it will be a big task. After every delete (of value, not the element itself, to be pedantic), you need to shift every remaining element. Rather I propose,

  1. Create another array.
  2. using a loop, start copying element by element from the source array.
  3. Check the occurrence of every nth index, skip storing that index value.
  4. Once finished, you get the modified array.

TL;DR You need a loop to get your job done.

[END of original answer]


EDIT:

Your code snippet is wrong for many reasons, like

  1. a being a pointer to double, statement like

    a = (double*)malloc(100*sizeof(n));
    

    is wrong, because
    1.1. sizeof(n) gies you sizeof(int) but actually it should be sizeof(double)
    1.2. see why not to cast the return value of malloc() and family in C.

    It should be written as

    a = malloc(100*sizeof*a);
    
  2. Asking user for the size and taking input in a fixed size variable is very dangerous. If the user input a size more than that of the allocation, your program will blow up.

  3. Using same identifier for nested loops are likely to be erroneous, just as in your case.

Finally, IMHO, only use dynamic memory when the allocation size is not known at compile time. If you're going to allocate dynamic memory in a fixed way (like in above code), it's effectively of no use.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @Bathsheba How exactly sir? are you referring to swapping? – Sourav Ghosh Jul 02 '15 at 12:23
  • @SouravGhosh i have a problem because the array starts with 0 , i tried to do an if statement , like if counter divided by 3 is 0 but if i start from 0 than the counter divided by 3 is on 4th position – whoidn Jul 02 '15 at 12:37
  • 1
    Why the downvote? This approach (though not as pretty as mine) is perfectly adequate. – Bathsheba Jul 02 '15 at 13:35
  • @Bathsheba I also don't get the logic . If anyone found anything wrong, thanks, but please let me know, I'm (or anyone, for that matter) is not a mind-reader. :-( – Sourav Ghosh Jul 02 '15 at 13:54
2

This is one way:

  for (
      int i = 0, j = 0; 
      i + j + 1 < sizeof(a) / sizeof(a[0]); 
      j += (i + j) % 3 ? 0 : 1, a[i] = a[i + j], ++i
   );

It does it in one traversal and does not adjust the end of the array. Since the zeroth element is always removed, i + j + 1 is a good stopping condition.

sizeof(a) / sizeof(a[0]) gives you the number of elements in an array with at least one element.

The iteration expression is well-defined since , is sequenced and evaluated from left to right.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

I believe that you can traverse the array from one end (let's just say index 0 to be consistent).

  1. Initialize a counter to 1, but let it refer to index 0 of the array (this way you won't have to divide by 0).

  2. Loop through the array and if((index + 1) % 3 == 0) delete that index

  3. On each iteration of the loop, you set (index - 1) to index if the index is not a element in a 3rd place (I'm assuming you mean multiples of 3. i.e 3, 6, 9, 12...)

  4. If it's the last element of the array, set it to NULL, that way you wont have a duplicate at the end of the array.

This way you don't have to create an extra array and you can delete every third element.

Jon
  • 150
  • 1
  • 3
  • 15
0

Here is a demonstrative program that shows how the corresponding function can be written

#include <stdio.h>

size_t remove_nth( int *a, size_t size, size_t n )
{
    int *p = a;

    if ( n != 0 )
    {
        size_t i = 0;

        while ( i < size && ( i + 1 ) % n != 0 ) i++;

        p = a + i;

        for ( ; i < size; i++ )
        {
            if ( ( i + 1 ) % n != 0 ) *p++ = a[i]; 
        }
    }

    return n == 0 ? size : p - a;
}

int main( void ) 
{
    int a[] = { 2, 12, 4, 5, 67, 33, 24, 45, 6, 1 }; 

    size_t n = remove_nth( a , sizeof( a ) / sizeof( *a ), 3 );

    for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
}

The program output is

2 12 5 67 24 45 1 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

First of all your malloc statement is wrong. Change it to

a = (double *) malloc(n * sizeof(double));

Now simply constructing a new array

b = (double *) malloc(n * sizeof(double));
j = 0;
for (i = 0; i < n; ++i)
    if ((i + 1) % 3 != 0)
        b[j++] = a[i];

j will have the length of array b after the loop finishes.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50