Skip to content
Snippets Groups Projects

array_remove

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Jan De Rudder

    Removing from array by shifting to the left the remaining part after deletion of n elements.

    Example:

    remove 2 elements from index 1 array_remove([0,1,2,3], 1, 2) -> [0,3]

    Edited
    array-remove.c 2.61 KiB
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    /******************************************************************************
     * utilities
    *******************************************************************************/
    void array_output(const int* array, const size_t size)
    {
        for (size_t i=0; i<size; ++i) {
            printf("%d ", array[i]);
        }
        printf("\n");
    }
    
    
    
    int* make_array(const size_t size)
    {
        int* array = calloc(size, sizeof(int));
        int initValue = 0;
        for (size_t i=0; i<size; ++i) {
            array[i] = initValue++;
        }
        return array;
    }
    
    
    
    /******************************************************************************
     * core function
    *******************************************************************************/
    void array_remove(
        int*   array,
        size_t arraySize,
        size_t from,
        size_t count
    ){
    
        memmove(&array[from], &array[from+count], (arraySize-count)*sizeof(int));
    }
    
    
    
    /******************************************************************************
     * sample program
    *******************************************************************************/
    int main(void)
    {
        const size_t arrSize = 6;
    
        int* array = make_array(arrSize);
    
        // experience 1: remove(0,1) continually
        printf("array          : ");
        array_output(array, arrSize);
    
        for (size_t i=0; i<arrSize; ++i) {
            printf("remove(0,1)    : ");
            array_remove(array, arrSize-i, 0, 1);
            array_output(array, arrSize-i-1);
        }
        printf("\n");
    
    
        // experience 2: remove(2,2)
        free(array);
        array = make_array(arrSize);
    
        printf("array          : ");
        array_output(array, arrSize);
    
        array_remove(array, arrSize, 2, 2);
    
        printf("remove(2,2)    : ");
        array_output(array, arrSize-2);
        printf("\n");
    
    
        // experience 3: remove(last,1) continually
        free(array);
        array = make_array(arrSize);
    
        printf("array          : ");
        array_output(array, arrSize);
    
        for (size_t i=0; i<arrSize; ++i) {
            printf("remove(%lu,1)    : ", arrSize-i-1);
            array_remove(array, arrSize-i, arrSize-i-1, 1);
            array_output(array, arrSize-i-1);
        }
    
        return EXIT_SUCCESS;
    }
    
    /*
    program output
    -----------------------------------------
    array          : 0 1 2 3 4 5
    remove(0,1)    : 1 2 3 4 5
    remove(0,1)    : 2 3 4 5
    remove(0,1)    : 3 4 5
    remove(0,1)    : 4 5
    remove(0,1)    : 5
    remove(0,1)    :
    
    array          : 0 1 2 3 4 5
    remove(2,2)    : 0 1 4 5
    
    array          : 0 1 2 3 4 5
    remove(5,1)    : 0 1 2 3 4
    remove(4,1)    : 0 1 2 3
    remove(3,1)    : 0 1 2
    remove(2,1)    : 0 1
    remove(1,1)    : 0
    remove(0,1)    :
    -----------------------------------------
    */
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment