Small Triangles, Large Triangles

Sort by

recency

|

434 Discussions

|

  • + 0 comments

    The formula for p is wrong. It's a+b+c/2 . On my screen it says a+b-c/2

  • + 0 comments

    include

    include

    include

    struct triangle { int a; int b; int c; };

    typedef struct triangle triangle;

    int compute_area(triangle tr){ int p = (tr.a + tr.b + tr.c) / 2; int s = sqrt(p * (p - tr.a) * (p - tr.b) * (p - tr.c)); return s; }

    int compare(const void *tr1, const void tr2){ int area1 = compute_area((triangle*)tr1); int area2 = compute_area((triangle)tr2); return (area1 - area2); }

    void sort_by_area(triangle* tr, int n) { /** * Sort an array a of the length n */ // Sort the computed area array. qsort(tr, n, sizeof(triangle), compare); }

    int main() { int n; scanf("%d", &n); triangle *tr = malloc(n * sizeof(triangle)); for (int i = 0; i < n; i++) { scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c); } sort_by_area(tr, n); for (int i = 0; i < n; i++) { printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c); }

    triangle tr1 = { 54, 62, 11 };
    triangle tr2 = { 31, 41, 14 };
    triangle tr3 = { 20, 23, 21 };
    printf("-------------- Test\n");
    

    1. printf("%d ", compute_area(tr1)); printf("%d ", compute_area(tr2)); printf("%d ", compute_area(tr3));

    return 0;
    

    }

  • + 0 comments

    [ int compute_area(triangle tr){ int p = (tr.a + tr.b + tr.c) / 2; int s = sqrt(p * (p - tr.a) * (p - tr.b) * (p - tr.c)); return s; }

    int compare(const void *tr1, const void tr2){ int area1 = compute_area((triangle*)tr1); int area2 = compute_area((triangle)tr2); return (area1 - area2); }

    void sort_by_area(triangle* tr, int n) { /** * Sort an array a of the length n */ // Sort the computed area array. qsort(tr, n, sizeof(triangle), compare); }](https://)

  • + 0 comments

    Epihany. having a separate function for calculating area makes the code much simpler to understand and implement...

    float calcArea (triangle tr) //func for cleaner code to calculate the area

    { float p = (tr.a + tr.b + tr.c) / 2.0;

    return sqrt (p * (p - tr.a) * (p - tr.b) * (p - tr.c));
    

    }

    void sort_by_area(triangle* tr, int n)

    {

    for (int i = 0; i < n - 1; i++)
    
    {
    
        for (int j = 0; j - n - 1; j++)
    
        {
    
            if(calcArea(tr[j]) > calcArea(tr[j+1]))
    
            {
    
                triangle temp = tr[j];
    
                tr[j] = tr[j+1];
    
                tr[j+1] = temp;
    
            }
    
        }
    
    }
    

    }

  • + 0 comments

    This is also one of the same way double area(triangle tr) { double s=(tr->a+tr->b+tr->c)/2.0; if((tr->a+tr->b)>tr->c&&(tr->b+tr->c)>tr->a&&(tr->a+tr->c)>tr->b) { double area=sqrt(s(s-tr->a)(s-tr->b)(s-tr->c)); return area;

    }
    return 0;
    

    }

    void sort_by_area(triangle* tr, int n) {

    double arr[n];
    for(int i=0;i<n;i++)
    {
         arr[i]=area(&tr[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            if (j == i)
            {
              arr[j] = arr[i]; 
            }
            if(arr[j]>arr[i])
            {
                double temp_1=arr[j];
                arr[j]=arr[i];
                arr[i]=temp_1;
                triangle temp=tr[j];
                tr[j]=tr[i];
                tr[i]=temp;
            }
        }
    
    }
    

    }