Description
While we are still focused on collections such as Arrays and Vectors, it is important to also consider how data is loaded into these structures. I will admit that other languages can make this task much easier. Let’s work with some real world examples.
The Files referred to below are in the item named “This week’s Assignment Resources” in this module.
I have included an industry standard file called airports.dat. It lists the major commercial airports in the world in a comma delimited format. Your assignment is to read this data and populate a vector with the 3 character airport code that is in the 5th ( 4th indexed) delimited position of the file. Review Chapter 5 – Reading Data from a File. If that airport code value happens to be empty or does not have a length of 3 characters, do not include it in your vector. Also, if you cannot read or find the file, you should exit the program.
Feel free to open the airports.dat file and see how it is structured.
Task 1: I have included a file called LoadAirports.cpp for this assignment. Modify this program to do the steps required for this assignment. Put your name in the heading comments and complete the //TODOs,
Read the attached file (AddingFileInVS.txt) on how I need you to open and plant the data file for your programs.
Task 2. Once you have this, sort the vector using built-in sort method. This method is not yet covered in your book. See https://en.wikipedia.org/wiki/Sort_(C%2B%2B) (Links to an external site.) for an example. Basically the syntax is:
sort (yourVectorName.begin(), yourVectorName.end());
Task 3, 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.
Task 4, In main(), create a loop and prompt for values to search for.
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.
The steps done or invoked from main() 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. Display 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.
7. Create a loop in main() to prompt to find valid Airport codes. Expect only uppercase input. Display “Found” or “Not found”. Use “X” to exit your loop. Display “Done” when complete.
Enter an Airport Code:oooo
Not found
Enter an Airport Code:DFW
Found
Enter an Airport Code:X
Done!
The Deliverable is your CPP file.
5pts Extra Credit: Use the available find method for the vector search. See C++ find
here is the code for the file LoadAirports.cpp
#define VERSION 1.02
#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;
}
/********************************
Main Body of Work
*/
int main() {
// Declare variables
vector < string > dataLineVector;
int lineCount = 0;
//TODO Create a vector of airport codes here
// …
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.fail()) {
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;
}
lineCount++;
dataLineVector = split(inputLine, “,”);
dataElement = dataLineVector[AIRPORT_CODE_ELEMENT];
// TODO
// You now have data in the variable named dataElement
// Validate that it is a length of 3 chars
// if valid add it to your created vector of airport codes
} while (true);
cout << “All airport code data has been extracted from ” << INPUT_FILE_NAME << endl;
cout << lineCount << ” lines processed.” << endl;
inputFile.close();
//TODO Display total number of validated airport codes
//TODO Check if Sorted -report/display as Sorted or Not Sorted
//TODO Sort Vector
//TODO Check if Sorted -report/display as Sorted or Not Sorted
//TODO Create a Loop to Search for Airport code. Report if Found or Not found. Use ‘X’ to exit.
return 0;
}