### Calculating area of triangle of sides a, b with angle C between them

My second program in c++ (1st being hello world)
Trying to use the trig formulae ( area of triangle = absin(C) )
However getting odd results and not sure why.

 ``12345678910111213141516171819202122232425`` `````` #include #include #define PI 3.14159265 double areaoftriangle(double a, double b, double c) { double c1 = PI/180; double c2 = c1 * c; double b1 = 0.5 * b; double s = sin (c2); return s * b1 * a ; } int main () { using namespace std; cout << "area of triangle of sides a, b with angle c inbetween them" << endl << "enter a b c " << endl; int a3,b3,c3; cin >> a3, b3,c3; cout << "area of the triangle is " << areaoftriangle(a3,b3,c3); return 0; }``````
a3,b3,c3 are declared as ints but are passed as doubles and used as doubles which probably the problem.

While this is a preference thing, but you could compress a lot of your codes in your function, but, I don't think they are the problem.
changed the code so int passed as int but getting negative results for the area of a triangle in the hundreds for a triangle 3, 7 35 degrees.
 ``12345678910111213141516171819202122232425`` ``````#include #include #define PI 3.14159265 double areaoftriangle(int a, int b, int c) // now int { double c1 = PI/180; double c2 = c1 * c; double b1 = 0.5 * b; double s = sin (c2); return s * b1 * a ; } int main () { using namespace std; cout << "area of triangle of sides a, b with angle c inbetween them" << endl << "enter a b c " << endl; int a3,b3,c3; cin >> a3, b3,c3; cout << "area of the triangle is " << areaoftriangle(a3,b3,c3); return 0; }``````
Last edited on
the answer should be 6.0225 but it is showing up -563.961
found the aswer, and it's a little complicated. Link to the problem and code to solve it looks like you have to go into the steps involved in creating the formulea and then writing the code step by step. http://www.cprogramto.com/c-program-angle-side-sas-known/
`cin >> a3, b3,c3; ` this does not do what you think it does.

You have to use `>>` not commas.

`cin >> a3 >> b3 >> c3;` should be the correct way.

otherwise what you are doing is the same as:
 ``123`` ``````cin >> a3; b3; c3;``````

which would be the same as:

`cin >> a3;`

Also you can pass an int to a function that wants a double it will implictly convert it. Another problem you have is integer division which will probably cause you to always have radians of 0. integer division floors it down.

So try any of these:

 ``12345`` ``````180. 180.0 180f 180.f 180.0f``````

*edit brain fart wasn't thinking about pi being a double.
Last edited on
it's now half past midnight so ill re write the code when i wake up this morning.
The program shows correct results now the correct doubles are put in thank you so much.
 ``12345678910111213141516171819202122232425`` ``````#include #include #define PI 3.14159265 double areaoftriangle(double a, double b, double c) { double c1 = PI/180.0; // sorry forgot to add the .0 double c2 = c1 * c; double b1 = 0.5 * b; double s = sin (c2); return s * b1 * a ; } int main () { using namespace std; cout << "area of triangle of sides a, b with angle c inbetween them" << endl << "enter a b c " << endl; double a3,b3,c3; cin >> a3 >> b3 >> c3; cout << "area of the triangle is " << areaoftriangle(a3,b3,c3); return 0; }``````
Last edited on
Hi Jordy,

line 1: use `<cmath>` not math.h

line 3: Prefer to use a const variable rather than a #define. Put `const double PI = 3.14159265;` in main. PI / 180.0 is a constant value too, you can use `constexpr` in C++11. The `cmath` header might have an `M_PI` already defined, but this is not standard.

line 6: Prefer to declare functions before main, then provide a definition after main.

line 19: delete this line. Put `std::` before each `std` `namespace` thing -` std::cin` , `std::cout` , `std::endl` etc. Ideally, you should have you code in your own namespace. Worthwhile reading up about namespaces.

Variable names : Make them more meaningful, for example `SideA`, `SideB`, `AngleC` . This is a preference thing, it is common to use single variable names if that is what is in the documentation - if so put the documentation in as comments, copy & paste from wiki say. However, it is worthwhile being able to tell from the variable name which is a distance & which is an angle. In a larger program confusion could arise from applying a trig function to a distance say.

To aid understanding, you can make the variable names the same for the function declaration, definition, and call. But don't do it for local variables (declared inside a function) because they are actually a different variable.

IMO you are making the function too complicated - it could be done with 1 LOC.

Variable types: Make them all `double` - in the declaration, the definition and the function call.

Hope all goes well :+)
I'll take that on board, it's been a week on c++ but there is a long way to go before i start my computer science degree in September so im confident ill get there in time, first year is c++ and so far i love it.
code tidyed up now with menu and what you have said has been taken on board cannot use constexpr as my Compiler don't support C++11.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include const double PI = 3.14159265; const double Degradconv = PI/180.0; void menu(); void areaoftriangle(); int main () { menu(); return 0; } void menu() { std::cout << "Welcome to Jordy's triangles" << std::endl; std::cout << "1. Area of triangle knowing 2 sides (a,b) and an angle (C) inbetween them" << std::endl; std::cout << "Enter your selection "; int menuselect; std::cin >> menuselect; if(menuselect==1) { std::cout << std::endl; areaoftriangle(); } } void areaoftriangle() { std::cout << "area of triangle of sides a, b with angle C inbetween them" << std::endl; std::cout << "enter a b C " << std::endl; double side1,side2,angle1; std::cin >> side1 >> side2 >> angle1; double area = 0.5 * side1 * side2 * sin(angle1*Degradconv); std::cout << "area of the triangle is " << area << std::endl; std::cout << "Enter 0 for menu " << std::endl; int returnmenu; std::cin >> returnmenu; if(returnmenu==0) { std::cout << std::endl << std::endl << std::endl; menu(); } } ``````
Last edited on
Hi Jordy,

First of all, good effort in fixing up your program. Lots of beginners make the mistake of saying "It works, so I will leave it now". But you have made some improvements - so GOOD WORK !

However there are still a few improvements to make plus some new concepts.

The first is that you have chained all your functions together, there is nothing illegal about this, but it is more organised in this situation & IMO to call functions from main. The `menu` function is fine as `void`, but `areaoftriangle` should return a value, and the display of the results could either be done in `main()` or in it's own function.

A big help to beginners, even though it's boring, is to write psuedo code as comments. This allows one to identify functions, the control flow (looping, branching etc) structure of the program, even variable names. This can be an iterative process - you can start as simple as you like then go back & add more detail & refine things as much as you want. Leave the comments in the code, as they can serve as a bit of documentation. Variable & function names should be self documenting, that is the name says what it is, or does .

Here is some psuedo code to start with, I have put a bit of code in as well:
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````includes function declarations main function double TriangleArea = 0.0; double CircleArea =0.0; bool Quit = false; //start loop: // use a while loop // DisplayMenu - a void function - displays the text only // GetMenuSelection - function return an unsigned int or a uppercase char use toupper function // DoMenuSelection // this could be a function needs MenuSelection as argument switch (MenuSelection) { //case 'A' : // or numbers if you wish //call function to do triangle area // TriangleArea = CalcTriangleArea; // call function to display results // break; //case 'C' : // call function to do circle area say // call function to display results //break; //case 'Q' : // user wants to quit Quit = true; break; //default : // catch bad input //Process bad input break; } //end loop: other function definitions``````

With functions, write a few lines of comments, stating the preconditions. That is what type of value does the function expect & what rage of values are valid. Also state the post conditions - what the function returns including it's type and range. For example the area function should return a positive `double` - which prompts the question of what to do with negative sine values (180 < theta < 360).

With variables, always initialise them to something that might give a clue that things have gone wrong. For example MenuSelection = 'z'

Here is some info about the switch- near the bottom
http://www.cplusplus.com/doc/tutorial/control/

There is lots of good stuff in the tutorial, also the reference pages & articles at the top left of this page.

Here is an example of a menu & switch - not a complete program, but you can see how it works.
 http://www.cplusplus.com/forum/beginner/99203/#msg534078

Hope all goes well :+)
Topic archived. No new replies allowed.