Structures and String Arrays, C
I am working on a project that needs to use structures and I'm pretty sure string arrays. First I declare my structures and they must be exactly like this.
typedef struct { int hour; int minute; char ampm[3]; } time_type; typedef struct { char ride_name[MAX_SIZE]; time_t ride_time; } fastpass_type; typedef struct { char first_name[MAX_SIZE]; char last_name[MAX_SIZE]; fastpass_type passes[MAX_PASSES]; int num_passes; } customer_type; There i read in various names from a file. I am not sure how to assign the name to a spot in the array. Am I supposed to declare time_type time; fastpass_type ride1; customer_type person1; for example? But then would I have to do that for every person because I don't know how many there are. I have a for loop that scans them in. Also I must take each of these names and change the lettering around to determine if they are like the other names. Any help or general info would be much appreciated. Thank you in advance. |
Try something like this:
Code:
times = (time_type *) malloc(100 * sizeof(time_type)); #etc. By the way, the "fuzzy matching" of names to find duplicates is a non-trivial task. Much has been written about such matching (often in the context of "cleaning" mailing lists of duplicated names), and I'd suggest you investigate the literature before "jumping into the deep end of the pool." Without additional information, an exactly matched name is, of course, just that, with no clue if it's the same person or not. (I know of one Canadian athlete with the same name as mine. Google your own name sometime if you think your name is unique.) |
Your main function can declare an array of people the same way you're declaring an array of fastpass_types inside you're customer_type struct.
Code:
customer_type people[MAX_NUMBER_OF_PEOPLE]; Code:
people[0].num_passes = 23; What do you mean by "change the lettering around to determine if they are like the other names". You'll have to be more specific. |
Thanks. I just figured that out too lol.
For the names here is the directions. I figure I am going to have to sort through the array with different methods to test every variation of the name. Thus, for the purposes of your program, you are to treat two names as being the same so long as both the first and last names “match.” In order for there to be a match one of the following has to be true: 1) The names are exactly the same (without regard to case). 2) The names are both exactly the same length, with all corresponding letters matching, except for one (without regard to case). 3) The two names are reversed versions of each other (without regard to case). Here is a list of some possible versions of “Bill Johnson”: Will Johnson (changed the ‘B’ to a ‘W’) BiLk Johnsot (changed the ‘l’ to a ‘k’ in Bill and the ‘n’ to a ‘t’ in Johnson) Lliw Johnton (Reversed the first name and changed the ‘s’ to a ‘t’ in Johnson) Bill NoSnHoj (Reversed the second name) Here are names that should NOT be counted as “Bill Johnson”: Bill NosnhiJ (The last name is both reversed AND contains a letter change) Bll Johnson (The first name is a different length than the last.) Bill Jhnsono (There are 6 sets of corresponding letters in the last name that don’t match) Wlli Johnson (There are 2 sets of corresponding letters in the first name that don’t match) |
If you need to test every name against every other name you'll need two for loops.
Code:
for(i = 0; i < MAX_PEOPLE; i++) { Also here's a handy macro that will convert characters to upper case so you compare them easier. Code:
#define to_upper(x) ((x) < 'a' ? (x) : (x) + 'A' - 'a') |
All times are GMT -5. The time now is 01:04 AM. |