-1

There is 2D array with pointers from user, filled with random numbers, program count sum of every row. I need to sort array by sum of every row and print it. For example if we have array:1 2 2 (sum=5)2 9 9 (sum=20)2 1 6 (sum=9) output should be:1 2 2 (sum=5)2 1 6 (sum=9)2 9 9 (sum=20). Thanks for help.

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;
}
Džo Nis
  • 13
  • 3
  • 2
    Welcome to Stack Overflow ! Standard warning: [Do not cast the result of malloc](http://stackoverflow.com/q/605845/1151654) – Eregrith Jun 08 '15 at 14:17
  • 3
    You should also format and indent your code better, it will only help you. – Eregrith Jun 08 '15 at 14:17
  • 2
    Also don't use horrible syntax like `*(*(array+i)+j)` - it's much easier and more intuitive to write `array[i][j]`. – Paul R Jun 08 '15 at 14:19
  • (option 1) use qsort, columns size pass to compare function by global variable. (option 2) make array index and array of sum then use qsort to it. (option 3) make custom sort function mixed option 2. – BLUEPIXY Jun 10 '15 at 09:01
  • Maybe you could write me an example how to use qsort in this program ? – Džo Nis Jun 11 '15 at 12:31

1 Answers1

0

sample of option 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;
}

sample of option 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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70