Question

In: Computer Science

Write up to 4 or 5 lines to explain the logic used behind those codes. Separately...

Write up to 4 or 5 lines to explain the logic used behind those codes. Separately for each please

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;
        }
}

Solutions

Expert Solution

Ok. I will help you. For each question, first, I will give a small explanation of what the whole program is doing, And along with that, I will add the same code you given with comments in it for better understanding.

Program 1 :

This program will open the file "worldpop.txt" which contains the country and population in each line.

A vector was created, where each item will be a pair of string and int. And this program will print two outputs

  1. Names of countries with a population greater than or equal to 1000,000,000
  2. Names of countries with population less than or equal to 1000,000

code:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
    ifstream infile("worldpop.txt"); // open the file in the read mode
    vector<pair<string, int>> population_directory; // vector to store the contens of each line
    string line;
    while (getline(infile, line))   //read each line in the file
    {
        if (line.size() > 0)        // check if the line read was not empty
        {
            stringstream ss(line);  //Convert the line to the stringstream for easy type casting to other datatypes 
            string country;
            int population;
            ss >> country;          //converting the stringstream to string
            ss >> population;       //converting the stringstream to int type
            population_directory.push_back(make_pair(country, population)); // pushing the pair to the vector
        }
    } // after this loop, we will be having the vector with all the lines parsed and stored in it
    cout << "Task 1" << endl;
    cout << "Names of countries with population>=1000,000,000" << endl;
    for (int i = 0; i < population_directory.size(); i++)
    {
        //Retrieve each content in the vector
        // check if the population was above the required one 
        if (population_directory[i].second >= 1000000000)
        {
            cout << population_directory[i].first << endl; // Print the name of the country
        }
    }
    cout<< "Names of countries with population<=1000,000" << endl;
    for (int i = 0; i < population_directory.size(); i++)
    {
        //Retrieve each content in the vector
        // check if the population was below the required one 
        if (population_directory[i].second <= 1000000)
        {
            cout << population_directory[i].first << endl;// Print the name of the country
        }
    }
}

Program 2:

This program will open the file "worldpop.txt" which contains the country and population in each line.

A vector was created, where each item will be a pair of string and int.

And this program will print output which are the names of the first 10 countries in the file.

Code:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
    ifstream infile("worldpop.txt");                // open the file in the read mode
    vector<pair<string, int>> population_directory; // vector to store the contens of each line
    string line;
    while (getline(infile, line)) //read each line in the file
    {
        if (line.size() > 0) // check if the line read was not empty
        {
            stringstream ss(line); //Convert the line to the stringstream for easy type casting to other datatypes
            string country;
            int population;
            ss >> country;                                                  //converting the stringstream to string
            ss >> population;                                               //converting the stringstream to int type
            population_directory.push_back(make_pair(country, population)); // pushing the pair to the vector
        }
    } // after this loop, we will be having the vector with all the lines parsed and stored in it
    cout << "Task 2" << endl;
    cout << "Names of first 10 countries" << endl;
    for (int i = 0; i < 10; i++)
    {
        // Iterate through the vector and prints the first 10 items (only the country name)
        cout << population_directory[i].first << endl;
    }
}

Program 3:

This program will open the file "worldpop.txt" which contains the country and population in each line.

A vector was created, where each item will be a pair of string and int.

Iterate through the vector and prints the last 10 items (only the country name)

for this to happen , we initialize the looping variable i to the size of the vector - 11 ,

becase then only we will get 10 elements. Then print those items

Code:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
    ifstream infile("worldpop.txt");                // open the file in the read mode
    vector<pair<string, int>> population_directory; // vector to store the contens of each line
    string line;
    while (getline(infile, line)) //read each line in the file
    {
        if (line.size() > 0) // check if the line read was not empty
        {
            stringstream ss(line); //Convert the line to the stringstream for easy type casting to other datatypes
            string country;
            int population;
            ss >> country;                                                  //converting the stringstream to string
            ss >> population;                                               //converting the stringstream to int type
            population_directory.push_back(make_pair(country, population)); // pushing the pair to the vector
        }
    } // after this loop, we will be having the vector with all the lines parsed and stored in it
    cout << "Task 3" << endl;
    cout << "Names of last 10 countries" << endl;
    // Iterate through the vector and prints the last 10 items (only the country name)
    // for this to happen , we initialize the looping variable i to the size of the vector - 11 ,
    // becase then only we will get 10 elements. Then print those items
    for (int i = population_directory.size() - 11; i < population_directory.size(); i++)
    {
        
        cout << population_directory[i].first << endl;
    }
}

Related Solutions

Write up to 10 lines to explain the logic used behind this code pls!!!!! *****************************************/ #include...
Write up to 10 lines to explain the logic used behind this code pls!!!!! *****************************************/ #include <iostream> #include <string> #include <iomanip> #include<activaut.h> #include<activdbg.h> using namespace std; int main() {     int MatchesPlayed[6], MatchesWon[6], MatchesLost[6], MatchesDraw[6], MatchesPoints[6], TotalPoints[6];     MatchesWon[0] = 3; MatchesLost[0] = 0; MatchesDraw[0] = 1; //India     MatchesWon[1] = 2; MatchesLost[1] = 0; MatchesDraw[1] = 1; //NZ     MatchesWon[2] = 0; MatchesLost[2] = 3; MatchesDraw[2] = 0; //Australia     MatchesWon[3] = 1; MatchesLost[3] = 2; MatchesDraw[3] = 1;...
Write up to 10 lines to each code explain the logic used behind these code pls!!!!!...
Write up to 10 lines to each code explain the logic used behind these code pls!!!!! (10 lines for each please) 1. #include<iostream> using namespace std; int main() {        int id, reverse[20], count = 0, min = 0, max = 0;        cout << "\nStudent ID Number is \n";        cin >> id;        while (id != 0)        {              reverse[count] = id % 10;              if (count == 0)              {                     min = reverse[count];                     max...
Write up to 10 lines to explain the logic used behind this code pls!!!!! #include <iostream>...
Write up to 10 lines to explain the logic used behind this code pls!!!!! #include <iostream> #include <iomanip> #include<cmath> #include<AclUI.h> using namespace std; int main() {     int array[][2] = { {29,60} , {33,80} , {45,90} , {57,52} , {12,44} , {21,78} , {32,64} , {17,59} }; //given values     int Totalstudents = 0,TotalPass = 0, Average = 0;     for (int i = 0; i < 8; i++)      //students enrooled     {          Totalstudents += array[i][0];     }    ...
what is quality child care? write 4-5 lines.
what is quality child care? write 4-5 lines.
List and explain the logic behind the “four functions of money.” Then, consider the following: In...
List and explain the logic behind the “four functions of money.” Then, consider the following: In many prisons, cigarettes are used as money. Do cigarettes in prison have the ability to satisfy these four functions? Explain.
1. Explain the logic behind the application purchasing power price theory to explain changes in the...
1. Explain the logic behind the application purchasing power price theory to explain changes in the spot exchange rate. 2. Assume US interest rates are generally above foreign interest rates. a. what does this sideway about the future strength or weakness of the dollar based on the IFE (international Fisher effect)? b. should US investors invest in foreign securities if they believe in the IFE? c. should foreign investors invest in US securities if they believe in the IFE?
Can someone explain the logic behind this question? The addition of 1 g of which of...
Can someone explain the logic behind this question? The addition of 1 g of which of the following compounds to distilled water would cause the least change in pH? a) Cs(OH) b)HCl c) LiH d) LiOH I know that HCL is a strong acid which elimates that since it will affect PH. In class we had to memorize that group I metals with OH are strong bases making which LiOH and CsOH follow. The answer is A. The solution to...
Explain the logic behind the net present value decision rule and the internal rate of return...
Explain the logic behind the net present value decision rule and the internal rate of return decision rule.
Explain the logic behind the Bass diffusion model. How is the model useful to marketing managers?...
Explain the logic behind the Bass diffusion model. How is the model useful to marketing managers? What are the managerial implications of the model?
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT