c++ While we are still focused on collections such as Arrays and Vectors, it is
ID: 3744070 • Letter: C
Question
c++
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.
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.
I have included a file called InsertCode.txt that will assist in parsing the file. Simply place this split method in your program and include the "include" statements. When you read a line, then call the split method passing the string you just read and specify the comma (",") delimiter. It will return a vector and you can inspect that for proper values. If valid, load the 3 character value into your own vector.
Read the attached file (AddingFileInVS.txt) on how I need you to open and plant the file for your programs.
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) for an example. 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.
1. Open the File - Exit if error opening
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 size of vector
4. Check if vector is sorted
5. Sort Vector
6. Check if sorted
AddingFileInVS.txt
InsertCode.txt
airports.dat
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
11,Akureyri,Akureyri,Iceland,AEY,BIAR,65.659994,-18.072703,6,0,N
12,Egilsstadir,Egilsstadir,Iceland,EGS,BIEG,65.283333,-14.401389,76,0,N
13,Hornafjordur,Hofn,Iceland,HFN,BIHN,64.295556,-15.227222,24,0,N
14,Husavik,Husavik,Iceland,HZK,BIHU,65.952328,-17.425978,48,0,N
15,Isafjordur,Isafjordur,Iceland,IFJ,BIIS,66.058056,-23.135278,8,0,N
16,Keflavik International Airport,Keflavik,Iceland,KEF,BIKF,63.985,-22.605556,171,0,N
17,Patreksfjordur,Patreksfjordur,Iceland,PFJ,BIPA,65.555833,-23.965,11,0,N
18,Reykjavik,Reykjavik,Iceland,RKV,BIRK,64.13,-21.940556,48,0,N
19,Siglufjordur,Siglufjordur,Iceland,SIJ,BISI,66.133333,-18.916667,10,0,N
20,Vestmannaeyjar,Vestmannaeyjar,Iceland,VEY,BIVM,63.424303,-20.278875,326,0,N
21,Sault Ste Marie,Sault Sainte Marie,Canada,YAM,CYAM,46.485001,-84.509445,630,-5,A
22,Winnipeg St Andrews,Winnipeg,Canada,YAV,CYAV,50.056389,-97.0325,760,-6,A
23,Shearwater,Halifax,Canada,YAW,CYAW,44.639721,-63.499444,167,-4,A
24,St Anthony,St. Anthony,Canada,YAY,CYAY,51.391944,-56.083056,108,-4,A
25,Tofino,Tofino,Canada,YAZ,CYAZ,49.082222,-125.7725,80,-8,A
26,Kugaaruk,Pelly Bay,Canada,YBB,CYBB,68.534444,-89.808056,56,-6,A
27,Baie Comeau,Baie Comeau,Canada,YBC,CYBC,49.1325,-68.204444,71,-5,A
28,Bagotville,Bagotville,Canada,YBG,CYBG,48.330555,-70.996391,522,-5,A
29,Baker Lake,Baker Lake,Canada,YBK,CYBK,64.298889,-96.077778,59,-6,A
30,Campbell River,Campbell River,Canada,YBL,CYBL,49.950832,-125.270833,346,-8,A
31,Brandon Muni,Brandon,Canada,YBR,CYBR,49.91,-99.951944,1343,-6,A
32,Cambridge Bay,Cambridge Bay,Canada,YCB,CYCB,69.108055,-105.138333,90,-7,A
33,Nanaimo,Nanaimo,Canada,YCD,CYCD,49.052333,-123.870167,93,-8,A
34,Castlegar,Castlegar,Canada,YCG,CYCG,49.296389,-117.6325,1624,-8,A
Explanation / Answer
#include <iostream>
#include <fstream>
#include <regex>
#include<string.h>
#include <vector>
using namespace std;
int main()
{
ifstream in("file.txt"); //reading a file
if(!in) {
cout << "Cannot open input file. "; // if file doesn't exist
return 1;
}
string str;
std::vector<string> ve ;//vector to store 5th word in a line
while (std::getline(in, str)) { // reading a file line by line and storing them in string
char *token = strtok(str, ","); //splitting the line on basis of comma
int i=0;
while (token != NULL)
{if(i==4&&strlen(token)==3){ //condition to take only 5th word and lenth=3 if yes store in vector
ve.push_back(token);
token = strtok(NULL, ",");
break;
}
else
{i++;
token = strtok(NULL, ","); // else move ahead
}
}
}
// now we loop back and get the next line in 'str'
}
in.close(); //close the file
cout<<ve.size()<<endl; //print sizeof vector
bool flag =false;
if(ve.is_sorted(ve.begin(),ve.end())) //check if the vector is sorted or not
{ flag=true; //if yes flag=true;
//do nothing
}
else
ve.sort(ve.begin(),ve.end())); // else sort the vector
if(flag==true) //print sorted only if vector is sorted
cout<<sorted;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.