Y.D
Y.D

Reputation: 43

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.

What did I do wrong?

It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?

#include <stdio.h>
#include <stdlib.h>
   
int comp_a(int a, int b) {
  if (a < b) {
    return 1;
  } else {
    return 0;
  }
}
    
int comp_d(int a, int b) {
  if (a > b) {
    return 1;
  } else {
    return 0;
  }
}

void sort(int a[], int n, int (*comp)(int, int)) {
  int t;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (comp(a[j], a[j + 1]) == 0) {
        t = a[j];
        a[j] = a[j + 1];
        a[j + 1] = t;
      }
    }
  }
}
    
int main() {
  int n;
  printf("Enter the dimension of matrix(n): ");   
  scanf("%d", &n);                                
 
  int *mat = (int*)calloc(n, sizeof(int));

  for (int i = 0; i < n; i++) {
    mat[i] = (int*)calloc(n, sizeof(int));
  }
    
  for (int i = 0; i < n; i++) {
    printf("Enter row [%d]: ", i);
    for (int j = 0; j < n; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
    
  for (int i = 0; i < n; i++) {
    if (i%2 == 0) {
      sort(mat[i], n, &comp_a);
    } else {
      sort(mat[i], n, &comp_d);
    }
  }
    
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      printf("%d ", mat[i][j]);
    }
    printf("\n");
  }

  return 0;
}

Upvotes: 1

Views: 660

Answers (2)

MED LDN
MED LDN

Reputation: 669

I changed your matrix allocation

int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
    mat[i] = (int*)calloc(n, sizeof(int));
}

to

int mat[n][n];

I changed for loop of ordred

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n - 1; j++) {
        if(comp(a[j], a[j + 1]) == 0)

because he take a lot of time (time of running) to

 for(int i = 0; i < n; i++) {
    for(int j = i+1; j < n; j++) {
        if(comp(a[i], a[j]) == 0) 

my code:

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


int comp_a(int a, int b) {
if(a < b) {
    return 1;
}
else {
    return 0;
}
}

int comp_d(int a, int b) {
if(a > b) {
    return 1;
}
else {
    return 0;
}
}

void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
    for(int j = i+1; j < n; j++) {
        if(comp(a[i], a[j]) == 0) {
            t = a[j];
            a[j] = a[i];
            a[i] = t;
        }
    }
}
}

int main() {
int n;
do
{
    printf("Enter the dimension of matrix(n): ");
    scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
    printf("Enter row [%d]: ", i);
    for(int j = 0; j < n; j++) {
        scanf("%d", &mat[i][j]);
    }
}

for(int i = 0; i < n; i++) {
    if(i%2 == 0) {
        sort(mat[i], n, &comp_a);
    }
    else {
        sort(mat[i], n, &comp_d);
    }
}

for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
        printf("%d ", mat[i][j]);
    }
    printf("\n");
}

return 0;
}

Upvotes: 0

a.koptan
a.koptan

Reputation: 1248

You should change your matrix allocation to this:

int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
    mat[i] = (int*)calloc(n, sizeof(int));  // (2)
}

(1): Declaring an array of pointers of size n x int*.

(2): Where each pointer in this array points to an array of integers of size n x int.

Upvotes: 3

Related Questions