Small Triangles, Large Triangles in c Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Small Triangles, Large Triangles in c Hacker Rank Solution. This problem is a part of the HackerRank C Programming Series.

We also provide Hackerrank solutions in C++Java programming, and Python Programming languages so whatever your domain we will give you an answer in your field.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Small Triangles, Large Triangles in c - Hacker Rank Solution
Small Triangles, Large Triangles in c – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

Small Triangles, Large Triangles in c Hacker Rank Solution

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 volume of the triangle with sides a, b and c is Heron’s formula:
s = √(P * (P – a) * (P – b) * (P – c)) where P = (a+b+c)/2.

Input Format

First line of each test file contains a single integer n.n lines follow with ai, bi and con each separated by single spaces.

Constraints

  • 1<=n<=100
  • 1<= ai, bi, ci <=70
  • ai + bi > ci, ai + ci > bi and bi+ci > ai

Output Format

Print exactly n lines. On each line print 3 integers separated by single spaces, which are ai, bi and cof 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.





Small Triangles, Large Triangles in c Hacker Rank Solution

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
 int a;
 int b;
 int c;
};
typedef struct triangle triangle;
void sort_by_area(triangle* tr, int n)
{
  int *p=malloc(n*sizeof(float));
//create array of size n to store "volumes"
    for(int i=0;i<n;i++)
    {
 float a=(tr[i].a+tr[i].b+tr[i].c)/2.0;
//use 2.0 compulsary int/int gives int, int/float gives float
       p[i]=(a*(a-tr[i].a)*(a-tr[i].b)*(a-tr[i].c));
//formula without sqrt as areas are different guarenteed
//because sqrt dosent work well with float values
    }
//bubble sort
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n-i-1;j++)
        {
            if(p[j]>p[j+1])
            {
                int temp=p[j];
                p[j]=p[j+1];
                p[j+1]=temp;
//swapping array of areas in ascending
//and simuntaneously the structure contents
                temp=tr[j].a;
                tr[j].a=tr[j+1].a;
                tr[j+1].a=temp;
                temp=tr[j].b;
                tr[j].b=tr[j+1].b;
                tr[j+1].b=temp;
                temp=tr[j].c;
                tr[j].c=tr[j+1].c;
                tr[j+1].c=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;
}

Disclaimer: The above Problem (Small Triangles, Large Triangles in c ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

FAQ:

Is HackerRank good for C?

HackerRankHackerRank offers you to solve these programming challenges in any of various programming languages such as C, Java, Python, Ruby, etc. Apart from this, participants can solve the problems in various computer science domains like algorithms, machine learning, and artificial intelligence.

What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

Is HackerRank good for beginners?

HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

What is C programming in simple words?

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C’s features cleanly reflect the capabilities of the targeted CPUs. Wikipedia

Where can I find HackerRank solutions in C?

in this post you will get all the solutions of HackerRank C Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment