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

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;
}
Posted Date: 3/3/2012 4:17:46 PM | Location : United States







Related Discussions:- Read world population data file. store the values in multipl, computer science, Assignment Help, Ask Question on Read world population data file. store the values in multipl, computer science, Get Answer, Expert's Help, Read world population data file. store the values in multipl, computer science Discussions

Write discussion on Read world population data file. store the values in multipl, computer science
Your posts are moderated
Related Questions
Choice of Medium: All the transmission channels discussed in the foregoing paragraphs can be used as point to point connections or as broadcast systems. Transmitting informati

Classification of Computers DIGITAL, ANALOG AND HYBRID Computers are of three basic types – Digital, Analog, and Hybrid. Current use of the word “computer” almost a

Example of flowcharting: Example Problem statement: To find out whether a given number is even or odd. Algorithm: Step 1 Start Step 2 INPUT the number n  Step 3

Bidirectional Search-Artificial intelligence: We've concentrated so far on searches where the point of the search is to search a solution, not the path to the solution. In anot

Artificial Intelligence Agents: We discussed what we will be talking about in Artificial Intelligence and why those things are important. This chapter is all about how we wi

Question 1 Write a note on transition, transition types & Features Question 2 Describe the different types of lens used in film shooting Question 3 Briefly explain

Each Many to Many relationship type maps into the Table. The primary key of this relevant is the combination of the primary keys of the participating entity types. These are also c

Hi I have a 8 question machine learning problem sheet due to the 25th of december. The exercises require knowledge of basic classifiers, logistic regression, naive bayes, mixture o

Question 1 How can you create surface with 3D Scanner in geometric modeling? Question 2 Explain kinematics modeling in virtual reality Question 3 Describe object hierarc