C语言中按列进行二维数组排序

C语言中按列进行二维数组排序,c,sorting,multidimensional-array,columnsorting,C,Sorting,Multidimensional Array,Columnsorting,我有下面的代码,可以产生 [p18d541@csci112 program1]$ ./main 17 < inp17.txt 12 25 110 168 35 64 113 134 91 158 183 217 102 129 130 146 26 116 215 223 0 78 81 162 19 25 204 222 124 138 157 245 137 183 201 249 61 67 106 236 60 71 106 236 63 81 106 24

我有下面的代码,可以产生

[p18d541@csci112 program1]$ ./main 17 < inp17.txt
 12 25 110 168
 35 64 113 134
 91 158 183 217
 102 129 130 146
 26 116 215 223
 0 78 81 162
 19 25 204 222
 124 138 157 245
 137 183 201 249
 61 67 106 236
 60 71 106 236
 63 81 106 240
 14 27 111 168
 17 27 111 168
 26 116 215 220
 111 137 202 249
 111 137 202 246
因此,我的代码在输入中扫描,并将值存储在网络中 然后我需要按照从最小到最大的顺序排列它们,从第一个数字开始,一直到右边。前几个例子是

0 78 81 162
12 25 110 168
14 27 111 168
.
.
.

111 137 202 246
111 137 202 249
.
.
.

//声明库
#包括
#包括
//声明要在程序中使用的其他函数/文件
作废打印乐趣(作废);
int-sort_-fun(int-arg,无符号字符网络[arg][4]);
无效阅读乐趣(无效);
//读取命令行输入并存储信息
int main(int argc,字符**argv){
//十二进制变量
int arg=0;
//将argv转换为int
arg=atoi(argv[1]);
//为网络分配大小
无符号字符网络[arg][4];
//为网络分配输入

对于(intj=0;jj)“按列排列”是什么意思?您实际想要的预期输出是什么?我添加了一个说明,如果您有任何其他问题,请告诉我。好的,那么。这个基本想法是比较涉及多个键。在您的情况下,有四个键,它们是IP地址的四个字段。它有助于编写比较函数,例如
int compare(int addr1[4],int addr2[4])
。比较函数应返回-1(小于)、0(等于)或1(大于)。首先将
addr1[0]
addr2[0]进行比较
。如果它们不相等,则根据需要返回-1或1。否则,移动到下一个字段。最后,如果所有四个字段都相同,则返回0。我不太明白这是如何工作的,我查看了提供的示例,但我不明白如何使用数组,因为它看起来像是使用一维i数组,我不知道如何翻译。除非我必须使用for循环而不是常量?将问题想象为对IP地址的一维数组进行排序。排序函数需要两个嵌套循环,例如使用索引
i
j
。1D数组的元素是
网络[i]
网络[j]
。您需要能够比较这些元素并交换它们。因此,编写
比较
函数和
交换
函数。调用
比较
函数,如
结果=比较(网络[i],网络[j])
。调用
交换
函数,如
交换(网络[i],networks[j])
。一旦具备了可以比较和交换IP地址的功能,排序代码就与对1D数组排序完全相同。
0 78 81 162
12 25 110 168
14 27 111 168
.
.
.

111 137 202 246
111 137 202 249
.
.
.
//declare libraries

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

 //declare other functions/files to be used in the program
 void print_fun(void);
 int sort_fun(int arg, unsigned char networks[arg][4]);
 void read_fun(void);


 //read command line input and store the information
 int main(int argc, char** argv){
     //declar variable
     int arg = 0;

     //make argv into an int
     arg = atoi(argv[1]);
     //assign size to networks
      unsigned char networks[arg][4];

   //assign input to networks
     for (int j =0; j<1; ++j){
         if(argc == 1)
              {
                 printf("ERROR ERROR, you messed up\n");
             }

         else
         {
         // hold network addresses in a 2-d array, with 4 nsigned char

             for(int k = 0; k<arg; k++){
                 for (int i =0; i<4; i++){

                 scanf("%hhu.", &networks[k][i]);
                 //checks to see if scanf was working roperly
                // printf(" %hhu",networks[k][i]);

             }
                //printf("\n");
             }}}

             sort_fun(arg, networks);
 //sort array

 //count networks

 //print info about the array
 return(0);
}

 int sort_fun(int arg, unsigned char networks[arg][4]){
     //declaring variabes

 //sorting by comlumn p
     for (int k = 0; k < arg; k++){
         for( int i = 0; i < 4; i++){
             for (int j = i+1; j<4; ++j){
                 if (networks[k][i] > networks[k][j]) {
                     int swap = networks[k][i];
                     networks[k][i] = networks[k][j];
                     networks[k][j] = swap;
                 }
             }
         }
    }

for (int i =0; i<arg; i++){
     for (int j =0; j < 4; j++){
         printf(" %hhu", networks[i][j]);
         }
         printf("\n");
         }
 return(0);
 }