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

Memory of computer, Memory : Computers store information internally as wel...

Memory : Computers store information internally as well as externally. Linked to CPU is an internal memory unit where all the instructions and data are stored. It contains the

Super computers, Super Computers: The specialised demands and requirem...

Super Computers: The specialised demands and requirements of science, industry and military have led to the creation of powerful super computers. For numerically intensive com

Source and object programs, Source and Object Programs A set of instruc...

Source and Object Programs A set of instructions in a high-level language are called “Source program”. Since high-level languages are machine independent, it is required to fir

Data output, Data Output : Processed data is no use to you if you cannot u...

Data Output : Processed data is no use to you if you cannot use it. Once data has been processed, you will either: (1) Send it as a data file to another system, e.g. write a fi

Magnetic core memory, MAGNETIC CORE MEMORY: This type of memory is used...

MAGNETIC CORE MEMORY: This type of memory is used extensively in airborne digital systems, although integrated circuits are being developed with most modern aircraft systems.

Explain data movement ?, In any program it is essential to move the data in...

In any program it is essential to move the data in the memory and in the CPU registers; there are a number of ways to do this: it can copy data in the memory to a number of registe

Flow decomposition technique to list the cycles, QUESTION (a) Consider ...

QUESTION (a) Consider the graph below, use the Flow Decomposition technique to list the cycles and paths produced.                                                (b) Pr

Library function, Library function :   These are the functions supplied...

Library function :   These are the functions supplied with the programming language. The code or definition of library functions does not have to be written in a user program w

Exercise 2: Green cloud computing, Ask questioMost part of power consumptio...

Ask questioMost part of power consumption in data centers comes from computation processing, disk storage, network and cooling systems. Nowadays, there are new technologies and met

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