typdef, struct, and header files

This is a discussion on typdef, struct, and header files within the C Programming forums, part of the General Programming Boards category; I have an assignment to solve triangles given some parameters. Here is the meat: Assignment Write a program that, given ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    18

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    18

    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. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    18
    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. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    18
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    18

    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. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Would you show the code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    18
    sure! I switched everything back to double

    here's my custom triangles.h header

    Code:
    #define deg 57.2957795  /*radian to degree multiplier*/
    #define rad .017453292  /*degree to radian 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[5];
    
    	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. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    18

    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 subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21