Home > Software design >  How do I give last names their corresponding first names?
How do I give last names their corresponding first names?

Time:12-12

Output

The last names are sorted alphabetically, but they do not correspond to their first name. I used two arrays to hold the first/last names I extracted from a file.

I sorted last names using: `

for(int i= 0; i < LISTSIZE; i  )
{       
minidx = i;  //declared as int before
strcpy(headers[0],lastname[i]);
  
  for(int j = i; j < LISTSIZE; j  )
  {
  int match = strcmp(lastname[j],headers[0]);
  if (match < 0)
  {
  minidx = j;                   
  strcpy(headers[0], lastname[j]);      
  }             
  }
strcpy(headers[0], lastname[minidx]);
strcpy(lastname[minidx], lastname[i]);
strcpy(lastname[i], headers[0]);
  }

`

I tried combining last and first names into one array but that ended up with nothing. I have looked on multiple forums and found nothing of note. I am relatively new to programming, so excuse my lack of knowledge.

CodePudding user response:

You will want to hold the first and last names together in something like a structure when extracting from your file, and then sort those structures by the last name field. This keeps your associated data together from the beginning, so there's no need to attempt to recombine it later.

#include <string>

#define LISTSIZE 3

// Structure that stores a firstname and lastname for a person.
struct person {
    std::string firstname;
    std::string lastname;
};

bool lexical_less_than(std::string wordone, std::string wordtwo) {

    //code to compare two strings and determine which one is 'before'
    //the other in alphabetical order goes here

    return true;
}

int main() {

    //structure initialiser - firstname = bob, lastname = martin
    struct person p1 = {"bob", "martin"};
    struct person p2 = {"jim", "davey"};
    struct person p3 = {"james", "kirk"};
    
    //array to alphabetise
    struct person people[LISTSIZE] = {p1, p2, p3};

    
    //sketch of sorting code - this doesn't work!
    //this is just to demo moving structures around and 
    //accessing fields.
    for (int i = 0; i < LISTSIZE; i  ) {
        if (i != LISTSIZE - 1) {
            //pass the lastnames of our people to the compare function
            if (lexical_less_than(people[i].lastname, people[i 1].lastname)) {
                //swap the structures over. Note that you can manipulate 
                //structures in the same way as any other type
                struct person temp = people[i];
                people[i] = people[i 1];
                people[i 1] = temp;
            }
        }
        
    }



}

Some reading on Structures - MSDN, Structures - CPlusPlus.

Consider also using standard library algorithms/containers such as std::string and std::vector rather than char arrays and strcpy. These will be safer and more idiomatic.

CodePudding user response:

Based on this answer, if you have to use two separate arrays, use an index array to "sort" both of the arrays.

Here is an example of how to use an index array:

#include <iostream>
#include <string>
#include <algorithm>

#define LISTSIZE 4

int main()
{
    // Sample first and last name arrays
    std::string firstName[LISTSIZE] = {"Bob", "Alan", "Jack", "Jill"};
    std::string lastName[LISTSIZE] = {"Smith", "Jones", "Brown", "Johnson"};

    // This is the index array
    int index[LISTSIZE] = {0,1,2,3};

    // sort on last name
    std::sort(index, index   LISTSIZE, [&](int n1, int n2)
              { return lastName[n1] < lastName[n2]; });

    // Now print out the names, sorted on last name
    for (int i = 0; i < LISTSIZE;   i)
       std::cout << firstName[index[i]] << " " << lastName[index[i]] << "\n";
}  

Output:

Jack Brown
Jill Johnson
Alan Jones
Bob Smith

Note that we did not sort the arrays with the names themselves -- they have not been altered. The only thing done was to create the index array and sort the index array based on the ordering of the last name. At the end, we use the index value at position i to associate first and last names.

I know you used C-style strings, but the principle is the same.

  • Related