1. ## typdef, struct, and header files

I have an assignment to solve triangles given some parameters. Here is the meat:

Assignment

Write a program that, given three adjacent measurement s of a triangle, calculates the remaining three.
Program Description
Inputs:

For this program, you will be given three measurements of a triangle: side-angle-side, angle-side-angle, side-side-side, etc. The input stream will consist of sets of data that both identify what is being given and the actual measurements in the following form:

(type, meas1, meas2, meas3)

type may be:

ASA angle, side, angle
SAS side, angle, side
SSS side, side, side

AAS angle, angle, opposite side

meas1, meas2 and meas3 are:
double precision floating point numbers for the measurements.

All angles are in degrees, not radians.
Outputs:

You must calculate the remaining 3 measurements (sides and/or angles) using the rules learned in geometry. (Get out your geometry text if you dont remember.
Construction detail 1:

Build a structure typedef to contain the 3 sides and 3 angles for a triangle.
Construction detail 2:

Build a separate function for each solution case.
Construction detail 3:

Build a header file to contain the typedef for the structure and the prototypes for each of the functions. Include this header file in you main program, which must be in a separate file named  triangles.h
Construction detail 4:

Write a main program that:

1. inputs the description of the triangle,
2. echoes the triangles description
3. uses the individual functions to calculate the missing information,
4. then prints the completed triangle.

Remember that the main program must include the triangles.h header file, and may not be the same file as the functions.
First of all, I'm not asking anyone to do the assignment for me. I just need help understanding some things to get the header file correct. I have read a lot of posts regarding tydef struct and don't quite understand it yet. Would I do something like this?

Code:
```typedef struct {
double side_a;
double side_b;
double side_c;
double ang_A;
double ang_B;
double and_C;
} ASA;``` 2. I'm not sure that you'd want to call it "ASA", more like "triangle". But yes, you'd put the structure and any function prototypes there that are needed, like your instructions mention.

Quzah. 3. ## ahhhh

So even though the user will have the option of selecting ASA, SAS, SSS, or AAS I can just use this one typedef as all will have the same elements? ie...3 angles, 3 sides

I still don't really understand how I would use this in a function though.

btw....thanks for the help! 4. The struct is just a place to store your data; it doesn't know nor care what kind of data it is so long as it's a correct type. Storing a string in a double, say, would be silly. But aside from that, there's nothing wrong with a common struct for all indata.

So you create an instance of the struct and manipulate it.
Include the header where it's declared, then:
Ass data;
data.side_a = 1.0;
data.ang_A = 5.0;

Obviously replace the example with the data you get from the user or that which you calculate. 5. awesome! part of the assignment is to create a function for each case...ie given angle_A, side_c, angle_B separate functions for each to find angle_C, side_a, side_b. Would this form change my typedef struct as I would be using data that would be calculated in another function? 6. No. Those functions all need to use the same basic pieces of data - angles and sides.

All a struct does is group variables together into a single unit you can refer to. All typedef does is allow you to refer to that struct elsewhere in your code without having to use the 'struct' keyword all the time. 7. okay, thanks to everyone's help, I have my typedef written:

Code:
```typedef struct {
double side_a;
double side_b;
double side_c;
double ang_A;
double ang_B;
double ang_C;
} triangle;```
I guess I cant initialize a variable within my struct? I am getting a run-time error in the following function:

Code:
```void ASA_ang_C (double elmnt_1, double elmnt_3){

triangle s_angle_C;

elmnt_1 = s_angle_C.ang_A;
elmnt_3 = s_angle_C.ang_B;
s_angle_C.ang_C = 180 - s_angle_C.ang_A - s_angle_C.ang_B;
printf ("angle A = %.2f\n", s_angle_C.ang_A);
printf ("angle B = %.2f\n", s_angle_C.ang_B);
printf ("angle C = %.2f\n", s_angle_C.ang_C);

return;```
Run-Time Check Failure #3 - The variable 's_angle_C' is being used without being initialized.
I've tried to initialize it a couple of different ways, but not getting anywhere. What am I missing? fyi...I am bringing in elmnt_1 and elmnt_3 from a scanf statement in main. 8. The problem is that you are assigning the value of s_angle_C.ang_A to elmnt_1, but you actually want the other way round. The same goes for s_angle_C.ang_B. 9. ## great!

that worked! Thanks! Now I am getting some weird output for the angles

angle A: -107374176.000000
angle B: -107374176.000000
angle C: 214748532.000000
originally, I was bringing elmnt_1 and elmnt_3 in as double, and I was get even larger numbers. I changed them to float, and they got smaller, but nowhere near correct 10. Would you show the code? 11. sure! I switched everything back to double

Code:
```#define deg 57.2957795  /*radian to degree multiplier*/

void ASA_ang_C (double elmnt_1, double elmnt_3);  /*prototype*/

typedef struct {
double side_a;
double side_b;
double side_c;
double ang_A;
double ang_B;
double ang_C;
} triangle;```
here's the main function

Code:
```#include "triangles.h"
#include <math.h>
#include <stdio.h>

main(){  /*start main*/

double elmnt_1;
double elmnt_2;
double elmnt_3;
char id;

printf ("Welcome to the triangle solver\n");
printf ("\n");
printf ("Please enter triangle parameters - form: (type, element1, element2, element3)\n");
printf ("Example: (ASA, 45, 3.6, 60)\n");
printf ("\n");
scanf ("(%s, %lf, %lf, %lf)", &id,&elmnt_1,&elmnt_2,&elmnt_3);

ASA_ang_C(elmnt_1, elmnt_3);

return;
}```

and here's the function to calculate angle C

Code:
```#include "triangles.h"
#include <math.h>
#include <stdio.h>

void ASA_ang_C (double elmnt_1, double elmnt_3){

double ttl = 180;
triangle s_angle_C;

s_angle_C.ang_A = elmnt_1;
s_angle_C.ang_B = elmnt_3;
s_angle_C.ang_C = ttl - s_angle_C.ang_A - s_angle_C.ang_B;
printf ("angle A = %f\n", s_angle_C.ang_A);
printf ("angle B = %f\n", s_angle_C.ang_B);
printf ("angle C = %f\n", s_angle_C.ang_C);

return;
}``` 12. ## okay

I figured out it has to do with my scanf structure. Per the assignment the input should be like (ASA, 45, 3.6, 60), but when I took all of that out from my scanf and input it as
ASA 45 3.6 60 it worked just fine. I'm going to move on for now. If anyone has any suggestions to be able to make the scanf work per the assignment, I am all ears. Thanks! Popular pages Recent additions 