Skip to main content

13.5) Exercise 19


In a new .cpp file, write a function called triangle_area that computes the area of a triangle using Heron's formula (see the Wikipedia page if you are unfamiliar with this). Your function should have three parameters of type double to represent the lengths of the triangle's sides. Your function should return the computed area to the caller, as a double value

To compute the square-root, use the standard sqrt function from the C mathematical functions library. You will need #include <cmath> to gain access to this.

Underneath triangle_area, write a main function. The code inside main should test your function by calling it with values for the sides of the triangle that are input by the program user. It should display the value for area returned by the function.

To input the values, either prompt the user to enter each value and use cin to read it or obtain the values via the command line. If you do the latter remember that the command line arguments are C strings; you can convert them to double values by adding #include <cstdlib> to your program and using code like this:

double side1 = atof(argv[1]);