Adapting a trigonometry solver

Pages: 12
Hi guys, I have found this very good piece of C/C++ source code which allows you to find out pretty much everything about a triangle as long as you know the lengths of all three sides:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <conio.h>

int main()
{
float a,b,c,S,D,A,B,C,Area,R;
    printf("Enter the lengths of the three sides of the triangle:\n");
    scanf("%f%f%f",&a,&b,&c);

    S = (a+b+c)/2.0;        // S is the semiperimeter of the triangle
    D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
    if(D<=0)
 {
    printf("That triangle is not possible! Check your worksheet and try agian!\n");
 fflush(stdout);
    getchar();
    getchar();
 }

    if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
    printf("The triangle is ISOSCELES\n");
    if(a==b && b==c && c==a)
    printf("The triangle is EQUILATERAL\n");
    if(a!=b && b!=c && c!=a)
    printf("The triangle is SCALENE\n");

    Area = sqrt(D);
    R = (a*b*c)/(4.0*Area);
    printf("PERIMETER     = %.2f cm\n",(2.0*S));
    printf("AREA          = %.2f cm^2\n",Area);
    printf("CIRCUM RADIUS = %.2f cm\n",R);
    printf("\n");                                           // using sine rule,we get...
    A = (180.0/3.1415926)*asin(a/(2.0*R));//  value of pi should be upto 7
    B = (180.0/3.1415926)*asin(b/(2.0*R));//  decimal places of accuracy and

    C = (180.0/3.1415926)*asin(c/(2.0*R));//  note that the 7th decimal place
		 		 		 		        //  6 and not 7 as it had to be if were
    if(A==90.0 || B==90.0 || C==90.0)     //  approximated to 7 decimal

    printf("The triangle is RIGHT ANGLED\n");
    if(A<90.0 && B<90.0 && C<90.0)
    printf("The triangle is ACUTE ANGLED\n");
    if(A>90.0 || B>90.0 || C>90.0)
    printf("The triangle is OBTUSE ANGLED\n");

    printf("The angles are:\n\n");
    printf("A = %.2f degrees\n",A);
    printf("B = %.2f degrees\n",B);
    printf("C = %.2f degrees\n",C);
    printf("\nWhere A,B,C stand for angles opposite to sides with the lenghts of %.2f cm, %.2f cm, %.2f cm",a,b,c);
    printf(" respectively\n \n");
    fflush(stdout);
    getchar();
    getchar();  
return 0;
}



I would like to adapt this piece of code so that if you know two side lengths and an angle or two angles and side length you can find out everything about the triangle. Obviously I know of the cosine rule however I am not sure how to apply it here to solve my problem.

The people who tell me how to solve it can be featured in the "Dedicated to:" section of my final program if they want to.

Many thanks, Cameron
Last edited on
Enjoy that. And use std::cout and std::cin instead of print and scan because they are the standard. Also system functions are a bad idea. Try to avoid them.
Okay, I'll rewrite it so it uses standards when I get the problem sorted out.

Also, bump.
First, make a global variable float PI = 3.1415926; I consider this an exception to the "globals are evil" argument.

Second, your code fragment has no "main()" statement, and actually has no opening "{". I assume

1
2
int main()
{


goes before line 7

Take everything from line 21 on and make it a new function. Pass it a,b,c and D. A, B, C and R will be local to the new function. Call the new function at line 20.

For new inputs., change main to ask what form of input is wanted (ASA, AAS, SAS, SSS). Have subroutines to input the data and then calculate a, b, c and D. (lines 9 - 18 do this for SSS). Pass this data into the new subroutine.

Yeah, sorry about that- it was a bad copy and paste.

Thanks for the tips, I'll have a look at this right now :) Thanks!
Okay, I failed at even attempting that, doug4.

Please may you show me your C++ coding prowess and give me a visual example if you'd be so kind?
Here is a quick and dirty. I did not compile it. I did not add any functionality beyond what you wrote except for the input in main(). Enjoy.

I put in C++ streams instead of your printf and scan functions. I believe I got the "fixed" and "precision" statements correct. Have fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using std::cout;
using std::cin;
using std:endl;

#include <string>
using std::string;

float PI = 3.1415926;
void getSSS();
void printInfo();

int main()
{
    string triangleType;

    cout << "Enter type of triangle definition: SSS, SAS, AAS, ASA: ";
    cin >> triangleType;
    cout << endl;

    if (triangleType == "SSS")
    {
        getSSS();
    }
    /// else if (triangleType == "SAS") ...
    else
    {
        cout << "Type " << triangleType << " not supported." << endl;
    }

    return 0;
}

void getSSS()
{
    float a, b, c, D, S;

    cout << "Enter the lengths of the three sides of the triangle:";
    cin >> a >> b >> c;
    cout << endl;

    S = (a+b+c)/2.0;        // S is the semiperimeter of the triangle
    D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
    if(D<=0)
    {
        cout << "That triangle is not possible! Check your worksheet and try again!" << endl;
    }
    else
    {
        printInfo(a, b, c, D, S);
    }
}

void printInfo(float a, float b, float c, float D, float S)
{
    float A, B, C, Area, R;
    cout.precision(2);
    cout << fixed;

    if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
        cout << "The triangle is ISOSCELES" << endl;

    if(a==b && b==c && c==a)
        cout << "The triangle is EQUILATERAL" << endl;

    if(a!=b && b!=c && c!=a)
        cout << "The triangle is SCALENE" << endl;

    Area = sqrt(D);
    R = (a*b*c)/(4.0*Area);
    cout << "PERIMETER     =  " << (2.0*S) << "cm" << endl;
    cout << "AREA          = " << Area << "cm^2" << endl;
    cout << "CIRCUM RADIUS = " << R << "cm" << endl;
    cout << endl;

    // using sine rule,we get...
    A = (180.0/PI)*asin(a/(2.0*R));
    B = (180.0/PI)*asin(b/(2.0*R));
    C = (180.0/PI)*asin(c/(2.0*R));
		 		 		 		       
    if(A==90.0 || B==90.0 || C==90.0)
         cout << "The triangle is RIGHT ANGLED" << endl;

    if(A<90.0 && B<90.0 && C<90.0)
         cout << "The triangle is ACUTE ANGLED" << endl;

    if(A>90.0 || B>90.0 || C>90.0)
         cout << "The triangle is OBTUSE ANGLED" << endl;

    cout << "The angles are:" << endl << endl;
    cout << "A = " << A << " degrees" << endl;
    cout << "B = " << B << " degrees" << endl;
    cout << "B = " << B << " degrees" << endl;
    cout << endl << "Where A,B,C stand for angles opposite to sides with the " 
         << "lengths of " << a << " cm, " << b << " cm, " << c
         << " cm respectively" << endl << endl;

}
Last edited on
Thanks very much! I really appreciate this.

When I attempted to compile it I received an error to do with void printInfo(); having too many arguments, how do I fix that?

Thanks again
Sorry, my function prototype was incorrect. I went a little too fast.

Change line 11 to :

void printInfo(float a, float b, float c, float D, float S);
Okay, I have done that and now my IDE is saying there is an error involving line 24 'getSSS' was not declared in this scope
I hope you had a good weekend. Did you get any further in your endeavors?

I got a chance to compile the code I gave you. After I made the change to line 11, I also found other errors:

Line 4 should be using std::endl; (it was missing a colon in the scope operator)

I needed to add:
using std::fixed; (somewhere after #include <iostream> )
and
#include <cmath>

and there was a cut-and-paste problem in line 93 (should be "C"s rather than "B"s, and I added some spaces in lines 71 - 73 for readability. But then everything compiled.

I don't know why you got an error for getSSS(). Did you change the wrong line? Lines 10 and 11 should read:

1
2
void getSSS();
void printInfo(float a, float b, float c, float D, float S);


Here's my current version (it compiles):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <string>
using std::string;

#include <cmath>

float PI = 3.1415926;
void getSSS();
void printInfo(float a, float b, float c, float D, float S);

int main()
{
    string triangleType;

    cout << "Enter type of triangle definition: SSS, SAS, AAS, ASA: ";
    cin >> triangleType;
    cout << endl;

    if (triangleType == "SSS")
    {
        getSSS();
    }
    /// else if (triangleType == "SAS") ...
    else
    {
        cout << "Type " << triangleType << " not supported." << endl;
    }

    return 0;
}

void getSSS()
{
    float a, b, c, D, S;

    cout << "Enter the lengths of the three sides of the triangle:";
    cin >> a >> b >> c;
    cout << endl;

    S = (a+b+c)/2.0;        // S is the semiperimeter of the triangle
    D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
    if(D<=0)
    {
        cout << "That triangle is not possible! Check your worksheet and try again!" << endl;
    }
    else
    {
        printInfo(a, b, c, D, S);
    }
}

void printInfo(float a, float b, float c, float D, float S)
{
    float A, B, C, Area, R;
    cout.precision(2);
    cout << fixed;

    if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
        cout << "The triangle is ISOSCELES" << endl;

    if(a==b && b==c && c==a)
        cout << "The triangle is EQUILATERAL" << endl;

    if(a!=b && b!=c && c!=a)
        cout << "The triangle is SCALENE" << endl;

    Area = sqrt(D);
    R = (a*b*c)/(4.0*Area);
    cout << "PERIMETER     =  " << (2.0*S) << "cm" << endl;
    cout << "AREA          = " << Area << "cm^2" << endl;
    cout << "CIRCUM RADIUS = " << R << "cm" << endl;
    cout << endl;

    // using sine rule,we get...
    A = (180.0/PI)*asin(a/(2.0*R));
    B = (180.0/PI)*asin(b/(2.0*R));
    C = (180.0/PI)*asin(c/(2.0*R));
		 		 		 		       
    if(A==90.0 || B==90.0 || C==90.0)
         cout << "The triangle is RIGHT ANGLED" << endl;

    if(A<90.0 && B<90.0 && C<90.0)
         cout << "The triangle is ACUTE ANGLED" << endl;

    if(A>90.0 || B>90.0 || C>90.0)
         cout << "The triangle is OBTUSE ANGLED" << endl;

    cout << "The angles are:" << endl << endl;
    cout << "A = " << A << " degrees" << endl;
    cout << "B = " << B << " degrees" << endl;
    cout << "C = " << C << " degrees" << endl;
    cout << endl << "Where A,B,C stand for angles opposite to sides with the " 
         << "lengths of " << a << " cm, " << b << " cm, " << c
         << " cm respectively" << endl << endl;

}

Hi, thanks for the code! My weekend was pretty good, thanks :)

I've improved the Trinomial solver part of the program so it can now solve any quadratic under the sun including ones with complex solutions and it can also expand and solve quadratics in (ax+b)(cx+d)=0 form. I'll post the code in a minuet!

Thanks once again, I really appreciate it!
Whoah. That sounds useful. Now if only you could make a gui for it! Then we wouldn't have to do quadratic formulas by hand anymore.
Thank you very much!

There's loads of other stuff it can do such as finding prime factors of up to 10 bit numbers, finding the nth iteration of the Fibonacci series, a psudorandom integer generator, an equations of motion problem solver and loads of other stuff.

I plan to implement some calculus based algorithms to allow the user to be able to find facts about pretty much any 3D shape once I get advanced enough
https://www.dropbox.com/sh/e0hz3fyjbg2izsz/KBKJnDC_1n

Here's the link to the dropbox folder- I still need to upload the latest version
Just going to warn you, the code is pretty messy and unconventional. I WILL tidy it up and use standard and consistent code once I get the main shape of the program finished
Sweet. Save it up and add it to your portfolio one day.
I was planning to ;)

Please, download a copy and see if it's useful
Ok
Good job so far!
Pages: 12