C/C++ Compute the area of a triangle c++

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Area C++ Triangle
AI Thread Summary
The discussion focuses on computing the area of a triangle using the lengths of its sides and validating the input to ensure they can form a triangle. Key points include the necessity of checking that all sides are positive and that the sum of any two sides must be greater than the third, which is essential for triangle validity. The initial code provided has issues, particularly with the placement of the area calculation and error handling. Participants emphasize the importance of validating inputs before calculating the area to avoid runtime errors, particularly when the triangle inequality is not satisfied. Suggestions include using a while loop to repeatedly prompt the user for valid side lengths until they meet the criteria. Additionally, informative error messages are recommended to specify the nature of the input error, enhancing user experience. The final code iteration incorporates these suggestions, ensuring that the area is only calculated after confirming valid input.
ineedhelpnow
Messages
649
Reaction score
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
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.
 
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.
 
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.
 
how would i check to see the biggest side?
 
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]?
 
Because my teacher isn't as kind as you :o
 
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.
 
ineedhelpnow said:
Because my teacher isn't as kind as you :o

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:

$$s(s-a)(s-b)(s-c)>0$$

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

$$\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$$

$$\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$$

$$\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:

$$(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: 93
  • mid2.png
    mid2.png
    5.6 KB · Views: 101

Similar threads

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