[Solved] Remove empty content in array and sorted the array in C


So, you don’t want to sort your array after all ?

Well, this is the code to “remove” all the zero in an array.
Since you changed many time your question, I don’t know if it will be the rigth answer.

If you have question, do not hesitate.
The algorithm used below is a commom algorithm : recopy the array with an offset.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void Array_Display(const int * const array, const size_t size)
{
    for (size_t i = 0; i < size; ++i) {
        printf("%d", array[i]);
        if (i + 1 < size) {
            printf(", ");
        }
    }
    printf("\n");
}

void Array_MagicFunction(int * const array, size_t *size)
{
    size_t j = 0;

    for (size_t i = 0; i < *size; ++i) {
        if (array[i] != 0) {
            array[j] = array[i];
            ++j;
        }
    }
    *size = j;
}

#define ARRAY_SIZE 10

int main(void)
{
    int    array[ARRAY_SIZE] = {1,2,513,0,4,5,6,0,0,9};
    size_t size              = ARRAY_SIZE;


    Array_Display(array, size);

    Array_MagicFunction(array, &size);

    Array_Display(array, size);

    return (EXIT_SUCCESS);
}

I keep my first answer in case you still want to sort your array.

———-

Edit : Attention please.
The provided code below doesn’t work for Jackdon for some mysterious reasons.
It seem that his implementation of qsort is bugged (which is really surprising consedering a bug in a massive used function is higlhy improbable) : even when the function return the correct value, the array is not sorted …

I don’t understand what’s going on, I’m pretty sure that my code is correct (and it work on Debian9), so I just put some poor “debugging”.
Jackdon environment is the following :

  • Windows 10
  • Microsoft Visual Studio 2017 Community
  • the compile setting is follow default

Tom’s To Jackdon :

Could you please replace the intcmp function with this ?

int intcmp(const void *first, const void *second)
{
    const int *firstInt = first;
    const int *secondInt = second;
    int       result;

    if (*firstInt == 0) {
        result = 1;
    } else {
        result = (*firstInt) - (*secondInt);
    }

    printf("POOR DEBUG : ");
    for (size_t i = 0 ; i < ARRAY_SIZE; ++i) {
        printf("%d ", array[i]);
    }
    printf("\ncomparing %d to %d : %d\n", *firstInt, *secondInt, result);

    return (result);
}

Jackdon To Tom’s

POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 1 to 5 : -4
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 1 to 9 : -8
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 5 to 9 : -4
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 2 to 5 : -3
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 3 to 5 : -2
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 0 to 5 : 1
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 0 to 5 : 1
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 0 to 5 : 1
POOR DEBUG : 1 2 3 0 4 5 6 0 0 9
comparing 6 to 5 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 4 to 5 : -1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 5 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 4 to 5 : -1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 4 to 5 : -1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 6 to 0 : 6
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 6 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 0 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 9 to 0 : 9
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 6 to 0 : 6
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 6 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 0 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 6 to 0 : 6
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 0 to 6 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 6 to 0 : 6
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 2 to 1 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 3 to 2 : 1
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 5 to 3 : 2
POOR DEBUG : 1 2 3 5 4 0 6 0 0 9
comparing 4 to 5 : -1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 2 to 1 : 1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 3 to 2 : 1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 4 to 3 : 1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 2 to 1 : 1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 3 to 2 : 1
POOR DEBUG : 1 2 3 4 5 0 6 0 0 9
comparing 2 to 1 : 1
1 2 3 4 5 0 6 0 0 9

———-

Use qsort.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int intcmp(const void *first, const void *second)
{
    const int *firstInt = first;
    const int *secondInt = second;

    if (*firstInt == 0) {
        return (1);
    }
    return (*firstInt - *secondInt);
}

#define ARRAY_SIZE 10

int main(void)
{
    int array[ARRAY_SIZE] = {1,2,3,0,4,5,6,0,0,9};

    qsort(array, ARRAY_SIZE, sizeof(*array), intcmp);

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

    return (EXIT_SUCCESS);
}

The “removing zero” part is handled by putting all zero at the end of the array. So your final array length will simply be found by searching the index of the first zero in the array (don’t forget to take care if there is no 0 is the array to begin with).

8

solved Remove empty content in array and sorted the array in C