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.
  • #51
i didnt see a document but I am pretty sure I am doing the cout 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!
 
Technology news on Phys.org
  • #52
ineedhelpnow said:
i didnt see a document but I am pretty sure I am doing the cout 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.
 
  • #61
ineedhelpnow said:
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.

You are fulfilling the triangle inequality, with one statement, but you know your professor better than I do, so do what you think is best. :D
 
Back
Top