gpt4 book ai didi

c - 按 C 行中的第一个元素对矩阵的行进行排序

转载 作者:行者123 更新时间:2023-11-30 21:28:55 25 4
gpt4 key购买 nike

我有一个矩阵:

3  3 -1 -1
1 1 1 -1
6 6 6 6
0 -1 -1 -1

我需要的是这个输出:

0 -1 -1 -1
1 1 1 -1
3 3 -1 -1
6 6 6 6

我怎样才能像这样正确排序?

最佳答案

希望这段代码对您有所帮助。您应该自己完成注释部分才能得到结果。

#define RAW_NUM 4
#define COL_NUM 4

int main () {
int ** matrix = malloc (RAW_NUM * sizeof (int *));
// you should be sure about successful allocation
int i, j;
for (i=0; i < RAW_NUM; i++) {
matrix [i]= malloc (COL_NUM * sizeof (int);
// you should be sure about successful allocation
}
// fill the matrix here
for (i=0; i < RAW_NUM - 1; i++) {
for (j=i +1; j < RAW_NUM; i++)
if (*matrix [i] > *matrix [j])
{
// please swap matrix [i] and matrix [j] here
}
}
}

关于c - 按 C 行中的第一个元素对矩阵的行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com