LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Structures and String Arrays, C (https://www.linuxquestions.org/questions/programming-9/structures-and-string-arrays-c-875764/)

SithArrow 04-18-2011 08:49 PM

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.

PTrenholme 04-18-2011 09:59 PM

Try something like this:
Code:

times = (time_type *) malloc(100 * sizeof(time_type)); #etc.
# The "100" is arbitrary. You can always realloc() if you need more than 100 "people".

but a better solution would be to use, say, sqlite to create a data base containing your input data in tables, and then have your C program access the tables with the appropriate queries. (I suggest sqlite since it's designed to be created and called from C programs. F.Y.I.: Firefox, for example, uses sqlite for much of its internal housekeeping.)

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

Shum 04-18-2011 10:04 PM

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];
Then you can refer to individual people by indexing that array.

Code:

people[0].num_passes = 23;
people[1].num_passes = 73;
people[2].num_passes = -5;

Although you'd want to do this inside the for loop and index 'people' with a variable. I'll leave that as an exercise for you.

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.

SithArrow 04-18-2011 10:13 PM

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)

Shum 04-18-2011 10:32 PM

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++) {
    for(j = i + 1; j < MAX_PEOPLE; j++) {
        test people[i] vs people[j] in here.
    }
}

To compare two names you don't need to test every variation of the names. Just go through the two names character-by-character at the same time and compare how many letters are different. Then do it again except loop through one of the names in the opposite direction to test the reverse case.

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')
Just chuck that at the top your code somewhere and you can use to_upper like a function. The ? : notation is just a shorthand if statement. If x is lesser than 'a' (upper-case letter are ordered before lower-case letters) then return it, otherwise shift it by however many values separate 'a' and 'A' (which will convert lower-case letters to upper-case).


All times are GMT -5. The time now is 01:04 AM.