Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a C++ program to prompt the user to enter a number of names, then print out all the names in alphabetical order of the last names. If the last names are the same, then the first names are compared. A sample run is attached as follows (the text in bold face is user input):

How many names do you want to enter? 5
Enter name #1: Bill Gates
Enter name #2: Michael Jackson
Enter name #3: Tony Blair
Enter name #4: Janet Jackson
Enter name #5: Oprah Winfrey
The names in order are:
Blair, Tony
Gates, Bill
Jackson, Janet
Jackson, Michael
Winfrey, Oprah

The strings we are using in this program are C-strings. You cannot use C++ strings. And, you cannot use any library function other than the overloaded operator (<< or >>)

What I have tried:

I am stuck on this problem, I am using 2 2d arrays for the last names and first names, I initially planned to make a temp array that would hold the sorted last names and first names after I am done with comparison, however, I can't seem to move any further from here, I am stuck after comparing any help would be much appreciated!

int mystrlen(char* s) 
{
	int i;
	for (i=0; s[i] != '\0'; i++);
	return i;
}

int length_longest(char ** name)
{
    int max= mystrlen(name[0]);
    int j=0;
    
    for(int i=1; i<n; i++)
    {
        int len = mystrlen(name[i]);
        if(max<len)
        {
            max=len;
            j=i;
        }
        
    }
    return max;
}


int compare (char** string, int n)
{
    int max_len=longest_length(string);
    
    for(int j=0; j<max_len; j++)
    {
        for(int i=0; i<n; i++)
        {
            if(string[i][j]>string[i+1][j])
                return 1;
            else if(string[i][j]==string[i+1][j+1])
            {
                i++;
                j++;
            }
        }
    }
}

the main looks something like this:

int main() 
{
	int n;
	
	cout<<"How many names do you want to enter ? ";
	cin>>n;
	char** lastname = new char*[n];
	for(int i=0; i<n; i++)
		lastname[i]=new char[100];
		
	char ** firstname = new char*[n];
	for(int i=0; i<n; i++)
	    firstname[i]=new char[100];
		
	for(int i=0; i<n; i++)
	{
		cout<<"enter name #"<<i+1 <<" :";
	   	   cin>>firstname[i]>>lastname[i];
	   	   
	}
	
	for(int i=0; i<n; i++)
	{
	    cout<<firstname[i]<<" "<<lastname[i];
	    cout<<endl;
	}
	
}
Posted
Updated 8-Aug-21 11:52am

This question has already been discussed several times.

At last here:
How do I sort last names alphabetically and print them before first[^]
 
Share this answer
 
I would first write code to separate a line of text into a first name and a last name. What delineates the two? A space. That means you need to write a piece of code that takes a line of text, looks for a separating space (or spaces), and splits the line accordingly. To do this, I would save pointers to both of the names and during your scanning I would null out the spaces. That would leave you with two pointers to nulled c-style strings. You could write another function to provide the functionality of the strdup function which allocates just enough memory for a text string and copies the string into the new memory. Once you have the copied strings you can save those into an array of structures. The structures would contain the two pointers to the string. The last step will be to sort the array and that's easy enough.

There you have the basic algorithms. We are not here to do your homework for you so it's up to you to write the code.

If I were you I would make a text file with those inputs and read the data from the file instead of entering it every time. That always speeds up my debugging process.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900