Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 19, 2011, 09:06 PM   #1
Pili
macrumors regular
 
Join Date: Nov 2005
Location: Orlando/Miami, FL
Send a message via AIM to Pili Send a message via MSN to Pili
Conceptual Question: using Arrays in C++

Yes, this is part of a hw problem for class but I just need help in understanding where to go with this, not actual code. I'm given a file that lists lat and long data for states, as well a line that indicates the state (sample shown). The goal is to create a program, using arrays only I believe (not structs), that asks a user to input the state in the command line and the program draws the outline of the state in a window based on the coordinates (drawing is done via a custom library they provide us). I understand the basics of inputing data into an array but I'm at a loss of how to go about dealing with the state labels, and how/if the labels are used selecting the correct data to draw with. Any help would be appreciated. thanks

Code:
WA
122750 49000
117000 49000
117000 46333
116866 46000
119000 46000
122666 45500
122916 46166
123916 46250
124166 47000
124666 48333
122833 48166
122666 47416
122583 47333
122416 47333
122750 49000
-1 -1
ID
117000 49000
116000 49000
116000 48000
114500 46500
...
Pili is offline   0 Reply With Quote
Old Apr 19, 2011, 09:23 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Are you supposed to buffer up the whole file in memory, or search the file after the user inputs the state?

If you're buffering the whole thing an approach would be:
char stateAbbvs[51][3];
float latitudes[51][MAXPOINTS];
float longitudes[51][MAXPOINTS];

Or combine the coordinates:
float coordinates[51][MAXPOINTS][2]; //0 is for latitude, 1 is for longitude

Then when the user enters the abbreviation you find the position in the co-arrays then display based on that. Probably write a function that takes the coordinates for the one state to format for display.

Otherwise, just search the file and read into the float array(s).

-Lee

Last edited by lee1210; Apr 19, 2011 at 09:29 PM.
lee1210 is offline   0 Reply With Quote
Old Apr 19, 2011, 09:32 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
The "BF&I (Brute Force & Ignorant)" approach:

Read and ignore lines until the desired state abbreviation appears on a line by itself.
Read, parse, and draw lat/lon pairs until terminator appears (-1 -1).
Halt.


The "Parallel Arrays" approach.

Read a line. Does it look like a state abbreviation or a lat/lon pair?

If a state abbreviation, store it in an array of state abbreviations,
then commence reading lat/lon pairs into an array of lat/lon coords
for this state, stopping at terminator.
Store that array into another parallel array.

"Parallel array" means an array with a different name and type than the state-name array, but whose items have the same index as the corresponding state name. So the name for AK is at index 0, and the array of coords for AK is also at index 0, but in a different array. (This is a BF&I approach to structuring data, when structs or objects are much clearer and more encapsulated. It's the kind of BS you'd do in old-school BASIC programs where the only composite data structure was an array.)


And I'm wondering what the data for Michigan looks like, or really any state with islands or discontiguous land areas.
chown33 is offline   0 Reply With Quote
Old Apr 19, 2011, 09:43 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by chown33 View Post
(This is a BF&I approach to structuring data, when structs or objects are much clearer and more encapsulated. It's the kind of BS you'd do in old-school BASIC programs where the only composite data structure was an array.)
LOL. You got me remembering about BASIC as soon as I saw parallel arrays.

In the interests of KISS, I would recommended chown33's first BF&I approach, considering the state is one the command-line and the program won't have to make multiple lookups.

If the OP gets that to work, fork the code and try to the parallel arrays approach. Lee's given you the data structures for this. But really I wouldn't bother. For reals, you wouldn't do it this way.
jiminaus is offline   0 Reply With Quote
Old Apr 20, 2011, 09:19 AM   #5
Pili
Thread Starter
macrumors regular
 
Join Date: Nov 2005
Location: Orlando/Miami, FL
Send a message via AIM to Pili Send a message via MSN to Pili
Thanks! going to try and tackle it this afternoon. FYI the states are really blocky and cartoon shaped so its by no means accurate lol.
Pili is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
arrays? subview or submenu? tacotester1 iPhone/iPad Programming 6 Mar 14, 2014 08:06 PM
Please Help with arrays Bigred101 Mac Programming 7 Mar 13, 2014 07:39 PM
arrays of structs farmerdoug Mac Programming 3 Feb 7, 2013 01:47 PM
Comparing Two Arrays Ides iPhone/iPad Programming 5 Aug 10, 2012 03:56 PM
A Conceptual Question about the Mac Firewall and Security Reg88 OS X 6 Jun 23, 2012 10:47 PM

Forum Jump

All times are GMT -5. The time now is 08:22 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC