0

Given an integer array, I'm trying to print the array with all the zeros moved to the left of the array. The order of the remaining numbers doesn't matter. I keep getting strange outputs like "{-1073741824,1049472,1,49,63,1055984,1}" for the array that is hardcoded in main.

int main(int argc, const char * argv[]) {
    int a[10] = {3, 0, 1, 4, 0, 0, 7, 20, 1, 5};
    int n = 10;
    int count = 0;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == 0)
        {
            ++count;
        }
    }

    //////////

    int *array = malloc(0);
    for (int j = 0; j < count; ++j)
    {
        array = realloc(array, (j + 1) * sizeof(int));
        array[j] = 0;
    }

    //////////

    printf("%s", "{");
    for (int k = 0; k < n-1; ++k)
    {
        if (array[k] != 0)
        {
            printf("%d%s", array[k], ",");
        }
    }
    printf("%d", array[n-1]);
    printf("%s", "}\n");

    //////////

    free(array);
    return 0;
}
5
  • So, you define a as an array. Then you define another array, named array. Then you use array. But where do you use or copy the values from a? Commented Feb 8, 2016 at 23:43
  • Why not use a simple sort algorithm? Commented Feb 8, 2016 at 23:43
  • malloc(0) is undefined behavior... Though you've certainly used it in an interesting way. :) I doubt it's the source of your problem. Take note that you may actually have a valid pointer returned from that of a minimum allocation size (8 or 16 bytes). It's also a super good idea to check that malloc and realloc return valid pointers any time they are called. Commented Feb 8, 2016 at 23:45
  • Thanks AndASM, I realized that I'm printing from the wrong array. I'll see if I can fix it! Commented Feb 8, 2016 at 23:49
  • I suggest you learn some basic debugging skills either with a source level debugger or with well-placed printf() statements. Commented Feb 8, 2016 at 23:59

2 Answers 2

2

You can replace:

int *array = malloc(0);
for (int j = 0; j < count; ++j)
{
    array = realloc(array, (j + 1) * sizeof(int));
    array[j] = 0;
}

With something like:

int array[10]; //malloc(0);
int j = 0;
for (j = 0; j < count; ++j)
{
    array[j] = 0;
}

for (j = 0; j < n; ++j)
{
    if(a[j]!=0)
        array[count++] = a[j];
}

If you use this code you don't need malloc, realloc neither free.

Sign up to request clarification or add additional context in comments.

Comments

0

The code looks overly complicated - you don't need to pass a string in using %s you can just print it directly. So

printf("%s", "{");

can be:

print("{");

After your first step, you could just print the number of zeros you've found, followed by stepping through and printing all the non-zero integers. Something a bit like this

int a[10] = {3, 0, 1, 4, 0, 0, 7, 20, 1, 5};
int n = 10;
int count = 0;
for (int i = 0; i < n; ++i)
{
    if (a[i] == 0)
    {
        ++count;
    }
}
printf("{");
for(int i=0; i < count; i++)
{
     printf("0,");
}
for(int i=0; i < n; i++)
{
     if (a[i]!=0) {
         printf("%d", a[i]);
         if (i < n-1) {
             printf(",");
         }
     }
}
printf("}\n");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.