In: Computer Science
Input
No. of triangle : 5
Area of triangle 1: 12.00
Area of triangle 2: 14.14
Area of triangle 3: 14.00
Area of triangle 4: 7.31
Area of triangle 5: 17.32
Area of triangle 6: 14.14
Output:
Triangle 5 has the largest area: 17.32
If i have to save those area (input) into array form. How can i compare and find out which triangle with the largest area.
/*
* C++ Program to print the triangle with largest area
*/
#include <iostream>
using namespace std;
#define MAX 10
int main()
{
float area, areaTriangle[MAX];
int size;
cout << "No. of triangle: ";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "Area of triangle " << (i+1) << ": ";
cin >> area;
areaTriangle[i] = area;
}
int maxAreaIndx = 0;
for (int i = 0; i < size; i++)
{
if (areaTriangle[maxAreaIndx] < areaTriangle[i])
{
maxAreaIndx = i;
}
}
cout << "Triangle " << (maxAreaIndx + 1) << " has the largest area: " << areaTriangle[maxAreaIndx] << endl;
return 0;
}
