QUESTION

I can't seem to figure out the following C++ questions, followed by my codes. The program is running fine but is just not complete. I already have step 1-2b, but it'd be nice if you can double check.

Here is the problem:

sort the vector using built-in sort method. This method is not yet covered in your book. Basically the syntax is: sort (yourVectorName.begin(), yourVectorName.end());

Finally, Write a method that will validate that the vector is actually sorted. It should simply COUT that "Collection is sorted" or "Collection is NOT sorted". Call this method before you sort the array and also after you sort the array.

Be aware that unlike Arrays which are always passed by reference to a method, Vectors are passed by value by default. You must use the & indicator or return a vector to absorb changes to a vector made in a method.

Note there are no input prompts in this assignment. The steps are below. You should display what step you are on as the program proceeds. Note that steps 1 - 2b are provided for you in the LoadAirports.cpp program.

1. Open the File - Exit if error during open

2. Loop until end of file

a.Read a line from the file.

b. Pass the line to the split method

c. Obtain and validate specified data and place valid data into your new airportCode vector.

3. Report the number of elements in the vector that contains the airport codes

4. Check and report if vector is sorted or not sorted

5. Sort Vector

6. Check and report if vector is sorted or not sorted

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Provided Method - no need to change
vector < string > split(const string& s, const string& delim)
{
/*
Split lines of text using a delimiter
http://programmingknowledgeblog.blogspot.com/2013/04/the-most-elegant-way-to-split-string-in.html

This method will split a string and create (return) a vector of strings using a delimiter.
Thus the code split("HELLO,EARTH,AND FRIENDS", ",");
would return a vector of strings because the passed delimiter was ","
v[0] = "HELLO"
v[1] = "EARTH" ....
*/

vector < string > result;
if (delim.empty()) {
result.push_back(s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (!temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}

int main() {
// Declare variables
vector < string > dataLineVector;

//TODO Create a vector of airport codes here
// ...
vector < string > airPortCodes;

const string INPUT_FILE_NAME = "airports.dat";
const int AIRPORT_CODE_ELEMENT = 4;

string inputLine, dataElement;


// Open data file
cout << "Opening data file: " << INPUT_FILE_NAME << endl;
ifstream inputFile;
inputFile.open(INPUT_FILE_NAME.c_str());

if (!inputFile) { // some compilers (inputFile == 0)

cout << "ERROR - Unable to read from " << INPUT_FILE_NAME << "!" << endl;
cout << "Check to see if the file is missing or corrupted." << endl;
return 99;
}

// Read individual lines of data, extract the desired element and validate
cout << "Reading data from " << INPUT_FILE_NAME << endl;
do {
getline(inputFile, inputLine);
if (!inputFile) { // if End of File (EOF)
break;
}

dataLineVector = split(inputLine, ",");
dataElement = dataLineVector[AIRPORT_CODE_ELEMENT];

// TODO
// You now have data in the variable dataElement
// Validate that it is a length of 3 chars - just check size
// if valid add it to your created vector of airport codes

//COMMENT: check whether the vector's size is equal to 3
if (dataElement.size() == 3)
{
airPortCodes.push_back(dataElement);
cout << dataElement <<endl;
}
} while (true);

cout << "All airport code data has been extracted from " << INPUT_FILE_NAME << endl;
inputFile.close();

//TODO Display total number of validated airport codes
cout << "Total number of validated airport codes: " << airPortCodes.size() << endl;


//TODO Check if Sorted -report/display as Sorted or Not Sorted
//is_sorted algorithm determines whether the vector of airPortCodes is Sorted or not
if (is_sorted(airPortCodes.begin(), airPortCodes.end()))
{
cout << "Airport code vector is sorted" <<endl;
}
else
{
cout << "Airport code vector is not sorted" <<endl;
}

//TODO Sort Vector
//sort algorithm sorts the Vector of airPortCodes
sort(airPortCodes.begin(), airPortCodes.end());

//TODO Check if Sorted -report/display as Sorted or Not Sorted
if (is_sorted(airPortCodes.begin(), airPortCodes.end()))
{
cout << "Airport code vector is sorted" <<endl;
}
else
{
cout << "Airport code vector is not sorted" <<endl;
}

return 0;
}

This is the airports.dat file ( though this is just 10 lines out of 8000): You can just paste this into a Notepad and save it as airports.dat then load to the program to run your test with it if you need to.

1,Goroka,Goroka,Papua New Guinea,GKA,AYGA,-6.081689,145.391881,5282,10,U
2,Madang,Madang,Papua New Guinea,MAG,AYMD,-5.207083,145.7887,20,10,U
3,Mount Hagen,Mount Hagen,Papua New Guinea,HGU,AYMH,-5.826789,144.295861,5388,10,U
4,Nadzab,Nadzab,Papua New Guinea,LAE,AYNZ,-6.569828,146.726242,239,10,U
5,Port Moresby Jacksons Intl,Port Moresby,Papua New Guinea,POM,AYPY,-9.443383,147.22005,146,10,U
6,Wewak Intl,Wewak,Papua New Guinea,WWK,AYWK,-3.583828,143.669186,19,10,U
7,Narsarsuaq,Narssarssuaq,Greenland,UAK,BGBW,61.160517,-45.425978,112,-3,E
8,Nuuk,Godthaab,Greenland,GOH,BGGH,64.190922,-51.678064,283,-3,E
9,Sondre Stromfjord,Sondrestrom,Greenland,SFJ,BGSF,67.016969,-50.689325,165,-3,E
10,Thule Air Base,Thule,Greenland,THU,BGTL,76.531203,-68.703161,251,-4,E

Public Answer

RZZWZA The First Answerer