Calculating Triangle Area Using C++ Programming

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
60 replies · 9K views
ineedhelpnow said:
i fixed my last post.

Okay, your logic looks good now...I would compile and run...:D
 
Physics news on Phys.org
fixed it. ok so do i move the count statement to the if {} part or do i put it at the bottom of the program?
 
ineedhelpnow said:
fixed it. ok so do i move the count statement to the if {} part or do i put it at the bottom of the program?

You want a different output depending on whether $r$ is positive or not, so it appears to me that they are in the right place.
 
do i replace a number with a negative to test else?
 
ineedhelpnow said:
do i replace a number with a negative to test else?

You are checking to see if $r$ is positive, and if it is, then you are computing and displaying the area of the triangle. If $r$ is not positive, then the code in the else block is executed, displaying the error message.

Code:
if (0 < r)
{
    //code that executes if r is positive (if the condition 0 < r returns "true").
}
else
{
    //code that executes if r is not positive (if the condition 0 < r returns "false").
}
 
can u give me an example on how to test it? i want to make sure that it also prints an error statement.
 
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.
 
when i put those numbers in instead, the screen that displays the statement opens for like half a second then closes immediately :(
 
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?
 
yes but the window should only close if their is something wrong with the program. now it won't even print error.
 
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]count[/m] correctly by reading the documentation provided...I am not familiar with this method of output.
 
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?
 
Don't calculate the square root until you're sure that $r$ is positive.
 
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;
}
 
thats why i have if (r>0)

How do i test the value of r?
 
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...
 
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
 
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
 
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))
 
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]count[/m] to see if your syntax is correct?
 
i didnt see a document but I am pretty sure I am doing the count 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!
 
ineedhelpnow said:
i didnt see a document but I am pretty sure I am doing the count 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)
 
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;
}
 
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...
 
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
 
(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.
 
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.
 
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.
 
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.
 
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.