Compute the area of a triangle c++

In summary, to make a valid triangle, all sides have to be positive and the sum of any two sides must be greater than the third.add a while loop so that if the sides are invalid the user is asked to re enter the sides.
  • #1
ineedhelpnow
651
0
compute the area of a triangle. write an if/else statement to check if the three sides provide a valid triangle. to make a valid triangle, all sides have to be positive and the sum of any two sides must be greater than the third. add a while loop so that if the sides are invalid the user is asked to re enter the sides.

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main() {
double a=0,b=0,c=0;
cin >>a>>b>>c;
double A=0;
double s=0;
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));

if(((a>0)&&(b>0)&&(c>0))&&(((a+b)>c)&&((a+c)>b)&&((b+c)>a))) {
    while(!(a<=0||b<=0||c<=0||(a+b)<=0||(a+c)<=0||(b+c)<=0))
    cout << A;
}
else
cout << "Error.";

}

this is what i wrote. is it correct? i compiled it on this site Compile and Execute C++ online i don't know if you can see my code if not you can just copy and paste it there. at the bottom of the page it says STDIN Input. you can enter sides to test it. just leave a space between them (i.e. 3 8 -2). this question will be brought on our midterm so i want to make sure i did it absolutely correct now and that no errors slip past me.
 
Last edited:
Technology news on Phys.org
  • #2
It appears to me that you will always receive an error because you will never have:

[m]((a + b) > c) && ((a + c) > b) && ((b + c) > a))[/m]

You only need to test if [m]A > 0[/m] to satisfy the strict triangle inequality.
 
  • #3
the reason why i put that is because 2 sides have to be greater than one. i put in different inputs at the bottom and it seemed to work but i want to make sure i didnt skip anything.
 
  • #4
ineedhelpnow said:
the reason why i put that is because 2 sides have to be greater than one. i put in different inputs at the bottom and it seemed to work but i want to make sure i didnt skip anything.

The two smaller sides have to be greater than the largest side. When [m]A > 0[/m], this will be true. Otherwise you will need to determine which is the largest side, and then make sure the sum of the other two sides it greater than it is.
 
  • #5
how would i check to see the biggest side?
 
  • #6
ineedhelpnow said:
how would i check to see the biggest side?

Why would you do that when all you need to do is test to see if [m]A>0[/m]?
 
  • #7
Because my teacher isn't as kind as you :eek:
 
  • #8
First, you should write the complete problem statement. I see that you tried a negative number as a side length, so I assume your program has to take into account that input numbers may not represent side lengths of an actual triangle. A program that tests for such inputs is different from a program that expects correct lengths. What checks exactly do you need to make? Also, you should write the problem statement in the body and not in the title as described in rule #10 http://mathhelpboards.com/rules/.

It is not clear to me why you used the [m]while[/m] loop. This loop is used when some calculation have to be done repeatedly (and usually when the number of iterations is not known in advance). In this problem, there are no iterations; the number of operations it takes to find the area is known in advance.

You should test whether the triangle inequality holds before taking the square root in the calculation of $A$. For example, if $a=1$, $b=2$ and $c=5$ we have that $a+b>c$ does not hold, but $a+c>b$ and $b+c>a$ do hold. Meanwhile,
\[
s-c=\frac{a+b+c}{2}-c=\frac{a+b-c}{2}<0
\]
for these $a$, $b$ and $c$. Similar calculations show that $s-a>0$ and $s-b>0$, so $s(s-a)(s-b)(s-c)<0$ and taking the square root will through an error. Checking that the triangle inequalities hold will guarantee that the expression under the square root is positive.

As a final remark, it is nice to write what exactly is wrong with inputs instead of just printing "Error.". Is one of the sides negative? Is the triangle inequality violated? It may seem like a hassle, but printing informative error messages is a very valuable programming habit.

MarkFL said:
It appears to me that you will always receive an error because you will never have:

[m]((a + b) > c) && ((a + c) > b) && ((b + c) > a))[/m]
Doesn't this holds when $a$, $b$ and $c$ are side lengths of a real triangle?

MarkFL said:
You only need to test if [m]A > 0[/m] to satisfy the strict triangle inequality.
Computing $A$ may produce an error as described above.
 
  • #9
ineedhelpnow said:
Because my teacher isn't as kind as you :eek:

While I am flattered, it really isn't a matter of kindness, but rather of efficiency of coding. Also, I made a boo-boo, we want to make sure the radicand in [m]A[/m] is greater than zero.

Let's look at:

\(\displaystyle s(s-a)(s-b)(s-c)>0\)

Since \(\displaystyle s=\frac{a+b+c}{2}\), this becomes:

\(\displaystyle \frac{a+b+c}{2}\left(\frac{a+b+c}{2}-a\right)\left(\frac{a+b+c}{2}-b\right)\left(\frac{a+b+c}{2}-c\right)>0\)

\(\displaystyle \frac{a+b+c}{2}\left(\frac{b+c-a}{2}\right)\left(\frac{a+c-b}{2}\right)\left(\frac{a+b-c}{2}\right)>0\)

\(\displaystyle \frac{1}{16}(a+b+c)(a+b-c)(a+c-b)(b+c-a)>0\)

Now, you have checked to make certain that [m](a > 0) && (b > 0) && (c > 0)[/m] so we only need:

\(\displaystyle (a+b-c)(a+c-b)(b+c-a)>0\)

And so, I was completely wrong when I first looked at your code, you are in fact testing the right conditions. (Wasntme)

But, I still think it is more efficient to write something like:

Code:
s = (a + b + c)/2;
r = s*(s - a)*(s - b)*(s - c);
if ((a > 0) && (b > 0) && (c > 0) && (r > 0))
{
    A = sqrt(r);
    cout << A;
}
else
{
    cout << "Error.";
}

You see, this way, we only invoke the [m]sqrt()[/m] function once we are sure an internal error won't be thrown by potentially trying to use a radicand that is not positive.
 
  • #10
Evgeny.Makarov said:
First, you should write the complete problem statement. I see that you tried a negative number as a side length, so I assume your program has to take into account that input numbers may not represent side lengths of an actual triangle. A program that tests for such inputs is different from a program that expects correct lengths. What checks exactly do you need to make? Also, you should write the problem statement in the body and not in the title as described in rule #10 http://mathhelpboards.com/rules/.

It is not clear to me why you used the [m]while[/m] loop. This loop is used when some calculation have to be done repeatedly (and usually when the number of iterations is not known in advance). In this problem, there are no iterations; the number of operations it takes to find the area is known in advance.

You should test whether the triangle inequality holds before taking the square root in the calculation of $A$. For example, if $a=1$, $b=2$ and $c=5$ we have that $a+b>c$ does not hold, but $a+c>b$ and $b+c>a$ do hold. Meanwhile,
\[
s-c=\frac{a+b+c}{2}-c=\frac{a+b-c}{2}<0
\]
for these $a$, $b$ and $c$. Similar calculations show that $s-a>0$ and $s-b>0$, so $s(s-a)(s-b)(s-c)<0$ and taking the square root will through an error. Checking that the triangle inequalities hold will guarantee that the expression under the square root is positive.

As a final remark, it is nice to write what exactly is wrong with inputs instead of just printing "Error.". Is one of the sides negative? Is the triangle inequality violated? It may seem like a hassle, but printing informative error messages is a very valuable programming habit.

Doesn't this holds when $a$, $b$ and $c$ are side lengths of a real triangle?

Computing $A$ may produce an error as described above.
ill fix the title. my instructor says we have to use a while loop so that if the input sides are invalid the user is asked to re enter the sides until they are valid. (i know i missed that part where i ask the user the re enter sides)
 
  • #11
i fixed the title. I am going to add something to the while loop now that i missed.
 
  • #12
I would actually use something like:

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main() {
double a = 0, b = 0, c = 0, A = 0, s = 0, r = 0;
bool quit = false;
while (!quit)
{
    cout << "Enter the three side lengths of a triangle:";
    cin >> a >> b >> c;
    s = (a + b + c)/2;
    r = s*(s - a)*(s - b)*(s - c));
    if ((a > 0) && (b > 0) && (c > 0) && (r > 0))
    {
        quit = true;
    }
    else
    {
        cout << "Error: The sides entered do not represent a valid triangle";
    }
}

A = sqrt(r);
cout <<  "The area of the triangle is: " << A;

}
 
  • #13

Attachments

  • mid1.png
    mid1.png
    10.7 KB · Views: 64
  • mid2.png
    mid2.png
    5.6 KB · Views: 72

FAQ: Compute the area of a triangle c++

What is the formula for computing the area of a triangle in C++?

The formula for computing the area of a triangle in C++ is (base * height) / 2. This can also be written as (0.5 * base * height).

How do I input the base and height values for the triangle in my C++ program?

You can use the cin function in C++ to prompt the user for input and store the values in variables. For example, you can use "cout" to ask the user to input the base and height values, and then use "cin" to store those values in variables to use in your formula.

What data types should I use for the base and height values in my C++ program?

The base and height values for a triangle can be represented as either floating-point numbers (decimal numbers) or integers. It is up to you to choose which data type is most appropriate for your program and the level of precision you need for your calculations.

Can I compute the area of a triangle with negative or zero values for the base and height?

No, the base and height values for a triangle must be positive numbers in order to accurately compute the area. Negative or zero values will result in an incorrect area calculation.

Is there a library function in C++ that can be used to compute the area of a triangle?

No, there is not a specific library function in C++ for computing the area of a triangle. However, you can use the pow() function from the library to calculate the exponent of a value, which may be useful in your area calculation formula.

Similar threads

Replies
5
Views
2K
Replies
22
Views
3K
Replies
15
Views
3K
Replies
6
Views
10K
Replies
39
Views
4K
Replies
17
Views
2K
Replies
1
Views
1K
Back
Top