This is the assignment Read World Population Data File. Store the values in mult
ID: 3639965 • Letter: T
Question
This is the assignmentRead World Population Data File. Store the values in multiple vectors. Sort them by most populated region and output the results
Here is my code please take a look in the code
I have created dome functions for the sorting but I am not sure if they are right and how to use to reprint everything again but by the most populated one please help me to finish it
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <algorithm>
using namespace std;
/**
Converts string to an integer value
parameter is countryCode a string representing an integer number
return the integer number
**/
int string_to_int(string countryCode)
{
istringstream strm;
strm.str(countryCode);
int countryCodes = 0;
strm >> countryCodes;
return countryCodes;
}
/**
Converts string to a floating value
parameter is countryPopulation a string representing a floating number
return the floating number
**/
double string_to_double(string countryPopulation)
{
istringstream strm;
strm.str(countryPopulation);
double countryPopulations;
strm >> countryPopulations;
return countryPopulations;
}
/*
Description: swaps two double values
Parameters:
countryPopulations: vector of double values
i: array index for the first value
j: array index for the second value
Return Value: None
*/
void swap(double& i, double& j )
{
double temp = countryPopulations[i];
countryPopulations[i] = countryPopulations[j];
countryPopulations[j]=temp;
// copy value at cell i to a new variable
// copy value at cell j to cell i
// copy value from new variable to cell j
}
/**
Gets the position of the highest element in a vector range
parameter: countryPopulations the vector
the begining of the range
the end of the range
returns the position of the highest element in the
range countryPopulations[from].....countryPopulations[to]
**/
double highest_position( vector<double>& countryPopulations, double from, double to)
{
double max_pos = from;
int i
for( i = from + 1; i < = to; i++)
if(countryPopulations[i] > countryPopulations[max_pos]) max_pos = i
return max_pos;
}
/**
Description: sorts vector values from the most populated to the lowest populated
Parameters: countryPopulations the vector to sort
Return Value: None
**/
void selection_sort(vector<double>& countryPopulations)
{
double next; //the next position to be set to the maximum
for (next=0; next < countryPopulations.size() - 1; next++)
{
/*find the position of the highest populations */
double max_pos = max_position(countryPopulations, next, countryPopulations.size() - 1);
if (max_pos != next)
swap(countryPopulations[max_pos], countryPopulations[next]);
}
}
int main()
{
//Define vectors to hold the data adquire for file and be able to sort them later
vector <string> countryNames;
vector <int> countryCodes;
vector <double> countryPopulations;
//Open the file and Reading from file
string filename = "C:\WorldPopulation2010.txt";//declares where can the file be found
ifstream in_file; //open a file variable for input
infile.open(filename.c_str()); //ios::in);//open the file to read it and for input what is ios for? is option I dont need it
//Reading from file line by line
//1st Read the header
string header;//variable declaration
getline(in_file, header);//Reads the entire line in the file
string line;
while (getline(in_file, line)//Read the whole line after the header
{
cout << line << endl;//prints the line on the console
//declaration of variables
string countryName;
string countryCode;
string countryPopulation;
//Extract the data from the string
int i = 0;
while ((!isdigit(line[i])) && (i < line.length())) {i++}//Locate the first digit and we use && so the line would not go on
string countryName = line.substr(0, i);//extract country Name
string countryCode = line.substr(i, 3);//extract code
string countryPopulation = line.substr(i+3, line.lenghth - i - 3);//extract country population
//Because population and codes are store as a string we need to convert
//them into integer and float and that is why I have functions
countryCodes = string_to_int(line.substr(i,3));
countryPopulations = string_to_float(line.substr(i+3, line.length -i -3);
}
//Sort Vectors by most populated region and print out on the console
for (int i=0, i< countryPopulations.size; i++)
selection_sort( countryPopulations);
//I dont know how to type the code here to print out for
//Country Name Country code Country populations
//from the mos populated to the least populated
//can you help me please?
// Write the vectors to a file
string out_filename =
"C:\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;
}
This is a small example of how the text file looks
"Major area, region, country or area" Country code 2010
WORLD 900 6895889.02
More developed regions 901 1235899.87
Less developed regions 902 5659989.15
Least developed countries 941 832329.58
"Less developed regions, excluding least developed countries" 934 4827659.58
"Less developed regions, excluding China" 948 4287840.92
Sub-Saharan Africa 947 856327.16
AFRICA 903 1022234.40
Eastern Africa 910 324044.32
Burundi 108 8382.85
Comoros 174 734.75
Djibouti 262 888.72
Eritrea 232 5253.68
Ethiopia 231 82949.54
Kenya 404 40512.68
Madagascar 450 20713.82
Malawi 454 14900.84
Mauritius 480 1299.17
Mayotte 175 204.11
Mozambique 508 23390.77
Réunion 638 846.07
Rwanda 646 10624.01
Seychelles 690 86.52
Explanation / Answer
Next time before writing a function check the algorithms library, don't reinvent the wheel. #include #include #include #include #include #include #include #include #include using namespace std; /** Converts string to an integer value parameter is countryCode a string representing an integer number return the integer number **/ int string_to_int(string countryCode) { istringstream strm; strm.str(countryCode); int countryCodes = 0; strm >> countryCodes; return countryCodes; } /** Converts string to a floating value parameter is countryPopulation a string representing a floating number return the floating number **/ double string_to_double(string countryPopulation) { istringstream strm; strm.str(countryPopulation); double countryPopulations; strm >> countryPopulations; return countryPopulations; } /* Description: swaps two double values Parameters: countryPopulations: vector of double values i: array index for the first value j: array index for the second value Return Value: None */ /*void swap(double& i, double& j ) { double temp = i; i = j; j=temp; THERE IS ALREADY A SWAP FUNCTION IN ALGORITHMS // copy value at cell i to a new variable // copy value at cell j to cell i // copy value from new variable to cell j }*/ /** Gets the position of the highest element in a vector range parameter: countryPopulations the vector the begining of the range the end of the range returns the position of the highest element in the range countryPopulations[from].....countryPopulations[to] **/ double highest_position( vector& countryPopulations, double from, double to) { double max_pos = from; int i; for( i = from + 1; i countryPopulations[max_pos]) max_pos = i; return max_pos; } /** Description: sorts vector values from the most populated to the lowest populated Parameters: countryPopulations the vector to sort Return Value: None **/ void selection_sort(vector& countryPopulations,vector & countryNames,vector & countryCodes) { double next; //the next position to be set to the maximum for (next=0; nextRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.