Herons Formula Help!!

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.
So far we have learned iomanip (???), setw, setprecision, fixed, sqrt, pow, abs, include <cmath>
All you need is to code the input and output. You have the formula above.

Get input from the user;
Use formula with users input;
Output formula product;
I really dont know where to start I know to uses the following

#include <cmath>
int main()
{
using namespace std;




return 0;
}
I dont know what goes in between and it would be awesome if you could explain plz
Sorry, I don't do peoples homework!

Look through the tutorials on this site and learn basic input/output. You have everything else you need in the first post!

http://www.cplusplus.com/doc/tutorial/basic_io/
im not asking you too. I think what im trying to say is that I need an explantion as to how to do this. Ill do the coding myself
Ask the user for input:
cout << "message...";

Get input for three sides of the triangle:
cin >> variable_1 >> variable_2 >> variable_3;

Add the 3 user inputs to get the perimeter;
Use the above formula to calculate area;

Output area:
cout << "Area is: " << area << endl;
how do we define area as a variable
ok im up to par with this part but how do we input the formula, I know its different in C++ than in regular algebra
Here is what I have so far. However it keeps saying its wrong????



//Herons Formula
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
int area, perimeter, s;

perimeter='A'+'B'+'C';
s= perimeter/2;
area= sqrt(s*(s-'A')*(s-'B')*(s-'C'));

double s;
double 'A', 'B', 'C';
double 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';
cout << "The area of the triangle is " << area << ".\n";
return 0;
}
A,B and C are variable names (identifiers) the same as area, perimeter and s, so do not put single quotes (') around them.
Last edited on
JCharles, a previous quote from you:
So far we have learned iomanip (???), setw, setprecision, fixed, sqrt, pow, abs, include <cmath>


Why would your teacher teach you this when you hasn't even taught you basic input/output?
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;
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
//Herons Formula
#include <cmath>
#include <iostream>
using namespace 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.

I hope this helps you to understand,
Last edited on
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/
Topic archived. No new replies allowed.