Need assistance with Structures.

This is a discussion on Need assistance with Structures. within the C Programming forums, part of the General Programming Boards category; Hello,
I'm working on a lab assignment and in need of assistance related to Structures. The next step I am ...

Need assistance with Structures.

Hello,

I'm working on a lab assignment and in need of assistance related to Structures. The next step I am trying to figure out is how to calculate the distance from one airport (SFO) to all other cities using the Haversine Formula.

So far, I have:
1. Read the data file "Coordinates.csv".
a. Stored the city name in the 'city' field.
b. Stored the city code in the 'code' field.
c. Stored the latitude in the 'latitude' field.
d. Stored the longitude in the 'longitude' field.

It's confusing. The title says needs help with structures, but you don't need help with structures. You need help with the calculations, say you don't know where to begin, but you have an entire function already for the calculations.

Can you narrow this down - what EXACTLY is it that isn't working as it should. Say you feed it the data you listed above, what calculations are coming out wrong. I need a starting place - something definite.

If you would include the formula you're using for the distance calculation, that would be a good thing to add. I certainly don't have it memorized.

> I'm really not too sure where to start on calculating the distance but my assumption is that I need to point values from latitude and longitude to dlatitude1-2 and dlongitude1-2.
I think you've got pointers on the brain.

> while (!feof(fp))
You could condense this code to be
while ( fgets(temp,128,fp) != NULL )

> *((*strings)+count) = (char*)malloc(strlen(temp)+1);
> strcpy(*((*strings)+count),temp);
You do this in a number of places (and get it wrong in one of them).
Consider a small function to allocate space and copy the string, returning the pointer to the newly allocated and copy string.

You should also stop casting the return result of malloc in C programs.
If (by removing the cast) you get "can't convert void* to T*" warnings, then you should stop using a C++ compiler to compile your C code.

> void main()
Nope - main returns int

At some point, you'll need to call free() on all the things you malloc'ed.

I'm basically looking to understand how to use the Haversine formula to calculate the distances. I'm able to print out all airports and their coordinates from the data file at this point. Just not too sure how to use the formula and print out the total distance between SFO and all other airports.

I placed the Haversine formula at the bottom of the code and not too sure where to go from there.

Thanks for the feedback Salem I'll give that a try. Really appreciate the feedback.

I went with Salem suggestion and it works. Thanks. Now the issue I am having is that the distance prints the same number for each airport distance. My output looks like this:

SFO to SEA = 670.52 miles
SFO to SFO = 670.52 miles
SFO to LAX = 670.52 miles
SFO to DFW = 670.52 miles
SFO to MCO = 670.52 miles
SFO to ATL = 670.52 miles
SFO to SLC = 670.52 miles
SFO to LGA = 670.52 miles
SFO to ORD = 670.52 miles
SFO to DEN = 670.52 miles