question archive Option 1: Sorted Phone List Write C++ program that prints a sorted phone list from a database of names and phone numbers

Option 1: Sorted Phone List Write C++ program that prints a sorted phone list from a database of names and phone numbers

Subject:Computer SciencePrice:6.86 Bought8

Option 1: Sorted Phone List

Write C++ program that prints a sorted phone list from a database of names and phone numbers. The data is contained in two files named "phoneNames.txt" and "phoneNums.txt". The files may contain up to 2000 names and phone numbers. The files have one name or one phone number per line. To save paper, only print the first 50 lines of the output.

Note: The first phone number in the phone number file corresponds to the first name in the name file. The second phone number in the phone number file corresponds to the second name in the name file. etc. You will find the test files in the Asn Five.zip 

 

EXAMPLE OUTPUT:

A Ablon             614-604-2400

A Bender             925-480-3800

A Emmerson           530-367-7000

A King              582-436-7724

A Meyerson           213-840-5000

A Perenchio          310-558-6868

Aart DeGeus          850-982-5000

Aileen Adams         918-853-2838

Ake Persson          757-332-5000

Akop Pogosyan        717-669-8450

Al Carter            909-599-8676

Alan Austin          850-493-9300

Alan Bersin          819-625-6580

Alan Dachs           415-274-7500

Alan Fohrer          949-652-5577

Alan Karp            614-542-3355

Alan Lowe            407-282-8600

Alan Rasmussen       209-375-8800

Alexander Lidow      310-322-3331

Alexis Lodde         606-783-7970

...

 

End program.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

An oftsream object 'f' is taken which corresponds to a file named 'phoneNames.txt', and another ofstream object 'f1' is taken which corresponds to a file named 'phoneNums.txt'. Details of 2000 names and phone numbers are written into both the files randomly. Now two ifstream objects are taken and all the 2000 data are read from the files to two string arrays.

 

 

Then, the string are sorted based on ascending order of their names. Then, top 50 details are printed which are in ascending order as output to the console.

 

Code:

#include <iostream>

using namespace std;

#include <fstream>

#include <algorithm>

#include <iomanip>

int main()

{

    /* to write 2000 phone numbers and corresponding anmes into the respective files */

    ofstream f("phoneNames.txt");

    ofstream f1("phoneNums.txt");

    /* randomly writing the details */

    for(int i=0;i<2000;i++)

    f<<char((rand()%26)+65)<<char(rand()%26+97)<<char(rand()%25+97)

    <<char(rand()%23+97)<<char(rand()%14+97)<<endl;

    f.close();

    for(int i=0;i<2000;i++)

    f1<<rand()%9<<rand()%9<<rand()%9<<rand()%9<<rand()%9<<rand()%9<<rand()%9<<rand()%9<<rand()%9

    <<rand()%9<<endl;

    f1.close();

   

    /* to read the data from the files */

    ifstream p;

    ifstream p1;

    p.open("phoneNums.txt");

    p1.open("phoneNames.txt");

    string s1[2000],s2[2000];

    /* taking the details into the string arrays */

    for(int i=0;i<2000;i++)

    {

        string s;

        p>>s;

        string ss;

        p1>>ss;

        s1[i]=s;

        s2[i]=ss;

    }

    /* to sort the details based on names of people */

    for(int i=0;i<50;i++)

    {

        for(int j=0;j<50-1-i;j++)

        {

            if(s1[j]>s1[j+1])

            {

                string temp=s1[j];

                s1[j]=s1[j+1];

                s1[j+1]=temp;

                string temp1=s2[j];

                s2[j]=s2[j+1];

                s2[j+1]=temp1;

            }

        }

    }

    /* printing the top 50 details of people names and numbers */

    cout<<"Top 50 list of names and phone number of sorted order:"<<endl<<endl;

    for(int i=0;i<50;i++)

    {

        cout<<left;

        cout<<setw(15)<<s1[i]<<setw(10)<<s2[i]<<endl;

    }

}

 

Output:

Please see the attached file for the complete solution