Question

C++ I need a program that will read a list of names from a data file and store them in an array. You will then sort the array by last name using a quicksort, and display the results, one name per line...

C++ I need a program that will read a list of names from a data file and store them in an array. You will then sort the array by last name using a quicksort, and display the results, one name per line.Each line has a first name and a last name.The file has 40 names in it right now, but the array should be able to accommodate up to 50

names.txt*****************************************************

Barbara Wright
Ian Chesterton
Steven Taylor
Sara Kingdom
Dodo Chaplet
Ben Jackson
Jamie McCrimmon
Victoria Waterfield
Zoe Heriot
Liz Shaw
Jo Grant
SarahJane Smith
Harry Sullivan
Kay Nine
Tegan Jovanka
Nyssa Traken
Kam Elion
Perpugilliam Brown
Melanie Bush
Ace McShane
Grace Holloway
Lucie Miller
Charlotte Pollard
Rose Tyler
Jack Harkness
Mickey Smith
Donna Noble
Martha Jones
Astrid Peth
Wilfred Mott
Amy Pond
Rory Williams
River Song
Craig Owens
Clara Oswald
Paternoster Gang
Bill Potts
Graham O'Brien
Ryan Sinclair
Yasmin Khan

**********************************************************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

INPUT
#######

Input file name used is input.txt

names Notepad File Edit Format View Help Barbara Wright IanChesterton Steven Taylor Sara Kingdom Dodo Chaplet Ben Jackson Jam

//####################### PGM START ########################################

#include<iostream>
#include<fstream>

using namespace std;

//function to partition the array of strings and sort it
int partition (string first[],string last[], int low, int high) {
  
   string pivot = last[high];    // pivot
    int i = (low - 1); // Index of smaller element

    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (last[j] <= pivot)
        {
            i++;    // increment index of smaller element
          
            //swaping last names
            string temp=last[i];
            last[i]=last[j];
            last[j]= temp;
          
            //swaping first names
            temp=first[i];
            first[i]=first[j];
            first[j]=temp;
          
        }
    }
    //swaping last names
    string temp=last[i+1];
    last[i+1]=last[high];
    last[high]= temp;
          
    //swaping first names
    temp=first[i+1];
    first[i+1]=first[high];
    first[high]=temp;
    return (i + 1);
}
//function to do the quicksorting of names based on last name
void quickSort(string first[],string last[],int low, int high)
{
    if (low < high)
    {
        int pi = partition(first, last, low, high);
        quickSort(first, last, low, pi - 1);
        quickSort(first, last, pi + 1, high);
    }
}
//function to print the names
void printNames(string first[], string last[], int size)
{
    int i;
    for (i=0; i < size; i++)
        cout<<first[i]<<" "<<last[i]<<"\n";
}
//main method
int main(){
   string firstname[50],lastname[50];
   int n=0;
  
   ifstream infile;
   infile.open("names.txt");
  
   if(infile.bad()){
       cout<<"Cant open names.txt file or file does not exist\n";
   }else{
      
       while((infile>>firstname[n]>>lastname[n]) && n<50){
           n+=1;
       }
   }
   quickSort(firstname,lastname,0,n-1);
   printNames(firstname,lastname,n);
  
   return 0;
}

//################################### PGM END ################################

OUTPUT
########

C:\UsersJijinAVA Documents\Dev Perpugilliam Brown Melanie Bush Dodo Chaplet Ian Chesterton Kam Elion Paternoster Gang JoGrant

Add a comment
Know the answer?
Add Answer to:
C++ I need a program that will read a list of names from a data file and store them in an array. You will then sort the array by last name using a quicksort, and display the results, one name per line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT