#include <stdlib.h> #include <stdio.h> #include <math.h> #define N 9 void foo(const double* a, double* b) { int i, j; const double* row = a; for (i = 0; i < N; ++i) { if (row[i] < 0.0) { double s = *row; for (j = 1; j < N; ++j) { s += row[j]; } b[i] = s; } else { const double *col = a+i; double s = *col; for (j = 1; j < N; ++j) { col += N; s += fabs(*col); } b[i] = s; } row += N; } } void bar() { int i, j; for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { printf("%.1f, ", (double) i+1+(j+1)*10); } printf("\n"); } } static int __cdecl compare(const void* lhs, const void* rhs) { const double* x1 = (const double*) lhs; const double* x2 = (const double*) rhs; return (x1 < x2? -1: (x1 > x2? 1: 0)); } int main() { double a[N*N] = { 11.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 91.0, 12.0, 22.0, 32.0, 42.0, 52.0, 62.0, 72.0, 82.0, 92.0, 13.0, 23.0, 33.0, 43.0, 53.0, 63.0, 73.0, 83.0, 93.0, 14.0, 24.0, 34.0, 44.0, 54.0, 64.0, 74.0, 84.0, 94.0, 15.0, 25.0, 35.0, 45.0, 55.0, 65.0, 75.0, 85.0, 95.0, 16.0, 26.0, 36.0, 46.0, 56.0, 66.0, 76.0, 86.0, 96.0, 17.0, 27.0, 37.0, 47.0, 57.0, 67.0, 77.0, 87.0, 97.0, 18.0, 28.0, 38.0, 48.0, 58.0, 68.0, 78.0, 88.0, 98.0, 19.0, 29.0, 39.0, 49.0, 59.0, 69.0, 79.0, 89.0, 99.0 }; int i; double b[N]; foo(a, b); /* bar(); */ for (i = 0; i < N; ++i) { qsort(a+i*N, N, sizeof(*a), compare); } return 0; } |