Small Triangles, Large Triangles

Sort by

recency

|

431 Discussions

|

  • + 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;
            }
        }
    
    }
    

    }

  • + 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);
  • + 0 comments
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    struct triangle
    {
    	int a;
    	int b;
    	int c;
    };
    
    typedef struct triangle triangle;
    double calculate_area(triangle tr) {
        double s = (tr.a + tr.b + tr.c) / 2.0;
        return sqrt(s * (s - tr.a) * (s - tr.b) * (s - tr.c));
    }
    
    int compare(const void *a, const void *b) {
        triangle *tr1 = (triangle *)a;
        triangle *tr2 = (triangle *)b;
        double area1 = calculate_area(*tr1);
        double area2 = calculate_area(*tr2);
        if (area1 < area2) return -1;
        if (area1 > area2) return 1;
        return 0;
    }
    
    void sort_by_area(triangle* tr, int n) {
    	/**
    	* Sort an array a of the length n
    	*/
         qsort(tr, n, sizeof(triangle), compare);
    }
    
  • + 0 comments

    Why does my code compile differently in vscode than in the test case here?

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    struct triangle
    {
    	int a;
    	int b;
    	int c;
    };
    
    typedef struct triangle triangle;
    double squart(triangle triangle)
    {
        double p = (triangle.a + triangle.b + triangle.c) / 2.0;
        double s = sqrt((double)(p * (p - triangle.a) * (p - triangle.b) * (p - triangle.c)));
        return s;
    }
    void sort_by_area(triangle* tr, int n) {
    	/**
    	* Sort an array a of the length n
    	*/
        int tap = n / 2;
        for(tap; tap > 0; tap /= 2)
        {
            for(int i = tap; i < n; i++)
            {
                for(int j = i - tap; j >= 0 && squart(tr[j]) > squart(tr[j+tap]); j -= tap)
                {   
                    triangle temp = tr[j];
                    tr[j] = tr[j+tap];
                    tr[j+tap] = temp;
                }
            }
        }
    }
    
    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);
            // printf(" s = %.2lf", squart(tr[i]));
    	}
    	return 0;
    }