C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 15th, 2003, 08:24 AM
_john007 _john007 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 _john007 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
reading structured arrays, changing their structure

hi,

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..

Code:
 struct primary_plot {
  double   bearing; /*degrees */
  int      range;  /*metres */
  double   elevation; /*degrees */
 };


the second array, 'secondary', has a similar structure..

Code:
 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..

Code:
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!

Reply With Quote
  #2  
Old May 15th, 2003, 03:53 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
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:
Code:
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:
Code:
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:
Code:
#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:
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > reading structured arrays, changing their structure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway