In: Computer Science
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;
}
}
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
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;
}
}