# Thread: Move 0s at top of each column in a MxN array

1. ## Move 0s at top of each column in a MxN array

Hello,

I have a matrix of NxM (the dimensions are set by the user) and I want a function to move 0s at the top of each column, and leave the rest integers as they are. Look at the example:

After the movement of 0s I want to have:

0 0 0 2 1
1 0 3 4 1
3 0 3 3 1
3 1 2 1 4
4 4 5 3 5

Someone please correct my zerototopMatrix function below:

Code:
```#include <stdio.h> #include <stdlib.h>
#include <time.h>

void printMatrix(int (*arr), int rows, int cols,int level)
{
int i, j;

printf("\n");
printf("\n");

printf("The matrix elements are:\n   ");
for (i = 0; i < cols+level; i++) {
printf("%d ", i+1);
}
printf("\n");
for (i = 0; i < (cols*2+1+level); i++) {
printf("-");
}
printf("-\n");
for (i = 0; i < rows+level; i++) {
printf("%d| ", i+1);
for (j = 0; j < cols+level; j++) {

printf("%d ", *(arr + i*cols + j));
}
printf("\n");

}
}

void zerototopMatrix(int (*arr), int rows, int cols,int level)
{
int i, j;

printf("\n");
printf("\n");

int count = 0,temp = 0;

for (j = 0; j < cols+level; j++) {
for (i = 0; i < rows+level; i++) {

if (*(arr + i*cols + j)==0)

{

(*(arr + i*cols + j)= (*(arr + count*cols + j)));
(*(arr + count*cols + j))=0;
count++;
}

}

}

}

int main() {

int number;

srand ( time(NULL) );
int level = 0;

int row,col,colors;
int points = 0;

printf("How many rows:\n");
scanf("%d", &row);

printf("How many columns:\n");
scanf("%d", &col);

printf("How many colors:\n");
scanf("%d", &colors);

int *arr = (int *)malloc(row * col * sizeof(int));
int i, j;

for (i = 0; i < row+level; i++)
for (j = 0; j < col+level; j++) {
number = rand() % colors ;
*(arr + i*col + j) = number;
}

printMatrix(arr, row, col,level);

printf("\n\nMove 0s to top:\n");
zerototopMatrix(arr, row, col,level);

printMatrix(arr, row, col,level);

free(arr);
return 0;
}```

2. I suggest using array index notation, e.g., *(arr + i*cols + j) would be arr[i*cols + j]. It tends to be easier to read that way.

Incidentally, this sounds like a situation where bubble sort would be easy to visualise: use a bubble sort on each column to "bubble up" the 0s until no more bubbling is done. More generally (and efficiently if your matrix has enough rows), pretty much any stable sorting algorithm to sort the 0s before the other values would work.

EDIT:
Okay, thinking about this a bit more, general sorting would be overkill. Maybe a solution inspired more by counting sort: start from the bottom of each column with a read and a write pointer. If the read pointer points to a 0, move the read pointer up one element in the column, and increment a counter. Otherwise, write the element at the read pointer to the element at the write pointer (if the counter is non-zero), then move both read and write pointers up by one. When the read pointer reaches the top of the column, use the counter with the write pointer to fill in the remaining top of the column with 0s.

I say read and write pointers, but you don't actually need to use pointers; array indices will do.

3. Originally Posted by laserlight
I suggest using array index notation, e.g., *(arr + i*cols + j) would be arr[i*cols + j]. It tends to be easier to read that way.

Incidentally, this sounds like a situation where bubble sort would be easy to visualise: use a bubble sort on each column to "bubble up" the 0s until no more bubbling is done. More generally (and efficiently if your matrix has enough rows), pretty much any stable sorting algorithm to sort the 0s before the other values would work.

EDIT:
Okay, thinking about this a bit more, general sorting would be overkill. Maybe a solution inspired more by counting sort: start from the bottom of each column with a read and a write pointer. If the read pointer points to a 0, move the read pointer up one element in the column, and increment a counter. Otherwise, write the element at the read pointer to the element at the write pointer (if the counter is non-zero), then move both read and write pointers up by one. When the read pointer reaches the top of the column, use the counter with the write pointer to fill in the remaining top of the column with 0s.
laserlight Thank you for your help!!! ... nice saying.... but I can't do that. Will you write a few sentences of code for me, and correct my code?

4. What have you tried?

5. Originally Posted by laserlight
What have you tried?

OK! I found my error and corrected it. Thank you. Case closed!!