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

Why is the random class in the .net framework not suitable, QUESTION (a...

QUESTION (a) (i) Why is the Random class in the .NET framework not suitable for generating random bytes for cryptography purposes? (ii) Mention two characteristics required

Environment for intelligent agents-artificial intelligence, Environments ...

Environments We have seen that intelligent agents might take into account certain information when   choosing   a   rational   action, by  including information from its sensor

Artificial intelligence-representations/languages used, Many persons are ta...

Many persons are taught AI with the opening line: " The three most important things in AI are representation, representation and representation ". While selecting the method of

Deadlocks, What is methods For handling Deadlocks?

What is methods For handling Deadlocks?

What are anti-patterns, QUESTION Developers spend much more time extend...

QUESTION Developers spend much more time extending and changing code than they did originally while developing it. (a) As a team leader, illustrate how you will introduce to

Unix, How do I change file permissions? Every time I execute chmod commands...

How do I change file permissions? Every time I execute chmod commands it tells me permission denied.

Explain ring counter with diagrams, Question 1 Convert the following hexad...

Question 1 Convert the following hexadecimal numbers to base 2 (D73)16 (A21)16 Question 2 Convert the following binary numbers to base 16 (11001011)2 (11

Write a long note on the types of charts, Question 1 Write a long note on ...

Question 1 Write a long note on the security features of Windows XP Question 2 Write a long note on computer peripherals, briefly describing some of the devices Question

Theory of computation, I define a restricted form of TMs M as follows. Give...

I define a restricted form of TMs M as follows. Given any input x on the tape of M, the initial portion of the tape that holds x is read-only and one-way. That is, M cannot write o

What is nanp? explain, Question 1 What is NANP? Explain Question 2 ...

Question 1 What is NANP? Explain Question 2 What is E&M signaling? Discuss in brief Question 3 What are the common custom calling features? Question 4 Expl

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