1Write a C program that asks the user for the lengths of the
1)Write a C++ program that asks the user for the lengths of the three sides of a triangle, determine whether the input is valid, and if it is, display the area of the triangle. There are three required functions you have to create to help streamline the mainline code: a) The getDouble function must: i)Accept two arguments: a double minimum value, and a double maximum value. ii)Prompt the user to enter a value iii)Get a double value from the user and display an error message if the value the user entered was out of range. iv)Continue prompting for input and accepting input until the input is valid. Once a valid double has been entered, return that double value. b) The isValidTriangle function must: i)Accept three arguments. All three are doubles representing the three lengths of the sides of a triangle. ii)Return true if the sides represent a valid triangle, and false otherwise. c)The calcArea function must: i) Accept three arguments (all doubles representing the three side lengths). ii)Use the isValidTriangle function to determine if the triangle is valid. If the triangle is not valid, then return -1. iii)If the triangle is valid then return the area of the triangle. The area of the triangle can be calculated using Heron\'s formula: area = (s (s - a) (s - b) (s - c)) ^ 0.5 d) The main function must: i)Display an introductory message: \"This program calculates the area of triangle\" ii)Use the getDouble function to get the lengths (between 1 and 100) of the three sides from the user. This will require three calls to getDouble. iii)Use the calcArea function to get the area of the triangle. If calcArea returns a negative value for the area, then that indicates the triangle is invalid. If the triangle is invalid, then display an error message; otherwise display the area with one digit after the decimal point. Sample run: *** first sample run *** This program calculates the area of triangle Enter length of first side (1-100): 3 Enter length of second side (1-100): 4 Enter length of third side (1-100): 20 That is not a valid triangle *** second sample run *** This program calculates the area of triangle Enter length of first side (1-100): 3 Enter length of second side (1-100): 4 Enter length of third side (1-100): 5 The area of the triangle is 6.0 *** third sample run *** This program calculates the area of triangle Enter length of first side (1-100): 5 Enter length of second side (1-100): 4 Enter length of third side (1-100): 3 The area of the triangle is 6.0 *** fourth sample run *** This program calculates the area of triangle Enter length of first side (1-100): -3 Error: Number below minimum value Enter length of first side (1-100): 300 Error: Number above maximum value Enter length of first side (1-100): 3 Enter length of second side (1-100): 5 Enter length of third side (1-100): 4 The area of the triangle is 6.0 *** fifth sample run *** This program calculates the area of triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 30 Enter length of third side (1-100): 15 That is not a valid triangle *** sixth sample run *** This program calculates the area of triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 20 Enter length of third side (1-100): 30 That is not a valid triangle *** seventh sample run *** This program calculates the area of triangle Enter length of first side (1-100): 10 Enter length of second side (1-100): 21 Enter length of third side (1-100): 30 The area of the triangle is 54.5
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <math.h>
using namespace std;
double getDouble (double min, double max); // function declarations
bool isValidTriangle (double a, double b, double c);
double calcArea (double a, double b, double c);
double getDouble (double min, double max) { // function that returns the value entered by the user
double a;
cout << \"Enter a length of the side (1 - 100) : \"; // prompt for the user to enter the data
cin >> a; // get the data
while ((a < max) || (a > min)) { // check for the data and iterate
if(a > max) { // if the data is greater
cout << \"Error: Number above maximum value.\" << endl; // print the message
cout << \"Enter a length of the side (1 - 100) : \"; // reprompt
cin >> a;
} else if (a < min) { // if the data is min to lesser
cout << \"Error: Number below minimum value.\" << endl; // print the message
cout << \"Enter a length of the side (1 - 100) : \"; // reprompt
cin >> a;
} else { // else
break; // break the loop
}
}
return a; // return the value
}
bool isValidTriangle (double a, double b, double c) { // function to check if the entered data is a valid or not
if((a + b > c) && (a + c > b) && (b + c > a)) { // condition to check the validations
return true; // return the values
} else {
return false;
}
}
double calcArea (double a, double b, double c) { // function to calculates the area of the triangle
double s = 0, area = 0; // required initialisations
bool valid = isValidTriangle (a, b, c); // call the function to check for the validations
if(valid == true) { // based on the return value0
s = (a + b + c) / 2.0; // calculates the area based on the henry\'s formulae
area = (sqrt)(s * (s - a) * (s - b) * (s - c));
return area; // return the area
} else {
return -1; // else return the -1
}
}
int main() // driver method
{
double res = 0; // local variables
cout << \"\ This program calculates the area of triangle\" << endl; // message about the code
double a = getDouble(1, 100); // call the functions to get the side values
double b = getDouble(1, 100);
double c = getDouble(1, 100);
res = calcArea(a, b, c); // getting the result value after calculations
if(res == -1) {
cout << \"That is not a valid triangle.!\" << endl; // message for a invalid triangle
} else {
cout << \"The area of the triangle is : \" << res << endl; // message about the result
}
}
OUTPUT :
Case 1 :
This program calculates the area of triangle
Enter a length of the side (1 - 100) : 3
Enter a length of the side (1 - 100) : 4
Enter a length of the side (1 - 100) : 20
That is not a valid triangle.!
Case 2 :
This program calculates the area of triangle
Enter a length of the side (1 - 100) : 3
Enter a length of the side (1 - 100) : 4
Enter a length of the side (1 - 100) : 5
The area of the triangle is : 6
Case 3 :
This program calculates the area of triangle
Enter a length of the side (1 - 100) : -3
Error: Number below minimum value.
Enter a length of the side (1 - 100) : 300
Error: Number above maximum value.
Enter a length of the side (1 - 100) : 3
Enter a length of the side (1 - 100) : 5
Enter a length of the side (1 - 100) : 4
The area of the triangle is : 6
Case 4 :
This program calculates the area of triangle
Enter a length of the side (1 - 100) : 10
Enter a length of the side (1 - 100) : 21
Enter a length of the side (1 - 100) : 30
The area of the triangle is : 54.4971
Hope this is helpful.


