C/C++ Calculating Triangle Area Using C++ Programming

AI Thread Summary
The discussion revolves around a programming project in C++ that requires calculating the area of a triangle based on three user-provided side lengths. The main focus is on using Heron's formula for area calculation and ensuring the validity of the triangle using the triangle inequality theorem. Participants emphasize the importance of checking if the sum of the two smaller sides is greater than the largest side to confirm that a valid triangle can be formed. Several coding issues are addressed, including the correct implementation of Heron's formula, variable declaration, and the need to compute the area only if the radicand (r) is positive. The conversation also touches on debugging, with suggestions to handle error messages and ensure the program does not close unexpectedly. Ultimately, the user learns to implement conditional statements effectively and understands the necessity of fulfilling both the area calculation and triangle validity conditions as part of the assignment requirements.
ineedhelpnow
Messages
649
Reaction score
0
Hi everyone (Wave)
In this project, given three sides from users, the area of a triangle is required to be printed out. If the three sides do not form a valid triangle, an error message should be displayed. Google for the formula and call math functions to calculate the area. Check the attached photo for classroom discussion.

ok so that's my project for intro to programming (c++). i have no idea where to begin. any ideas?
 
Technology news on Phys.org
Since you are given the sides of the triangle as input, I would look into Heron's formula for the area of a triangle to compute the output. I would look into the triangle inequality for a check on the validity of the input. :D
 
:confused: (Speechless)
 
im familiar with the formula but i didnt know it was called heron's formula (Blush). writing up a program that does that should be real simple right?
 
ineedhelpnow said:
im familiar with the formula but i didnt know it was called heron's formula (Blush). writing up a program that does that should be real simple right?

Yes, it should be pretty straightforward.

You want to assure that the sum of the two smaller values is larger than the largest value.

Once this is done, then define a variable to hold the value of the semi-perimeter and use Heron's formula to compute the area of the triangle.
 
ok so i need to define the sides
double side1=0
double side2=0
double side3=0

s= (side1+side2+side3) / 2
A= sqrt(s*(s-side1)+s*(s-side2)+s*(s-side3))

do i need to identify s and A? i know i have a mistake (mistakeS).

- - - Updated - - -

oops the first one should be double side1. and also how do i test the program using the sides 3.0,4.0,5.0
 
Last edited:
1.) Why have you declared side1 to be a character?

2.) Check your implementation of Heron's formula...

3.) If the triangle inequality is violated, what will happen to the value under the radical?
 
It's been quite a while since I have used C (and its variants), but I would think you could declare the variables without initializing them, e.g.:

double side1, side2, side3, s, A;
 
  • #10
he says we have to list them. do i use double or int?
 
  • #11
ineedhelpnow said:
he says we have to list them. do i use double or int?

By list them, do you mean declare them separately and initialize each to zero?

I would use double over int so that your results are more accurate.
 
  • #12
yes

- - - Updated - - -

should i use a, b, c instead?
 
  • #13
ineedhelpnow said:
yes

- - - Updated - - -

should i use a, b, c instead?

As the coder, you have the freedom to chose your variable names, but I would use a, b, and c just for brevity.

Have you spotted the error in your implementation of Heron's formula and realized what will happen if the 3 sides will not form a triangle?
 
  • #14
i still don't see what i did wrong.
 
  • #15
ineedhelpnow said:
i still don't see what i did wrong.

Heron's formula:

$$A=\sqrt{s(s-a)(s-b)(s-c)}$$

Do you see that you added instead of multiplying? :D
 
  • #16
(Blush) i made an oopsie

- - - Updated - - -

Code:
#include <iostream>
using namespace std;

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

is this right so far?
 
  • #17
Code:
#include iostream
using namespace std;

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

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

cout << "The area of a trianle with three given sides is " << areaofTriangle << endl;

return 0;
}
 
Last edited:
  • #18
ineedhelpnow said:
Code:
#include iostream
using namespace std;

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

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

Your area formula still isn't right...you have too many s's and we still need to catch the cases where a, b, and c will not form a triangle. :D
 
  • #19
fixed it. look at the post right above your last one
 
  • #20
ineedhelpnow said:
fixed it. look at the post right above your last one

Okay good!

Now, let's suppose $a$ is the largest side. Let's also suppose that $b+c<a$. What then happens to the factor:

$$s-a$$?
 
  • #21
not sure but i just noticed that i left out #include <cmath> otherwise it can't execute the statement because of "sqrt"
 
  • #22
Well, let's take a look see:

$$s-a=\frac{a+b+c}{2}-a=\frac{b+c-a}{2}$$

Now, since we have $$a>b+c$$, then this factor is negative, and all other factors will be postive, which will result in a negative under the radical.

So, to ensure than we have a valid and non-degenerate triangle, we must ensure that the value under the radical is positive. You may want to define another variable, we could call it $r$ for radicand, and then if $r>0$ compute and display the area [m]A = sqrt(r);[/m], otherwise display an error message. :D
 
  • #23
i figured that I am supposed to use if and else statements but i don't really know when
 
  • #24
Okay, let's say you've computed $r$ as follows:

Code:
r = s*(s - a)*(s - b)*(s - c)

Then we could use a construct something like:

Code:
if (0 < r)
{
    areaofTriangle = sqrt(r);
    //display computed area.
}
else
{
    //display error message.
}

This way, as long as $r$ is positive, we compute and display the area, otherwise if $r$ is not positive, then we display an error message.
 
  • #25
i thought // was just as a note for the programmer/coder. i didnt know it actually commanded the program to do something.
 
  • #26
ineedhelpnow said:
i thought // was just as a note for the programmer/coder. i didnt know it actually commanded the program to do something.

Yes, in javascript it is also for a comment or remark. I am leaving the actual statements for displaying the messages to you. I just wanted to give you the general logic of the if-else construct. :D
 
  • #27
ineedhelpnow said:
i thought // was just as a note for the programmer/coder. i didnt know it actually commanded the program to do something.

It doesn't. These are just placeholders for your own code, computers can't understand written english just yet :)

so get coding!
 
  • #28
had a lot of other homework to do but i FINALLY got back to this. how does it look so far?
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=3.0;
b=4.0;
c=5.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";
}

return 0;
}
 
Last edited:
  • #29
I would wait until you have determined whether $r$ is positive before computing the area, otherwise you could trigger an intrinsic domain error.

Put the statement that displays the area of the triangle in the block of code that is to be executed when $r$ is found to be positive, and then you will need an error message in the [m]else{}[/m] block, where $r$ is found to be non-positive.
 
  • #30
i fixed my last post.
 
  • #31
ineedhelpnow said:
i fixed my last post.

Okay, your logic looks good now...I would compile and run...:D
 
  • #32
fixed it. ok so do i move the cout 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 cout 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]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?
 
Back
Top