C Program To Sort Triangles Based On Area - Tutorialspoint
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Suppose we have an array of different triangles where triangles[i] = [ai, bi, ci] these are the sides of ith triangle. We shall have to sort the triangles based on their area. The area of a triangle by using sides is: square root of p*(p-a)*(p-b)*(p-c) where p = (a+b+c)/2.
So, if the input is like (7, 24, 25), (5, 12, 13), (3, 4, 5), then the output will be (3, 4, 5), (5, 12, 13), (7, 24, 25)
To solve this, we will follow these steps −
- Define triangle object with sides a, b and c
- Define a function square(), this will take Triangle t,
- a := t.a
- b := t.b
- c := t.c
- return (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c)
- From the main method, do the following:
- for initialize i := 0, when i < N, update (increase i by 1), do:
- for initialize j := i + 1, when j < N, update (increase j by 1), do:
- if square(a[i]) > square(a[j]), then:
- swap a[i] and a[j]
- if square(a[i]) > square(a[j]), then:
- for initialize j := i + 1, when j < N, update (increase j by 1), do:
Example
Let us see the following implementation to get better understanding −
#include <stdio.h> #define N 3 struct Triangle{ int a, b, c; }; int square(struct 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 solve(struct Triangle* a){ for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) if (square(a[i]) > square(a[j])){ struct Triangle temp = a[i]; a[i] = a[j]; a[j] = temp; } } int main(){ struct Triangle triangles[N] = {{7, 24, 25}, {5, 12, 13}, {3, 4, 5}}; solve(triangles); for (int i = 0; i < N; i++){ printf("(%d, %d, %d)", triangles[i].a, triangles[i].b, triangles[i].c); } }Input
{{7, 24, 25}, {5, 12, 13}, {3, 4, 5}}Output
(3, 4, 5) (5, 12, 13) (7, 24, 25)
Arnab Chakraborty Updated on: 2021-10-08T11:26:27+05:30 866 Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Area Of Triangle C Programming
-
Program To Find The Area Of A Triangle - Javatpoint
-
Area Of A Triangle In C | Programming Simplified
-
Area Of Triangle Program In C - Sanfoundry
-
C Program To Find Area Of A Triangle - Tutorial Gateway
-
How Do I Write A C Program To Find Area Of A Triangle? - Quora
-
C Program Area Of Triangle - Java Tutoring
-
C Program To Find The Area Of Triangle Using Base And Height
-
C Program To Find Area Of A Triangle - Codeforwin
-
C Program To Find Area Of A Triangle - GTU Practical
-
Program To Find Area Of A Triangle - GeeksforGeeks
-
C Program To Find Area Of Triangle - YouTube
-
C Program To Print Area Of Triangle, Square, Circle, Rectangle And ...
-
C Program To Find Area Of Triangle - Decode School
-
C Program To Find Area And Perimeter Of Triangle - CsTutorialpoint