Learning Objectives
• writing a C++ program that does input and output
• using Math Library functions
A triangle is a three-sided polygon. Every triangle has three sides and three
angles, some of which may be the same. Every triangle satisfies the so-called
triangle inequality: the sum of any two sides must be greater than the third
side. Triangles can be classified by the length of their sides or the values of
their angles. The study of triangles is sometimes known as triangle geome-
try, and is of interest to mathematicians and artists interested in generating
oblique shapes composed of triangles. They usually give beautiful results
and unexpected properties. August Leopold Crelle, a nineteenth century
German mathematician, once remarked, “It is indeed wonderful that so sim-
ple a figure as the triangle is so inexhaustible in properties. How many as
yet unknown properties of other figures may there not be?”
Your program will take a set of triples that satisfy the triangle inequality
as input. Your program will not validate the triple to ensure that they do; the
user will do the validation. The program will print the sides of the triangle,
its area and perimeter.
Your program should use the C++ standard Math Library function to
compute the square root in the formula below. Be sure to use the relevant
include directives. Assume that all input values are positive real numbers
that satisfy the triangle inequality.
Relevant Formulas:
Let A, B and C denote the sides of the triangle:
Perimeter = A + B + C
Area = sqrt(S(S − A)(S − B)(S − C)), where S = Perimeter/2
The formula for calculating the area of a triangle is attributed to Heron
of Alexandria. All numeric values in the output should be displayed with
four decimal places and the input and output MUST be formatted as below.
Name your source file triangle.cpp.
- - -- - - - - - - - -- - - - - - - -
Could someone show me how this would look and explain what you did. I am brand new to C++ and am in need of good help. Thank you
This is a kind of homework or something like this. If you are "brand new" to C++ why are you trying to solve a problem like this? I don't think that a teacher would give you a problem like this without learning you the basics of that programming language. Post what have you tried until now.
cout << "This program calculates the area of a triangle";
cout << "What is the length of side A, B, and C, respectively?";
cin >> 'A', 'B', 'C';
cout << "The area of the triangle is " << area << ".\n";
return 0;
}
Well your soft of on the right track but note that a program starts at the first line of main() and will do each calculation in sequence as it comes up. Order is very important. When working with variables, you need to do the following steps in the following order:
1. Define the variable double A;
2. Set the variable A = 5; or cin >> A;
3. Use the variable area = A + ... or cout << A;
You are calculating area without setting the variables.
Also, your variables 'A', 'B' and 'C' are used before they are defined as double. You need to define a variable before you can use it.
Finally, drop the single quotes around A B and C. Those are characters, not variables.
I've added suggestions to your code below to fix the problems;
//Herons Formula
#include <cmath>
#include <iostream>
usingnamespace std;
int main()
{
int area, perimeter, s;
//This block must go after all definitions and cin but before the cout for area.
perimeter=A+B+C; //I dropped the quotes here
s= perimeter/2;
area= sqrt(s*(s-A)*(s-B)*(s-C)); // I dropped the quotes here
//Move this block to the start of main. The variables must be defined before they are set or used.
double s;
double A, B, C; //I dropped the quotes here
double area;
//Move this section to a spot in the function after you've declared A,B,C but before you calculate perimeter, s and area.
cout << "This program calculates the area of a triangle";
cout << "What is the length of side A, B, and C, respectively?";
cin >> A >> B >> C; // Use the >> operator to get several values in a row.
//This part is correct.
cout << "The area of the triangle is " << area << ".\n";
return 0;
}
Code tags are also nice to have when you share code. It makes things more readable.
could you look at this topic and tell me what I am doing wrong. it is more detailed and a little corrected. I think I am almost there: http://cplusplus.com/forum/beginner/60490/