【问题标题】:Sort a matrix row wise (c programming)按行对矩阵进行排序(c 编程)
【发布时间】:2021-01-06 12:30:32
【问题描述】:

我编写了这段代码用于对 nxn 矩阵进行排序:奇数行按降序排列,双行按升序排列,但它没有通过编译阶段。

我做错了什么?

它主要告诉我这个:从 'int *' 分配给 'int' 使指针从没有强制转换的整数(我该如何解决这个问题)?

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

【问题讨论】:

  • 看来你想分配int*的数组,而不是int,所以分配它。此外,似乎应该更改比较函数或创建新的比较函数。
  • 向我们展示您有疑问的编译器输出会有所帮助。但我猜是创建矩阵的线?

标签: c sorting matrix


【解决方案1】:

您应该将矩阵分配更改为:

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

(1):声明一个大小为n x int*的指针数组。

(2):这里数组中的每个指针都指向一个大小为n x int的整数数组。

【讨论】:

  • 注意:不需要强制转换 calloc 或 malloc 调用的结果,但您应该始终检查它是否为 NULL。
【解决方案2】:

我改变了你的矩阵分配

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

int mat[n][n];

我改变了 ordred 的循环

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

因为他花了很多时间(跑步的时间)来

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

我的代码:

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

【讨论】:

  • 什么是请求矩阵的最佳方式:我以两种方式做到这一点 - 1)正如我在这段代码中所写的那样 2)通过索引( [0,0] [ 0,1]...),在你的改进中,我理解为什么它更好,但我不明白 while (n
  • 在您的代码中,您可以使用分配,因为C中的分配方法用于动态分配具有指定大小的单个大块内存。它返回一个 void 类型的指针,该指针可以转换为任何形式的指针。它使用默认垃圾值初始化每个块,并且在运行时更改数据结构(如数组)的大小,因此请求矩阵的最佳方法是分配方法
  • 能够编写int mat[n][n];,其中 n 在编译时未知是编译器扩展 AFAIK。 ANSI C 期望您以老式方式分配和释放该内存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2012-04-22
  • 2016-10-27
相关资源
最近更新 更多