Read world population data file. store the values in multipl, computer science, Basic Computer Science

Assignment Help:
Read World Population Data File. Store the
values in multiple vectors. Sort them by most
populated region and output the results

ext file is here : https://www.csupomona.edu/~hmzaidi/Winter2012/CS128/WorldPopulation2010.txt

please
show me the code which is working perfectly.

please follow under this code


#include
#include
#include
#include
#include
#include
using namespace std;

int main()
{
// Define vectors to hold the data
vector countryNames;
vector countryCodes;
vector countryPopulations;

// Open the input file
string filename =
"C:\\Teaching\\CSUPomona\\CS128\\Winter2011\\WorldPopulation2010.txt";
ifstream in_file;
in_file.open(filename.c_str(), ios::in);

// Read the header
string header;
getline(in_file, header);

string line;
while (getline(in_file, line))
{
cout << line << endl;

string countryName;
string countryCode;
string countryPopulation;

// Parse the line to extract country name, code & population
for (int i = 0; i < line.length(); i++)
{
if (isdigit((unsigned char)line[i]))
{
countryName = line.substr(0, i);
countryCode = line.substr(i, 3);
countryPopulation = line.substr(i + 3, line.length() - i - 3);

countryNames.push_back(countryName);
countryCodes.push_back(atoi(countryCode.c_str()));
countryPopulations.push_back(atof(countryPopulation.c_str()));

break;
}
}
}

// Sort the vectors based on population
for (int i = 0; i < countryPopulations.size(); i++)
{
for (int j = i + 1; j < countryPopulations.size(); j++)
{
if (countryPopulations[j] < countryPopulations[i])
{
double tempP = countryPopulations[j];
countryPopulations[j] = countryPopulations[i];
countryPopulations[i] = tempP;

int tempC = countryCodes[j];
countryCodes[j] = countryCodes[i];
countryCodes[i] = tempC;

string tempN = countryNames[j];
countryNames[j] = countryNames[i];
countryNames[i] = tempN;
}
}
}

// Write the vectors to a file

string out_filename =
"C:\\Teaching\\CSUPomona\\CS128\\Winter2011\\WorldPopulation2010Sorted.txt";

ofstream out_file;
out_file.open(out_filename.c_str(), ios::out);

for (int i = 0; i < countryPopulations.size(); i++)
{
out_file << countryNames[i] << " "
<< countryCodes[i] << " " << countryPopulations[i] << endl;
}

char key;
cin >> key;

return 0;
}

Related Discussions:- Read world population data file. store the values in multipl, computer science

Describe CPU registers , The CPU has four internal registers, each of 16 bi...

The CPU has four internal registers, each of 16 bits. The first four, AX, BX, CX, and DX are common use registers and can also be used as 8 bit registers, if used in such a way it

Pseudocode, Design a program that will read a file of sales records and pro...

Design a program that will read a file of sales records and produce a sales report. Each record in the file contains a customer’s ID, name, a sales amount, and a validated GST code

How can we create a fcb file?, Creating a new file For the formation of fil...

Creating a new file For the formation of files the 21H interruption 16H function is used. DX must identify a control structure whose necessities are that as a minimum the logic uni

SG Fuel power supply, Where can I find power supply HIPRO HP-W460GC33 for S...

Where can I find power supply HIPRO HP-W460GC33 for Silicon Graphics Fuel computer

Discuss about the descriptor classes, Question 1 List down any six feature...

Question 1 List down any six features of a Smartphone Question 2 Give a brief note on a) Internal Flash Disk. b) Use of C++ in Symbian OS Question 3 What are the naming

Ipv4 and ipv6, IPv6 documented in 1994 (RFC 1752) is still a collection...

IPv6 documented in 1994 (RFC 1752) is still a collection of Draft and Proposed standards Several pre-standard implementations exist IPv6 was intended to be a pragmatic up

Area of curve, Write a program to find the area under the curve y = f(x) be...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Impact printers and non - impact printers, Impact Printers and non - Impact...

Impact Printers and non - Impact Printers: Impact Printers: These are printers in which the print-head strikes a ribbon, and include the daisywheel and thimble printer

What is Semaphore and explain with example?, Description A semaphore is a...

Description A semaphore is a confined variable whose value can be accessed and changed only by the operations P and V and initialization operation known as ''Semaphoiinitislize''.

State the importance of visual storytelling, Question 1 What are the vario...

Question 1 What are the various steps involved in pre-production design? Question 2 What are the different kinds of perspectives used in a layout? Question 3 Descr

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd