2D数组,按行

时间:2015-06-08 14:14:46

标签: c arrays sorting pointers

有来自用户的指针的二维数组,填充随机数,每行的程序计数总和。我需要按行的总和对数组进行排序并打印出来。例如,如果我们有数组:1 2 2(sum = 5)2 9 9(sum = 20)2 1 6(sum = 9) 输出应为:1 2 2(sum = 5)2 1 6(sum = 9)2 9 9(sum = 20)。感谢帮助。

int main () {
    int i, j, row, column, **array,sum;
    time_t seconds;
    time (&seconds);
    srand ((unsigned int)seconds );

    printf ("Write number of rows:");
    scanf ("%d", &row);
    printf ("Write number of columns:");
    scanf ("%d", &column);

    array=(int**) malloc (row * sizeof(int *));
    if (array!=NULL){
        for (i=0; i<row;i++)
            array[i]= (int*) malloc (column *sizeof(int));
    }

    for (i=0; i<row;i++)          
        for (j=0; j<column;j++)
            array[i][j]=(rand()%100);

    for (i=0; i<row;i++){           
        for (j=0; j<column;j++)
            printf("%d ",array[i][j] );

        printf ("\n");
    }

    for(i=0;i<row;i++){   //find sum of each row
        sum=0;
        for(j=0;j<column;j++){
            sum=sum+array[i][j];
        }
        printf("%d \n",sum);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

选项1的样本

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

int COLUMNS;

int sum(int len, int *array){
    int i, sum = 0;
    for(i=0; i<len; ++i)
        sum += *array++;
    return sum;
}

int cmp(const void *a, const void *b){
    int sum1 = sum(COLUMNS, *(int**)a);
    int sum2 = sum(COLUMNS, *(int**)b);
    return (sum1 > sum2) - (sum1 < sum2);
}

int main(void){
    int i, j, row, column, **array;

    row = 3; column = 3;

    array = (int**) malloc (row * sizeof(*array));//cast of (int**) is redundant.
    array[0] = (int []){1, 2, 2};
    array[1] = (int []){2, 9, 9};
    array[2] = (int []){2, 1, 6};

    COLUMNS = column;//size of columns pass to compare function by global variable.
    qsort(array, row, sizeof(*array), cmp);

    for (i=0; i<row;i++){          
        for (j=0; j<column;j++)
            printf("%d ",array[i][j] );
        printf ("\n");
    }
    free(array);

    return 0;
}

选项2的样本

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

int sum(int len, int *array){
    int i, sum = 0;
    for(i=0; i<len; ++i)
        sum += *array++;
    return sum;
}

typedef struct pair {
    int *p;//or index
    int sum;
} Pair;

int cmp(const void *a, const void *b){
    Pair const *x = a;
    Pair const *y = b;
    return (x->sum > y->sum) - (x->sum < y->sum);
}

int main(void){
    int i, j, row, column, **array;

    row = 3; column = 3;

    array = (int**) malloc (row * sizeof(*array));//cast of (int**) is redundant.
    array[0] = (int []){1, 2, 2};
    array[1] = (int []){2, 9, 9};
    array[2] = (int []){2, 1, 6};

    Pair *temp = malloc(row * sizeof(*temp));
    for(i = 0; i < row; ++i){
        temp[i].p = array[i];
        temp[i].sum = sum(column, array[i]);
    }

    qsort(temp, row, sizeof(*temp), cmp);

    for (i=0; i<row;i++){          
        for (j=0; j<column;j++)
            printf("%d ", temp[i].p[j] );
        printf ("\n");
    }
    free(temp);
    free(array);

    return 0;
}