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

Page 1 of 1

## 3 Replies - 242 Views - Last Post: 12 May 2019 - 02:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=416084&amp;s=035055b7f7afb040034a55c0a0c50ed0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 akis

Reputation: 2
• Posts: 11
• Joined: 19-December 18

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

Posted 11 May 2019 - 11:30 AM

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

After the movement of 0s I want to have:

Someone please correct my zerototopMatrix function below:

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

#### Attached image(s)

Is This A Good Question/Topic? 0

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

### #2 jimblumberg

Reputation: 5731
• Posts: 17,557
• Joined: 25-December 09

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

Posted 11 May 2019 - 12:11 PM

Have you run the program with your debugger single stepping through the code in that function while watching the values of your variables?

Also posted here.

Jim

### #3 akis

Reputation: 2
• Posts: 11
• Joined: 19-December 18

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

Posted 12 May 2019 - 12:52 AM

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

### #4 baavgai

• Dreaming Coder

Reputation: 7433
• Posts: 15,409
• Joined: 16-October 07

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

Posted 12 May 2019 - 02:24 AM

Keeping your 2D in a 1D array is fine, but you're probably confusing yourself with it. Also, the level thing seems pointless and it looks like things would got hopelessly sideways if it weren't zero.

Write a helper function:
```int matrixGet(int *arr, int rows, int cols, int row, int col) {
// notice, we don't actually use rows, but we pass it because
// it would be more confusing not to
// and maybe in the future we can do some bounds check
return *(arr + row * cols + col);
}

```

Then your print, for instance, becomes much easier to follow:
```// don't put parents around your array pointer!!!
// it actually makes it look a little like a function pointer: one of the most confusing syntax constructs you'll run into in C
void printMatrix(int *arr, int rows, int cols) {
int row, col; // note, decent variable names

printf("\n\nThe matrix elements are:\n   ");
for (col = 0; col < cols; col++) {
printf("%d ", col + 1);
}
printf("\n");

for (col = 0; col < cols; col++) {
printf("--");
}
printf("\n");

for (row = 0; row < rows; row++) {
printf("%d| ", row + 1);
for (col = 0; col < cols; col++) {
printf("%d ", matrixGet(arr, rows, cols, row, col));
}
printf("\n");
}
}

```

You'll also want a matrixSet. Or, if you prefer, a matrixAt that returns a pointer to the proper entry.

If you've learned struct, now would be the time to pull that out. e.g.
```typedef struct {
int *cells, rows, cols;
} Matrix;

int *matrixAt(Matrix *, int row, int col);
void printMatrix(Matrix *);

```

Hope this helps.