Calculating Triangle Area Using C++ Programming

In summary: If the value under the radical is negative, then the triangle will not be valid and an error message should be displayed. In summary, to compute the area of a triangle, Heron's formula must be used along with the triangle inequality.
  • #36
can u give me an example on how to test it? i want to make sure that it also prints an error statement.
 
Technology news on Phys.org
  • #37
ineedhelpnow said:
can u give me an example on how to test it? i want to make sure that it also prints an error statement.

The if statement is already testing $r$ to see if it is positive. We require $r$ to be positive in order to have a valid triangle.

If you want to make certain that the error message is displayed, then let:

Code:
a = 1.0
b = 1.0
c = 3.0

This should trigger an error because $r$ will be negative.
 
  • #38
when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(
 
  • #39
ineedhelpnow said:
when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(

When the input is valid, does the output window stay open?
 
  • #40
yes but the window should only close if their is something wrong with the program. now it won't even print error.
 
  • #41
ineedhelpnow said:
yes but the window should only close if their is something wrong with the program. now it won't even print error.

The only thing I can suggest is to make sure you are using [m]cout[/m] correctly by reading the documentation provided...I am not familiar with this method of output.
 
  • #42
is it possible that the cin statement is causing a problem?

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
areaofTriangle=sqrt(r);

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

- - - Updated - - -

so i definitely don't need to use && commands for the if statement?
 
  • #43
Don't calculate the square root until you're sure that $r$ is positive.
 
  • #44
ineedhelpnow said:
is it possible that the cin statement is causing a problem?

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));
areaofTriangle=sqrt(r);

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

- - - Updated - - -

so i definitely don't need to use && commands for the if statement?

The error is that you are computing the area of the triangle before testing the value of $r$. :D

Try:

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}
 
  • #45
thats why i have if (r>0)

How do i test the value of r?
 
  • #46
ineedhelpnow said:
thats why i have if (r>0)

How do i test the value of r?

The if statement is testing $r$ to see if it is positive...
 
  • #47
MarkFL said:
The error is that you are computing the area of the triangle before testing the value of $r$. :D

Try:

Code:
#include <iostream>
#include <cmath>
using namspace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
}

return 0;
}

what did you do different? (mark you HAVE to get rid of the time gap between posts)

- - - Updated - - -

oh i see. well its still closing the window immediately
 
  • #48
ineedhelpnow said:
what did you do different? (mark you HAVE to get rid of the time gap between posts)

I removed the statement computing the area of the triangle before the if-else construct. We only want to compute the area once we are certain that the radicand $r$ is positive.

Flood control is a standard feature on all forums that I have ever been a part of. It is there to prevent users from flooding a thread or forum with posts made in rapid succession. :D
 
  • #49
i removed that statement and its STILL not working (ps i appreciate all your help)

(flood control is driving me nuts. just let the flood be (Dull))
 
  • #50
ineedhelpnow said:
i removed that statement and its STILL not working (ps i appreciate all your help)

Okay, did you check the provided documentation on the output statement [m]cout[/m] to see if your syntax is correct?
 
  • #51
i didnt see a document but I am pretty sure I am doing the cout statements correctly. buuuuuuuut anyways I GOT IT I GOT IT I GOT IT I GOT IT! i needed a cin statement for the else command. thank u!
 
  • #52
ineedhelpnow said:
i didnt see a document but I am pretty sure I am doing the cout statements correctly. buuuuuuuut anyways I GOT IT I GOT IT I GOT IT I GOT IT! i needed a cin statement for the else command. thank u!

That was actually going to be my next suggestion...:D

Glad you got it working correctly! (Yes)
 
  • #53
MarkFL said:
That was actually going to be my next suggestion...:D
:rolleyes: sure it was

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main () {
     double a=0.0;
     double b=0.0;
     double c=0.0;
     double s=0.0;
     double r=0.0;
     double areaofTriangle=0.0;

a=1.0;
b=1.0;
c=3.0;
s=(a+b+c) / 2;
r=s*((s-a)*(s-b)*(s-c));

if (r>0) {
       areaofTriangle = sqrt(r);
       cout << "The area of a triangle with the three given sides " << a << ", " << b << ", " << c << " is " << areaofTriangle << "." << endl; 
       cin >> areaofTriangle;
}
else {
       cout << "Error." << endl;
       cin >> areaofTriangle;
}

return 0;
}
 
  • #54
i actually didnt meet one of the conditions. that two of the sides has to be greater than one of them. or something like that...
 
  • #55
ineedhelpnow said:
i actually didnt meet one of the conditions. that two of the sides has to be greater than one of them. or something like that...

The condition that [m]r[/m] must be positive is equivalent to satisfying the triangle inequality. :D
 
  • #56
(Shake) he failed me on the assignment because i missed that second condition. he's giving a second chance though. also in my program, i stored the variables a b and c which was wrong.
 
  • #57
ineedhelpnow said:
(Shake) he failed me on the assignment because i missed that second condition. he's giving a second chance though. also in my program, i stored the variables a b and c which was wrong.

If it were me, I would present to my professor an algebraic argument demonstrating that the positive value of [m]r[/m] is in fact equivalent to the triangle inequality.
 
  • #58
yeah i could present him with a present lol i don't think he would care. he told me that i had to satisfy both conditions. i don't understand why because the program would still run properly once i get rid of what i stored for a,b, and c but he said i have to put that second condition.
 
  • #59
ineedhelpnow said:
yeah i could present him with a present lol i don't think he would care. he told me that i had to satisfy both conditions. i don't understand why because the program would still run properly once i get rid of what i stored for a,b, and c but he said i have to put that second condition.

I would still make such an argument, but this is up to you. The way you have it coded now is much more efficient than having to determine which is the largest of the 3 values, and then make certain that the sum of the other two sides is greater than it. But, if in the end your professor is adamant and unmoved by a well-reasoned argument, you must do what you must do.
 
  • #60
he wants us to fulfill the second condition because he wants us to be able to code it properly. to have us use all the different types of commands and functions.
 
  • #61
ineedhelpnow said:
he wants us to fulfill the second condition because he wants us to be able to code it properly. to have us use all the different types of commands and functions.

You are fulfilling the triangle inequality, with one statement, but you know your professor better than I do, so do what you think is best. :D
 

Similar threads

Replies
15
Views
3K
Replies
12
Views
3K
Replies
4
Views
2K
Replies
13
Views
5K
Replies
3
Views
5K
Back
Top