We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Small Triangles, Large Triangles
Small Triangles, Large Triangles
Sort by
recency
|
436 Discussions
|
Please Login in order to post a comment
It reminds us that foundational shapes, like small triangles, often build into more intricate systems—large triangles—mirroring how basic concepts evolve into broader frameworks in both math and life. Gold365 site Login
Here is Small Triangles, Large Triangles solution in c - https://programmingoneonone.com/hackerrank-small-triangles-large-triangles-solution-in-c.html
The formula for p is wrong. It's a+b+c/2 . On my screen it says a+b-c/2
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); }
1. printf("%d ", compute_area(tr1)); printf("%d ", compute_area(tr2)); printf("%d ", compute_area(tr3));
}
[ 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://)