Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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 : http://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;
}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9503601

Have any Question?


Related Questions in Computer Engineering

A chemistry student needsnbsp550 gnbspof dimethyl sulfoxide

A chemistry student needs 55.0 g of dimethyl sulfoxide for an experiment. By consulting the  CRC Handbook of Chemistry and Physics , the student discovers that the density of dimethyl sulfoxide is 1.10 g.cm^-3. Calculate ...

Be as specific as you can for the following questions

Be as specific as you can for the following questions, explain each concept in detail and how you arrive at your solutions. This is JUST as important as your actual answers. You are provided a unit square domain where th ...

Are us executives paid too much particularly compared to

Are U.S. Executives paid too much particularly compared to the average worker in their organization?

How do you calculate the number of atoms given the weight

How do you calculate the number of atoms given the weight of an element, such as nitrogen?

Show that set cover can be polynomial-time reduced to

Show that Set Cover can be polynomial-time reduced to CNF-SAT (CNF-SAT is essentially 3SAT without the restriction of having at most 3 literals per clause). Hint: For each set Si in the Set Cover instance, let there be a ...

Question suppose a node is on the same lan as 973939166

Question : Suppose a node is on the same LAN as 97.39.39.166 needs 97.39.39.166's MAC address. What would the destination address of the ARP query be (link layer)? The response must be typed, single spaced, must be in ti ...

Question suppose that you have called a function called

Question : Suppose that you have called a function called sphere that creates (stores points and normals in a vertex Array and in the buffer) a sphere of radius 1 centered at the origin. Write the portions of the render ...

Do a system model in terms of states inputs outputs next

Do a system model in terms of: States, Inputs, Outputs, Next state (state transition) function, and Readout (output) function. Define the states, inputs and outputs in terms of a description and as formal mathematical se ...

How many electrons in a tellurium te atom have a principal

How many electrons in a tellurium (Te) atom have a principal quantum number of n = 4 and a magnetic quantum number of m l  = 0? a)2 b) 3 c) 6 d) 9 e) 18 Why does Beryllium (Be) have a positive electron affinity? ( That i ...

A chemist weighed outnbsp779 g of calcium calculate the

A chemist weighed out 77.9 g of calcium. Calculate the number of moles of calcium she weighed out. Round your answer to 3 significant digits.

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As