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

Click For 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.
 

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
7K
  • · Replies 9 ·
Replies
9
Views
2K