fork download
  1. #include "time.h"
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4.  
  5. //N x M parameters
  6. static int rows = 7;
  7. static int columns = 10;
  8.  
  9. int row_compare(const void * first_row, const void * second_row)
  10. {
  11. const int * first_row_1 = *(const int**)first_row;
  12. const int * second_row_1 = *(const int**)second_row;
  13.  
  14. if (first_row_1[columns] < second_row_1[columns])
  15. return -1;
  16. else if (first_row_1[columns] > second_row_1[columns])
  17. return 1;
  18. else
  19. return 0;
  20. }
  21.  
  22. void print_array(int ** arr)
  23. {
  24. printf("\nARRAY:\n");
  25. for(int i = 0; i < rows; i++)
  26. {
  27. printf("ROW %d:", i);
  28. for(int j = 0; j < columns; j++)
  29. {
  30. printf("%4d ", arr[i][j]);
  31. }
  32. printf("METRIC: %d\n", arr[i][columns]);
  33. }
  34. }
  35.  
  36. int main(int argc, char* argv[])
  37. {
  38. //init values
  39. srand((unsigned)time(NULL));
  40. //allocs
  41. int ** my_array = (int**)malloc(sizeof(int*) * rows); //allocate pointers to rows
  42. for (int i = 0; i < rows; i++)
  43. my_array[i] = (int*)malloc(sizeof(int) * (columns+1)); //allocate the whole row + place for metric
  44. //fill the array + calculate the metric
  45. for (int i = 0; i < rows; i++)
  46. {
  47. //fill row with random values
  48. for (int j = 0; j < columns; j++)
  49. my_array[i][j] = (rand() % 20) - 10;
  50. //calculate metric for a row
  51. int metric = 0;
  52. for (int j = 0; j < columns; j++)
  53. {
  54. int elem = my_array[i][j];
  55. if ( (elem % 2) != 0 && elem < 0)
  56. metric += elem;
  57. }
  58. my_array[i][columns] = metric; //metric value
  59. }
  60. print_array(my_array);
  61. //sort it!
  62. qsort(my_array, rows, sizeof(int*), row_compare);
  63. print_array(my_array);
  64. system("pause");
  65. //free everything
  66. for (int i = 0; i < rows; i++)
  67. free(my_array[i]);
  68. free(my_array);
  69. return 0;
  70. }
  71.  
  72.  
Success #stdin #stdout #stderr 0s 3468KB
stdin
Standard input is empty
stdout
ARRAY:
ROW 0:   4    -7     6     5    -1     1    -9     7     9     3  METRIC: -17
ROW 1:  -4    -2     6     8     6     0    -2    -4     7     1  METRIC: 0
ROW 2:   4     0    -2    -5    -3    -9     3     0    -7    -5  METRIC: -29
ROW 3:  -9     7    -2     7     2     8    -1    -4    -3   -10  METRIC: -13
ROW 4:   1     3   -10    -2    -7     7     0     1    -7    -3  METRIC: -17
ROW 5:  -8     7    -1    -7     5     6    -6   -10     8     9  METRIC: -8
ROW 6:  -5     9     7    -5     7    -9    -7     8    -3     0  METRIC: -29

ARRAY:
ROW 0:   4     0    -2    -5    -3    -9     3     0    -7    -5  METRIC: -29
ROW 1:  -5     9     7    -5     7    -9    -7     8    -3     0  METRIC: -29
ROW 2:   4    -7     6     5    -1     1    -9     7     9     3  METRIC: -17
ROW 3:   1     3   -10    -2    -7     7     0     1    -7    -3  METRIC: -17
ROW 4:  -9     7    -2     7     2     8    -1    -4    -3   -10  METRIC: -13
ROW 5:  -8     7    -1    -7     5     6    -6   -10     8     9  METRIC: -8
ROW 6:  -4    -2     6     8     6     0    -2    -4     7     1  METRIC: 0
stderr
sh: 1: pause: not found