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;
}
}
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.
Get Answers For Free
Most questions answered within 1 hours.