HackerRank Small Triangles, Large Triangles Solution | C Programming Solution | Ask The Code
Problem:
You are given n triangles, specifically, their sides ai, bi and ci. Print them in the same style but sorted by their areas from the smallest one to the largest one. It is guaranteed that all the areas are different.
The best way to calculate a area of a triangle with sides a, b and c is Heron's formula:

Input Format:
The first line of each test file contains a single integer n.
n lines follow with three space-separated integers, ai, bi and ci.
Output Format:
Print exactly n lines. On each line print 3 space-separated integers, the ai, bi and ci of the corresponding triangle.
Sample input:
3
7 24 25
5 12 13
3 4 5
Sample output:
3 4 5
5 12 13
7 24 25
Explanation:
The square of the first triangle is 84. The square of the second triangle is 30. The square of the third triangle is 6. So the sorted order is the reverse one.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
int square(triangle t){
int a = t.a, b = t.b, c = t.c;
return (a + b + c)*(a + b - c)*(a - b + c)*(-a + b + c);
}
void sort_by_area(triangle* a, int n){
for (int i = 0; i < n; i++ask){
for (int j = i + 1; j < n; j++){
if (square(a[i]) > square(a[j])){
triangle temp = a[i];
a[i] = a[j];
a[j] = 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);
}
return 0;
}
Comments