c - C编程中根据一列对二维数组进行排序

标签 c arrays

我大约 10 周前开始编码,目前一直在做 2D 数组分配:/

程序应该在每一列中随机分配数字,显示 exam1、exam2 的分数以及每个学生的平均分数,这很简单。

enter image description here

但是,现在我应该根据最终的“分数”对 2D 数组进行排序。

enter image description here

教授只教我们如何用一维数组进行冒泡排序,但没有教我们如何用二维数组进行冒泡排序。但是,我仍然尝试以某种方式即兴创作。

这是我使用的代码:

/*for(i = 0; i < 29; i++){
    for(j = 29; j > i; j--)
        if(array2[j-1][4] < array2[j][4]){
                t = array2[j-1][4];
                array2[j-1][4] = array2[j][4]; array2[j][4] = t[4];
        }
}*/

但是,这只会对“分数”列进行排序并显示每个分数的等级,但不会对整行进行排序。

enter image description here

因此,我尝试将整行移动到一维数组中,然后交换。

for(i = 0; i < 29; i++){
    for(j = 29; j > i; j--)
        if(array2[j-1][4] < array2[j][4]){
            for(r = 0; r < 4; r++)
                t[r] = array2[j-1][r];
            for(r = 0; r < 4; r++)
                array2[j-1][r] = array2[j][r];
            for(r = 0; r < 4; r++)
                array2[j][r] = t[r];
        }
}

但是上面的代码不会执行任何操作,并且 array2 将保持不变:/

有什么帮助吗?

最佳答案

根据您的方式解决:

    for(i = 0; i < 29; i++){
         for(j = 29; j > i; j--)
             if(array2[j-1][4] < array2[j][4]){
                 for(r = 0; r < 5; r++)
                     t[r] = array2[j-1][r];
                 for(r = 0; r < 5; r++)
                     array2[j-1][r] = array2[j][r];
                 for(r = 0; r < 5; r++)
                     array2[j][r] = t[r];
              }
    }

这部分是我在数组中分配值的部分:

    srand(time(NULL));
    int i,j;
    int array2[ROW][COLUMN];  //ROW=30, COLUMN=5
    int t[COLUMN];
    int h=0;
    int sum=0;  

    for(i=0; i<ROW;i++) {
         array2[i][0] = (101+i);
         for(j=1; j<4; j++) {
             array2[i][j] = rand()%100+1;   
             sum+=array2[i][j];             
         }
         array2[i][j] = sum/3;      //calculate the score
         sum=0;
    }

...

我认为,有必要使用指针数组来实现此解决方案,如下所述。当您尝试使用静态数组解决此问题时,有必要将每个值(exam1,exam2,exam3,Score,ID)一一分配。

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

#define ROW     30  //Number of students
#define COLUMN  5   //Keep respectively the values of ID, 1.exam , 2.exam, 3.exam, Score

int main()
{
    srand(time(NULL));
    int i,j;
    int sum=0;
    int *temp;  //use it in sorting to swap address of pointers

    // Dynamically allocate a 2D array
    int **student = (int **)malloc(ROW * sizeof(int *)); 
    for (i=0; i<ROW; i++) 
         student[i] = (int *)malloc(COLUMN * sizeof(int));


    for(i=0; i<ROW;i++) {
        student[i][0] = (101+i);
        for(j=1; j<4; j++) {
            student[i][j] = rand()%100+1;   
            sum+=student[i][j];             
        }
        student[i][j] = sum/3;      //calculate the score
        sum=0;
    }

    printf("\nNotes of students according to ID order : \n");
    printf("ID      1.exam      2.exam      3.exam      Score\n");
    printf("---     ------      ------      ------      -----\n");
    for(i=0; i<ROW; i++) {
        printf("%d", student[i][0]);
        for(j=1; j<COLUMN; j++) {
            printf("%17d", student[i][j]);
        }
        puts("");
    }



    //SORTING

    for(i=0; i<(ROW-1); i++) {
        for(j=0; j<(ROW-1-i); j++) {
            if(student[j][4]<student[j+1][4]) {
                temp = *(student+j);                
                *(student+j) = *(student+j+1);
                *(student+j+1)= temp;
            }
        }
    }


    puts("\nNotes of students according to Score order : \n");
    printf("ID      1.exam      2.exam      3.exam      Score\n");
    printf("---     ------      ------      ------      -----\n");
    for(i=0; i<ROW; i++) {
        printf("%d", student[i][0]);
        for(j=1; j<COLUMN; j++) {
            printf("%17d", student[i][j]);
        }
        puts("");
    }

}

有用的链接:

How to dynamically allocate a 2D array in C?

Using a swap function to swap the address in pointers of a 2D-array

关于c - C编程中根据一列对二维数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53059544/

相关文章:

Javascript for 数组中的循环行为

php - 在 foreach 循环中取消设置数组值

c++ - 如何使用字符串(字符数组)初始化 char 指针而不是整数数组的 int 指针?

C 错误 : undefined reference to function, 但已定义

arrays - 在 PL/pgSQL 中使用动态查询创建数组

ios - Swift:如何创建保存字典的数组

Java 8 从数组中流式传输唯一字符

c - c中的这种编码风格是什么?

尝试编译 HPCC 基准测试时无法链接到 BLAS/ATLAS

python - 实参校验性能影响