Small Triangles, Large Triangles

  • + 0 comments
    1. double calculate_area(triangle tr) {
    2. double s = (tr.a + tr.b + tr.c) / 2.0;
    3. return sqrt(s * (s - tr.a) * (s - tr.b) * (s - tr.c));
    4. }
      1. int compare(const void *a, const void *b) {
    5. triangle *tr1 = (triangle *)a;
    6. triangle *tr2 = (triangle *)b;
    7. double area1 = calculate_area(*tr1);
    8. double area2 = calculate_area(*tr2);
    9. if (area1 < area2) return -1;
    10. if (area1 > area2) return 1;
    11. return 0;
    12. }
      1. void sort_by_area(triangle* tr, int n) {
    13. /**
      • Sort an array a of the length n */
    14. qsort(tr, n, sizeof(triangle), compare);