Calculating Triangle Area Using C++ Programming

Click For Summary

Discussion Overview

The discussion revolves around a programming project that requires calculating the area of a triangle using C++. Participants explore the implementation of Heron's formula, the triangle inequality, and the necessary programming constructs to handle user input and error checking.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant suggests using Heron's formula to calculate the area of the triangle and checking the triangle inequality for validity.
  • Another participant expresses familiarity with the formula but not its name, indicating a need for clarification.
  • Several participants discuss the correct declaration of variables and the importance of using double for accuracy in calculations.
  • There is a correction regarding the implementation of Heron's formula, highlighting a mistake in the mathematical expression used.
  • Participants discuss the need to check if the sides form a valid triangle before computing the area, emphasizing the implications of negative values under the radical in the formula.
  • One participant proposes using if-else statements to handle the logic for computing the area or displaying an error message.
  • There are discussions about the placement of output statements based on the validity of the computed value r.
  • Participants express uncertainty about testing the program with negative values to trigger error handling.

Areas of Agreement / Disagreement

Participants generally agree on the use of Heron's formula and the necessity of checking the triangle inequality. However, there are ongoing discussions about specific implementations, variable declarations, and error handling, indicating that the discussion remains unresolved in some areas.

Contextual Notes

There are unresolved issues regarding the correct implementation of Heron's formula and the handling of user input, particularly in ensuring that the program correctly identifies when the sides do not form a valid triangle.

Who May Find This Useful

This discussion may be useful for students learning C++ programming, particularly those working on projects involving geometric calculations and error handling in user input.

  • #31
ineedhelpnow said:
i fixed my last post.

Okay, your logic looks good now...I would compile and run...:D
 
Technology news on Phys.org
  • #32
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?
 
  • #33
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.
 
  • #34
do i replace a number with a negative to test else?
 
  • #35
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").
}
 
  • #36
can u give me an example on how to test it? i want to make sure that it also prints an error statement.
 
  • #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]count[/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]count[/m] to see if your syntax is correct?
 
  • #51
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!
 
  • #52
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)
 
  • #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.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
Replies
2
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K
  • · Replies 9 ·
Replies
9
Views
2K