Question

Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit...

Language: C++

You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit ID# and a phone# in the format of ###-###-####. The data were generated randomly so there might be duplicates in the IDs. You're asked to do the following by using standard library algorithms as much as possible:

1. read the file into a map which has an integer for key (ID#) and a string for value (phone#), this allows the duplicates to be removed. Print the map's size.

2. print the count of phone numbers with the area code of 347.

3. print someone's ID, given the person's phone #: 212-536-6331. Note that there might be more than one ID with this phone# (like siblings sharing a home number), and you need to list them all.

4. create a vector that stores all phone numbers with area code 212, from the map. Print the size of the vector.

5. create another vector from step 4's result, with all duplicates of 212 numbers removed. Print the size of this vector.

Please produce your program output in the following format:

Size of the map: ???

Count of phone# with area code 347: ???

The ID(s) with the given phone#: ???

Size of the vector with all (212) numbers: ???

Size of the vector with unique (212) numbers: ???

Message me for the phoneno.txt, cant post it here

Homework Answers

Answer #1

Hey There,

- I understood your question and I have given the answer to the best of my knowledge.

- Since the "phoneno.txt" was not provided I have assumed the content of it. I have also attached the file that I have used to check the correctness of my code. Please run the following code on your main phoneno.txt file.

- If you face any problem while running it on your text file or you need any further explanations then do let me know in the comments box. I will be more than happy to help you.

- I have also put comments in my code so that you can also understand what I did in the code.

Answer:

- phoneno.txt file that I have used

- If your "phoneno.txt" file looks different that mine then just comment how your file looks and provide 4-5 rows as a sample. I will edit my code accordingly.

11111 123-456-7891
22222 234-567-8912
33333 345-678-9123
44444 456-789-1234
11111 123-456-7892
55555 347-146-4548
66666 347-583-5698
77777 123-456-7892
88888 212-154-5942
99999 212-498-4851
12111 212-154-5942
13111 456-789-1234

- Main file:

#include<bits/stdc++.h>
using namespace std;

int main(){
    // Initializing necessary variables 
    ifstream fin; 
    string line;
    int numbers = 0;
    map<int, string> m1;
    vector<int> list;
    vector<string> area_212;
    set<string> area_212_set;
    
    // Opening the "phoneno.txt" file
    fin.open("phoneno.txt"); 
  
    // This loop will run to the end of the file
    while(fin) { 
        // Reading a line from the file
        getline(fin, line); 
        
        // If the length of line is 0 then moving to next line
        if(line.length() == 0)
            continue;
        
        // Dividing whole line into 2 parts
        // 1st part = 5 digit integer id
        // 2nd part = 12 digit string phone number
        stringstream geek(line); 
        int id = 0; 
        geek >> id;
        string number = line.substr(6, 12);
        
        // Storing the data into the map
        m1[id] = number;
        
        string areaCode = line.substr(6, 3);
        
        // Counting all the numbers that has areaCode 347  
        if(areaCode == "347"){
            numbers++;
        }
        
        // Counting all the numbers that has areaCode 212 
        if(areaCode == "212"){
            area_212.push_back(number);
            area_212_set.insert(number);
        }
    } 
    
    // Printing ID(s) for the given phone number
    for(auto i : m1){
        if(i.second == "456-789-1234"){
            list.push_back(i.first);
        }
    }
    
    // SHOWING OUTPUTS AS MENTIONED IN THE QUESTION
    
    // 1st
    cout << "Size of the map: " << m1.size() << endl;
    
    // 2nd
    cout << "Count of phone# with area code 347: " << numbers << endl;
    
    // 3rd
    cout << "The ID(s) with the given phone#: ";
    for(int i=0; i<list.size(); i++)
        cout << list[i] << " ";
        
    // 4th
    cout << "\nSize of the vector with all (212) numbers: " << area_212.size() << endl;
    
    //5th
    cout << "Size of the vector with unique (212) numbers: " << area_212_set.size() << endl;
    
    // Close the file 
    fin.close(); 
}

Screenshot of running the code correctly:

- You can also rectify the output based on the given "phoneno.txt" file.

Hope it helps:)

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT