Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Small Triangles, Large Triangles in C Hackerrank Solution

You are given  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 ab and c is Heron’s formula:

s = √(P * (P – a) * (P – b) * (P – c)) where P = (a+b+c)/2.

Input Format

The first line of each test file contains a single integer nn lines follow with three space-separated integers, aibi and ci.

Constraints

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

Output Format

Print exactly n lines. On each line print 3 space-separated integers, the  aibi  and  ci of the corresponding triangle.

Sample Input 0

3
7 24 25
5 12 13
3 4 5

Sample Output 0

3 4 5
5 12 13
7 24 25

Explanation 0

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

Comments