1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <string>
using namespace std;
// Function prototypes
void getLengths(double[], double);
void sortArray(double[], double);
void isTriangle(double[], double);
void equalSides(double[], double);
void angleType(double[], double);
// Main function calls all the other functions
int main()
{
// Triangles always have 3 sides
const int sides = 3;
double triangleLengths[sides];
// Function calls
getLengths(triangleLengths, sides);
sortArray(triangleLengths, sides);
isTriangle(triangleLengths, sides);
equalSides(triangleLengths, sides);
angleType(triangleLengths, sides);
system("pause");
return 0;
}
// This function collects the three lengths from the user
void getLengths(double lengths[], double sides)
{
// The counter for given lengths
int index;
// Ask user to input 3 lengths
for (index = 0; index <= sides; index++)
{
cout << "Enter the length of side " << (index + 1) << ": ";
cin >> lengths[index];
{
while (lengths[index] < 0)
{
cout << "Please enter a length greater than zero " << endl;
cin >> lengths[index];
}
}
}
}
// This function sorts the array
void sortArray(double array[], double sides)
{
bool swap; // Swaps two double values
double temp;
do
{
swap = false;
for (int count = 0; count <= (sides - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// This function confirms if the entered lengths form a triangle
void isTriangle(const double lengths[], double sides)
{
for (int i = 0; i <= sides; i++)
{
if (lengths[i] + lengths[i + 1] > lengths[i + 2])
cout << "These 3 sides form a triangle" << endl;
else if (lengths[i] + lengths[i + 2] > lengths[i + 1])
cout << "These 3 sides form a triangle" << endl;
else if (lengths[i + 1] + lengths[i + 2] > lengths[i])
cout << "These 3 sides form a triangle" << endl;
else
cout << "These 3 sides do not form a triangle" << endl;
}
}
// This function tells the user the type of triangle
void equalSides(const double lengths[], double sides)
{
for (int i = 0; i < 3; i++)
{
if (lengths[i] == lengths[i + 1] && lengths[i] == lengths[i + 2])
cout << "This is an equalateral triangle" << endl;
else if (lengths[i] == lengths[i + 1] || lengths[i] == lengths[i + 2])
cout << "This triangle is isosceles" << endl;
else
cout << "This triangle is scalene" << endl;
}
}
// This function tells if the triangle is right, acute, or obtuse
void angleType(double lengths[], double sides)
{
for (int i = 0; i <= sides; i++)
{
if (lengths[i + 2] * lengths[i + 2] == lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
cout << "This is a right triangle" << endl;
else if (lengths[i + 2] * lengths[i + 2] > lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
cout << "This triangle is obtuse" << endl;
else if (lengths[i + 2] * lengths[i + 2] < lengths[i] * lengths[i] + lengths[i + 1] * lengths[i + 1])
cout << "This triangle is acute" << endl;
}
}
| |