Question

Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...

Write up to three lines to explain the logic used behind those codes.       

1) #include <iostream>

#include <string>

#include <fstream>

#include <vector>

#include <sstream>

using namespace std;

int main()

{

        ifstream infile("worldpop.txt");

        vector<pair<string, int>> population_directory;

        string line;

        while(getline(infile, line)){

                if(line.size()>0){

                        stringstream ss(line);

                        string country;

                        int population;

                        ss>>country;

                        ss>>population;

                        population_directory.push_back(make_pair(country, population));

                }

        }

        cout<<"Task 1"<<endl;

        cout<<"Names of countries with population>=1000,000,000"<<endl;

        for(int i=0;i<population_directory.size();i++){

                if(population_directory[i].second>=1000000000){

                        cout<<population_directory[i].first<<endl;

                }

        }

        cout<<"Names of countries with population<=1000,000"<<endl;

        for(int i=0;i<population_directory.size();i++){

                if(population_directory[i].second<=1000000){

                        cout<<population_directory[i].first<<endl;

                }

        }

}

2)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
 
using namespace std;
int main()
{
        ifstream infile("worldpop.txt");
        vector<pair<string, int>> population_directory;
        string line;
        while(getline(infile, line)){
                if(line.size()>0){
                        stringstream ss(line);
                        string country;
                        int population;
                        ss>>country;
                        ss>>population;
                        population_directory.push_back(make_pair(country, population));
                }
        }
        cout<<"Task 2"<<endl;
        cout<<"Names of first 10 countries"<<endl;
        for(int i=0;i<10;i++){
                cout<<population_directory[i].first<<endl;
        }       
}

3)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
 
using namespace std;
int main()
{
        ifstream infile("worldpop.txt");
        vector<pair<string, int>> population_directory;
        string line;
        while(getline(infile, line)){
                if(line.size()>0){
                        stringstream ss(line);
                        string country;
                        int population;
                        ss>>country;
                        ss>>population;
                        population_directory.push_back(make_pair(country, population));
                }
        }
        cout<<"Task 3"<<endl;
        cout<<"Names of last 10 countries"<<endl;
        for(int i=population_directory.size()-11;i<population_directory.size();i++){
                cout<<population_directory[i].first<<endl;
        }
}

Homework Answers

Answer #1

There are three cpp programs. All are doing common thing reading a file called "worldpop.txt" and keepig the data as country and population pair in vector popoulation_directory .

1. Except the above common stuff, the first program loops through the vector data and checks if the second entry population count is greater than or equal to one million, then displaying the names of the countries with greater than or equal to one million population.

2.Except the common stuff reading file data and storing in vector, this program loops through the vector data ten times and displaying the first ten country names.

3. Except the common stuff reading file data and storing in vector, third program loops through the vector data starting from last 10 lines upto last line, then displaying the last ten country names in the vector.

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
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile;...
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; }...
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is ";...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }