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:


Move 0s at top of each column in a MxN array-matrix-png

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; 
}