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.
  • #1
ineedhelpnow
651
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
  • #2
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
 
  • #3
:confused: (Speechless)
 
  • #5
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?
 
  • #6
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.
 
  • #7
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:
  • #8
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?
 
  • #9
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:

\(\displaystyle 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:

\(\displaystyle 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:

\(\displaystyle s-a=\frac{a+b+c}{2}-a=\frac{b+c-a}{2}\)

Now, since we have \(\displaystyle 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").
}
 

Similar threads

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