codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <stdio.h> #include <stdlib.h> #include <math.h> struct mycomplex { double re; double im; }; int cmp(void const *a, void const *b) { struct mycomplex const *x, *y; x = a; y = b; if ((x->re * x->re + x->im * x->im) > (y->re * y->re + y->im * y->im)) return 1; else return -1; } int main() { int n, i; static struct mycomplex data[] = { { 9.7, 9.3 }, { 3.1, 4.1 }, { 3.3, 8.3 }, { 2.3, 8.4 }, { 6.2, 6.4 }, { 5.9, 2.6 }, { 5.3, 5.8 }, { 2.7, 9.5 } }; n = sizeof(data) / sizeof(data[0]); qsort(data, n, sizeof(struct mycomplex), cmp); for (i = 0; i < n; i++) printf("(%.1f, %.1f) = %.3f\n", data[i].re, data[i].im, sqrt(data[i].re * data[i].re + data[i].im * data[i].im)); putchar('\n'); return 0; } /* end */
Private
[
?
]
Run code
Submit