# Thread: Triangle coding (pretty much finished just need a quick tip)

1. ## Triangle coding (pretty much finished just need a quick tip)

I have written the following code.
Given two sides and an angle, calculate the third side and the remaining two angles in the triangle. Then output if it is right, obtuse or acute.

My problem is that it will only output information if I input 90 for the initial angle. What am I doing wrong?

Code:
```#include <stdio.h>#include <math.h>
#define PI 3.14159265358979323846

long double input_a(void);
long double input_b(void);
long double input_A(void);
long double calc_c(long double a,long double b,long double A);
long double calc_B(long double b,long double c,long double A);
long double calc_C(long double c,long double a,long double A);

int main(void)
{
long double A, B, C, a, b, c;

a=input_a();
b=input_b();
A=input_A();

if (A==90)
{

c= calc_c(a, b, A);
B= calc_B(b, c, A);
C= calc_C(c, a, A);
}
else
{
c= calc_c(a, b, A);
B= calc_B(b, c, A);
C= calc_C(c, a, A);
}
if(A<90 && B<90 && C<90)
{
printf("This is an acute triangle");
}
else if(A==90 || B==90 || C==90)
{
printf("This is a right triangle");
}
else
{
printf("This is an obtuse triangle");
}
getch();

}

long double input_a()
{
long double a;
printf("Enter the length of the first side of the triangle\n");
scanf("%lf", &a);

return a;
}

long double input_b()
{
long double b;
printf("Enter the length of the second side of the triangle\n");
scanf("%lf", &b);

return b;
}

long double input_A()
{
long double A;

printf("Enter the angle A (degrees) between the two sides\n");
scanf("%lf", &A);

return A;
}
long double calc_c(long double a,long double b, long double A)
{
long double c;

c = sqrt((a*a) + (b*b)) -  2 * a * b * cos (A * PI/180);

printf("The length of the third side is %lf\n", c);

return c;
}

long double calc_B(long double b,long double c,long double A)
{
long double B;

B=  asin(b*sin(A * PI/180)/c);

printf("Angle B is %lf (radians)\n", B);

B= 180*B/PI;

printf("Angle B is %lf (degrees)\n", B);
return B;
}

long double calc_C(long double c,long double a,long double A)
{
long double C;

C=  asin(a*sin(A * PI/180)/c);

printf("Angle C is %lf (radians)\n", C);

C= 180*C/PI;

printf("Angle C is %lf (degrees)\n", C);

return C;
}```

2. would you first of all mind indenting your code properly please?? all the #includes at the beginning would be better on separate lines..... let me look at the code.. but first what kind of help do you want??

3. First eliminate all this warnings
Code:
```Macintosh-c8bcc88e5669-9:~ usi\$ pico px.c
Macintosh-c8bcc88e5669-9:~ usi\$ gcc -Wall px.c -o px
px.c: In function ‘input_a’:
px.c:57: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c:57: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c: In function ‘input_b’:
px.c:68: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c:68: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c: In function ‘input_A’:
px.c:80: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c:80: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘long double *’
px.c: In function ‘calc_c’:
px.c:93: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:93: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c: In function ‘calc_B’:
px.c:108: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:108: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:114: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:114: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c: In function ‘calc_C’:
px.c:127: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:127: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:133: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
px.c:133: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘long double’
Macintosh-c8bcc88e5669-9:~ usi\$```
I eliminate it them from inputa function
Code:
```long double input_a()
{
long double a;
printf("Enter the length of the first side of the triangle\n");
scanf("%Lf", &a);

return a;
}```
Now you should be able to eliminate the rest

4. I want to know why the only input that yields an output is 90 for the degrees. If I type anything else in the program yields nothing. I think my code is correct when deducing whether or not the triangle is obtuse or acute, but it isn't outputting the information I need. I'm under the impression I'm missing something small, I was hoping someone here would catch it for me

5. I don't get any of those errors. My professor specified to use long double when I went to office hours and told me to add the "l" inside the %f

6. Did you compiled your code with a flag, such as -Wall?

I would replace l with L .

7. @dKarayof hey why do you need to create functions that would read the lengths of the triangle and the angle.. it would be better you just read them directly in the main() and create a function to determine the angle between the sides and the type of triangle let me just edit and post a more compact code.

8. @std100.. i got similar flags on my codeblocks compiler ... let me post a better code.

9. Originally Posted by std10093
Did you compiled your code with a flag, such as -Wall?

I would replace l with L .
i dont think i did

10. Code:
```#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846

long double input_a(void);
long double input_b(void);
long double input_A(void);
long double calc_c(long double a,long double b,long double A);
long double calc_B(long double b,long double c,long double A);
long double calc_C(long double c,long double a,long double A);

int main(void)
{
long double A, B, C, a, b, c;

a=input_a();
b=input_b();
A=input_A();

if (A == 90)
{

c = calc_c(a, b, A);
B = calc_B(b, c, A);
C= calc_C(c, a, A);
}
else
{
c= calc_c(a, b, A);
B= calc_B(b, c, A);
C= calc_C(c, a, A);
}

if(A<90 && B < 90 && C<90)
printf("This is an acute triangle");

else if(A==90 || B==90 || C==90)
printf("This is a right triangle");

else
printf("This is an obtuse triangle");

getc(stdin);

return 0;
}

long double input_a()
{
double a;
printf("Enter the length of the first side of the triangle\n");
scanf(" %lf", &a);

return a;
}

long double input_b()
{
double b;
printf("Enter the length of the second side of the triangle\n");
scanf(" %lf", &b);

return b;
}

long double input_A()
{
double A;

printf("Enter the angle A (degrees) between the two sides\n");
scanf(" %lf", &A);

return A;
}
long double calc_c(long double a,long double b, long double A)
{
long double c;

c = sqrt((a*a) + (b*b)) -  2 * a * b * cos (A * PI/180);

printf("The length of the third side is %lf\n", c);

return c;
}

long double calc_B(long double b,long double c,long double A)
{
long double B;

B=  asin(b*sin(A * PI/180)/c);

printf("Angle B is %lf (radians)\n", B);

B= 180*B/PI;

printf("Angle B is %lf (degrees)\n", B);
return B;
}

long double calc_C(long double c,long double a,long double A)
{
long double C;

C=  asin(a*sin(A * PI/180)/c);

printf("Angle C is %lf (radians)\n", C);

C= 180*C/PI;

printf("Angle C is %lf (degrees)\n", C);

return C;
}```

11. Thank you so much!

This code is exiting however before I have a chance to read the output. Is it the compiler I'm using?

12. nevermind i added a getchar(); and it all worked out. Thank you sooo much

13. well i dunno im using the codeblocks version 10.01 on windows machine which are u using??

14. EDIT: Man, this thread moves fast! I was writing this before I saw post #11. Still, everything here is valid.

@dKarayof:
First, you must fix the errors that std10093 pointed out related to scanf and printf. They may actually be part of your problem, since using the wrong format specifier with the wrong variable type may result in undefined behavior. scanf may be overwriting memory it isn't supposed to, since the size of the variable you pass, and what it expects, are not the same. Until you fix it, the best you can hope for incorrect results. Also, you should put some error checking into your input functions, to make sure nobody enters negative lentghs or angles outside the range of (0, 180) degrees. Lastly, you only need two input functions, input_length and input_angle. Then you just do
Code:
```a = input_length();
b = input_length();
C = input_angle();```
Note, I used C, not A. A is typically the angle opposite side a, i.e. between sides b and c. C is the angle between sides a and b. You should probably adjust your program (calc_X functions, input/output messages and variable names) accordingly.

Remember, a function should do one thing and do it well. A calculation function should not have any input or output, just calculations. Print text to the user in a different function, to which you pass the results of the calculations.

calc_B and calc_C could just be called calc_angle, and you pass different sides and angle to it accordingly. I would also rename calc_c to calc_side.

Then, tell us exactly what's wrong. Tell us the exact input you give. Then tell us what output you expect and what output you actually get. Do you get incorrect results? Strange things like "Inf" or "NaN"? It may help to copy-paste exactly from your terminal.

Originally Posted by Nyah Check
@dKarayof hey why do you need to create functions that would read the lengths of the triangle and the angle.. it would be better you just read them directly in the main() and create a function to determine the angle between the sides and the type of triangle let me just edit and post a more compact code.
A function is perfectly acceptable here, especially since he can reuse code for inputting length, as I showed above. Also, when he adds error checking, it will help keep the main function less crowded.

15. ok i was just thinking that level orthogonality would not be really needed in this small piece of code... any way thats fine. and thanks @andruril...

Popular pages Recent additions