1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Rep Power

    reading structured arrays, changing their structure


    i am trying to read in two different structured arrays, place them in a different structure so they are both the same and then compare them.

    the first array, 'primary', has the structure..

     struct primary_plot {
      double   bearing; /*degrees */
      int      range;  /*metres */
      double   elevation; /*degrees */
    the second array, 'secondary', has a similar structure..

     struct secondary_plot {
      double bearing; /*degrees */
      int range;  /*metres */
      int altitude; /*metres */
      int flight_number;
    i need to read these arrays into a file, and store them in seperate arrays with this structure..

    struct aircraft_information {
      double bearing; /*degrees */
      double  range;  /*kilometres */
      int altitude; /*metres */
      int flight_number;
      int  match;  /*array index of matching plot */
    obviously i will need to convert the range's from both the primary and secondary into kilometres which isnt a problem, im just unsure of how to carry out this task.

    any help will be appreciated!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    I feel I must be missing something, because I do not understand what problem you're having. Here's how I think it would be done:
    struct primary_plot pri_plot;
    struct secondary_plot sec_plot;
    struct aircraft_information ac_info;
    struct aircraft_information {
    ac_info.bearing = pri_plot.bearing; /*degrees */
    ac_info.range = pri_plot.range;  /*metres */
    ac_info.range /= 1000.0;  /*kilometres */
    ac_info.altitude = sec_plot.altitude; /*metres */
    ac_info.flight_number = sec_plot.flight_number;
    Of course, I'm assuming that the corresponding bearing and range values are the same in both the primary and secondary plots. Otherwise, you'll have to make a determination as to which one you want to use.

    Was your question about how to handle the conversion of the range from int to double? In C, some simple data type conversions are automatic, like int to long int, float to double, and int to double. In many cases, you would need to use type casting to explicitly tell the compiler to perform a conversion; eg:
    ac_info.range = (double)pri_plot.range / 1000.0;  /*kilometres */
    That explicitly converts the integer value to a double before it gets divided by the double constant 1000.0.

    Sometimes, these automatic conversions within mixed-type arithmetic expressions can cause unexpected results. For example, this test program:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
        double dRange;
        int    iRange = 4500;
        dRange = iRange / 1000.0;
        printf("%d -> %g\n",iRange,dRange);
        dRange = iRange / 1000;
        printf("%d -> %g\n",iRange,dRange);
        return 0;
    produced this output:
    4500 -> 4.5
    4500 -> 4
    In the first case, the 1000.0 indicated a floating-point divide, so iRange was converted to double before the divide. To be honest, I had expected the int variable to cause the constant to be converted to int, because normally the left-hand term in an operation determines the data type of the result.

    In the second case, an integer divide was performed, which discards any remainder, and then the quotient was converted to double for the assignment. One would have assumed the result to be 4.5, but automatic conversion gave an unexpected result.

    In general, if your math expressions give unexpected results and they appear correct, then it may be due automatic type conversion.

    I hope that I addressed your question and that this helps.
    Last edited by dwise1_aol; May 15th, 2003 at 04:00 PM.

IMN logo majestic logo threadwatch logo seochat tools logo