Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

T Problem: This program will read in a large US Coast Guard database of ship position information from the Automatic Identification System (AIS)1. The student

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedT

Problem:

This program will read in a large US Coast Guard database of ship position information from the Automatic Identification System (AIS)1. The student provided functions will:

  1. Prompt the user for the filename and open the file.

  2. Read in data from the file into the vector database.

  3. Prompt the user for a name/phrase to find in the vessel names.

  4. Build a vector that contains unique vessels that contain the name/phrase.

  5. If the number of vessels found is not zero, prompt to see if the user wants to display the

    vessel records whose names contain the phrase. If yes show records.

1. Prompt the user to see if they want to calculate the distance traveled? If yes, do

calculation and display distance. 6. Go to step 3 until quit is requested.

C++

I need help writing the functions. Please take your time. Let me know if you need more pictures. I cannot type all my code bc chegg only allows limited characters. Please take your time.

#include
#include
#include
#include
#include
#include
using namespace std;
// Structure to hold item data
struct AISType
{
string MMSI; // 0
string baseDateTime; // 1
double lattitude; // 2
double longitude; // 3
double sog; // 4
double cog; // 5
double heading; // 6
string vesselName; // 7
string imo; // 8
string callSign; // 9
string vesselType; // 10
string status; // 11
double length; // 12
double width; // 13
double draft; // 14
string cargo; // 15
string transceiverClass; // 16
};
// Prototypes for functions
void readFile( ifstream & inFile, vector &item, int& count);
bool openInputFile( ifstream & inFile );
string makeStringUpper( string s);
int searchForVesselByName( vector & dataBase, string vesselName,
vector & s );
void printRecord( AISType & item );
bool getNextField(string &line, int &index, string &subString);
double stringConvert(string);
int findLastOccurrance(string mmsi, vector &d);
int findFirstOccurrance(string mmsi, vector &d);
void addUniqueString( vector &s, string value);
void saveField( int fieldNumber, string subString, AISType &tempItem );
double distanceTraveled( vector & dataBase, int first, int last );
int main()
{
// number of records read into the dataBase
int count=0;
// the dataBase
// vector type is used because it's too big for an array.
// (on my computer anyway)
vector dataBase;
vector mmsi;
// input file
ifstream inFile;
// temporary strings
string temp;
string ansYN;
int found=0;
string stars="";
int first =0, last =0;
// open the input file
if (openInputFile( inFile ) )
cout
else{
cout
return 0;
}
// read the entire file into the dataBase
readFile( inFile, dataBase, count);
cout
cin.ignore( 40, ' ');
// user interaction loop
do{
// prompt the user for the input to search for. q to quit
temp.clear();
mmsi.clear();
cout
// read the user input. getline is used so that spaces may be included
// in the input
getline(cin, temp, ' ');
// check to see if the user wants to exit the program.
// If not exiting, output the search string.
if ( temp != "q" or temp == "Q" ){
cout
}else
return 0;
// search for the number of items that contain the name/phrase
// All names in the vessel dataBase are upper case, so make the search
// string upper. MMSI is built by the function and contains the vector
// of unique vessels that contain the name searched for.
found = searchForVesselByName( dataBase, makeStringUpper(temp), mmsi );
// Let the user know if any ships were found with the name
if( found
cout
continue;
}else{
// output the results of the search
cout
cout
cout
cout
// ships were found, see if the user wants to display them
cout
cin >> ansYN;
if (ansYN =="y" or ansYN == "Y"){
// print all the first records for the ships found
for (unsigned int i=0; i
// find the vessels using MMSI and print the records
int index = findFirstOccurrance( mmsi[i], dataBase );
// verify that a valid record was found, print the record
if ( index != -1)
printRecord( dataBase[index]);
}
// Ask user if they want to calculate the distance traveled for
// the vessel.
cout
" [y] " ;
cin >> ansYN;
if ( ansYN == "y" or ansYN == "Y"){
cout
cin >> temp;
cout
// locate the index value of the first and last record
first = findFirstOccurrance( temp, dataBase);
last = findLastOccurrance( temp, dataBase);
//output the sitances and miles traveled
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
}
}
}
cin.ignore( 40, ' ');
} while ( true );
}
/* comments here
*/
double distanceTraveled( vector & dataBase, int first, int last ){
return 0.0; // Keeps the compiler happy, students should replace
}
// Example functon header comment.
/* findLastOccurrance - finds the last occurrance of an entry in the dataBase
using the MMSI as the matching criterion. The vector is search from the
last entry forward. The vector data is in time sequential order.
string mmsi - the MMSI of the desired vessel.
vector
for efficiency.
return value - the index of the last record in the dataBase for the vessel
with the matching MMSI. If the MMSI is not found, return -1.
Algorithm
Use a for loop the to seach from the last element of the vecotor toward
the first. Since the data is time ordered, oldest first, search the
vector from the bottom towards the top. This is a linear search and
returns as soon as a match is found.
*/
int findLastOccurrance(string mmsi, vector &d){
return -1; // Keeps the compiler happy, students should replace
}
/* comments here
*/
int findFirstOccurrance(string mmsi, vector &d){
return -1;// Keeps the compiler happy, students should replace
}
/* comments here
*/
int searchForVesselByName( vector & dataBase, string vesselName,
vector &s)
{
return 0; // Keeps the compiler happy, students should replace
}
/* comments here
*/
void addUniqueString( vector &s, string value){
auto it = std::find (s.begin(), s.end(), value);
if (it != s.end())
{
return;
}
else
s.push_back(value);
}
/* comments here
*/
double stringConvert(string s){
stringstream dou(s);
double res = 0.0;
dou >> res;
return res;
}
/* comments here
*/
void printRecord( AISType &item )
{
}
/* comments here
*/
bool openInputFile( ifstream & inFile )
{
return false ; // Keeps the compiler happy, students should replace
}
/* comments here
*/
void readFile( ifstream & inFile, vector &item, int& count)
{
string line;
string field;
int index;
int fieldNumber;
AISType temporaryRecord;
//read loop
while(1){
getline(inFile, line, ' ');
if(inFile.eof())
break;
}
//setting counters
index = 0;
fieldNumber = 0;
while(getNextField(line, index, field)){
//saving the field
saveField(fieldNumber, field, temporaryRecord);
//incrementing field number
++fieldNumber;
}
//adding the temporary record to item
item.push_back(temporaryRecord);
//reset counters
fieldNumber = 0;
index = 0;
//progress count
char eraseLine[] = {' ', 27, '[', '1', 'K'};
if((count % 10000) == 0) {
cout
cout.flush();
}
}
/* comments here
*/
void saveField( int fieldNumber, string subString, AISType &tempItem ){
}
/* comments here
*/
bool getNextField(string &line, int &index, string &subString)
{
//if index is more than length of line
if (index >= line.length()){
return false;
}
//clear the subString
subString.clear();
for(int i=index; i
//if, or newline is encountered
//ubdate index
//and break the loop
if(line[i] == ',' or line[i] == ' '){
index = i+1;
break;
}
//fill the subString with this character
else{
subString.push_back(line[i]);
}
}
return true;
}
/* comments here
*/
string makeStringUpper(string s)
{
string res = s;
transform(res.begin(), res.end(), res.begin(), ::toupper);
return res;
}
8 // Include libraries 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 16 using namespace std; 17 18 // Structure to hold item data 19 struct AISType 20 21 string MMSI; 22 string baseDateTime; // 1 23 double lattitude; // 2 24 double longitude; // 3 25 double sogi 26 double cogi // 5 27 double heading; 28 string vessel Name; // 7 29 string imo; 30 string callsign; // 9 31 string vesseltype; // 10 32 string status; // 11 33 double length; // 12 34 double width; // 13 35 double draft; // 14 36 string cargo; // 15 37 string transceiverclass; // 16 38 }; 39 40 // Prototypes for functions 41 void readFile( ifstream & infile, vector &item, int& count); 42 bool openInputFile( ifstream & inFile ); 43 string makeStringUpper( string s); 44 int searchForvesselByName( vector & database, string vesselName, 45 vector & s); 46 void printRecord ( AISType & item ); 47 bool getNextField(string &line, int &index, string &substring); 48 double stringConvert(string); 49 int findlastoccurrance(string mmsi, vector &d); 50 int findFirstOccurrance(string mmsi, vector &d); void addUniqueString( vector &s, string value); 52 void saveField( int fieldNumber, string substring, AISType &tempItem ); 53 double distanceTraveled( vector & database, int first, int last ); 54 55 int main() 56 { 57 // number of records read into the database 58 int count=0; 59 60 // the database 61 // vector type is used because it's too big for an array. 62 // con my computer anyway) 63 vector dataBase; vector mmsi; 65 66 // input file ifstream inFile; 68 69 // temporary strings 70 string tempi 71 string ansYN; 72 73 int found=0; 74 string stars=""; 75 int first =, last =; 76 77 // open the input file 78 if (openInputFile( infile ) ) 79 cout > ansYN; 135 136 if (ansYN =="y" or ansYN == "Y"){ 137 138 // print all the first records for the ships found 139 for (unsigned int i=0; i> ansYN; 155 156 if ( ans YN == "y" or ansYN == "Y") { 157 cout > temp; 159 cout & database, int first, int last ) { , ) 189 190 191 return @.@; // Keeps the compiler happy, students should replace 192 193) 194 195 // Example functon header comment. 196 /* findlastoccurrance - finds the last occurrance of an entry in the database 197 using the MMSI as the matching criterion. The vector is search from the 198 last entry forward. The vector data is in time sequential order. 199 string mmsi - the MMSI of the desired vessel. 200 vector &d) { 214 return -1; // keeps the compiler happy, students should replace 215) 216 217 /* comments here 218 */ 219 int findFirstOccurrance(string mmsi, vector &d) { 220 return -1;// keeps the compiler happy, students should replace 221 } 222 223 /* comments here 224 */ 225 int searchForvesselByName( vector & database, string vesselName, 226 vector &s) 227 { 228 229 return a; // keeps the compiler happy, students should replace 230 231 ) 232 233 /* comments here 234 */ 235 void addUniqueString( vector &s, string value) { 236 auto it = std::find (s.begin(), s.end(), value); 237 if (it != s.end() 238 { 239 return; 240 } 241 else 242 s.push_back(value); 243 244 ) 245 246 /* comments here 247 */ 248 double stringConvert(string s){ 249 stringstream dou(s); 250 251 double res = 0.0; 252 dou >> res; 253 254 return res; 255 } 256 257 258 /* comments here 259 */ 260 void printRecord ( AISType &item ) 261 { 262 263} 264 265 /* comments here 266 */ 267 bool openInputFile( ifstream & infile ) 268 { 269 270 return false; // keeps the compiler happy, students should replace 271 } 272 273 /* comments here 274 275 */ 276 void readFile( ifstream & infile, vector &item, int& count) 277 { 278 string line; 279 string field; 280 int index; 281 int fieldNumber; 282 AISType temporary Record; 283 284 //read loop 285 while(1) { 286 getline(infile, line, ' '); 287 288 if(infile.eof()) 289 break; File Data Format The file is comma separated, strings in the order listed below. Name Description Example Units Resolution Type Size 1 MMSI Maritime Mobile Service Identity value 477220100 Text 8 2 BaseDateTime Full UTC date and time 2017-02-01T20:05:07 YYYY-MM-DD HH-MM-SS DateTime - 3 LAT Latitude 42.35137 decimal degrees XXXXXXX Double 8 | 4 LON Longitude -71,04182 decimal degrees XXX.XXXXX Double 8 5 SOG Speed Over Ground 5.9 knots XXXX Float 6 COG Course Over Ground 47.5 degrees XXXX Float 7 Heading True heading angle 45.1 degrees XXX.X Float 8 VesselName Name as shown on the station radio license OOCL Malaysia Text 32 9 IMO International Maritime Organization Vessel number IM09627980 - Text 16 10 CallSign Call sign as assigned by FCC VRMET Text 8 11 VesselType Vessel type as defined in NAIS specifications 70 Integer 2 12 Status Navigation status as defined by the COLREGS Active Text 64 13 Length Length of vessel (see NAIS specifications) 71,0 meters XXXX Float 4 14 Width Width of vessel (see NAIS specifications) 120 meters XXXX Float 15 Draft Draft depth of vessel (see NAIS specifications) 3.5 meters XXX.X Float 16 Cargo Cargo type (see NAIS specification and codes) 70 Text 4 17 TransceiverClass Class of AIS transceiver AIS Class A Text 16 Table 1. Products File Format Example Line from Input File 367430850,2019-01-01T00:00:05,30.40366,-88.85801,0.0,126.3,511.0,MISS NIZ,IMO8987539,WDF2697,31,0,22,8,2.4,52,B Prototypes for Student Supplied Functions void readFile( ifstream & inFile, vector &item, int& count); bool open InputFile( ifstream & inFile ); string makeStringUpper( string s); int searchFor VesselByName( vector & dataBase, string vesselName, vector &s); void printRecord( AISType & item); bool getNextField(string &line, int &index, string &subString); double string Convert(string); int findLastOccurrance(string mmsi, vector &d); int findFirstOccurrance(string mmsi, vector &d); void addUniqueString( vector &s, string value); void saveField( int fieldNumber, string subString, AISType &tempItem); double distance Traveled( vector & database, int first, int last); Detailed Function Descriptions void readFile( ifstream & inFile, vector &item, int& count); inFile - already open file to be read item - vector to store data count - the number of records read This function uses the previously opened filestream to read in the data from the file. You must use getline() to read the data from the file with a ' ' as the terminating character. Once the line of data has been read, use getNextField() to parse the line, one field at a time. The initial call after the getline() should have the index set to zero, so it starts at the beginning of the string. getNextField() will update the index, so it won't need to be reset to zero until after the next call to getline(). The data should be read until end of file is reached. See example output. The function should start with a locally declared and initialized AISType temporary record. As the line is read/parsed and the fields are parsed, set the corresponding value in the temporary record. Once parsing the line is complete, and the temporary record is filled, use push_back() to add the temporary record to the AISType vector. Reset all values in the temporary record to default before reusing the temporary record on the next line. The function must keep a counter of the number of records read. The counter is used for outputting the number or records read. readFile() uses the following C++ functions getline() - C++ library getNextField() - student provided saveField() - student provided Algorithm for readFile() 1. use getline() to read the line into a string 2. check for eofo 3. set index to 0, fieldNumber to 0 4. call getNextField() on the string read in 1, passing index, and a string to receive the field 5. use saveField() to store the data in a temporary struct of type AISType (use fieldNumber to determine which field gets the data) 6. increment fieldNumber 7. loop back to 4 until the entire string has been processed. 8. add the temporary struct holding the data to the vector using .push_back() 9. reset fieldNumber and index to 0 10. loop back to 1 till eof() To get the progress count, place the following code at the bottom of the read loop. // Output the number processed. If the number update stops, then // their may be problem with the program. char eraseLine[]={" ',27,'1','1','K'}; if( (count % 100000) == 0){ cout & dataBase, string vesselName, vector & matchesAIS ); vector & dataBase - The database of AIS records read from the file string vesselName - the string to be searched for in the vessel names. vector & matches AIS all vessel whose names contains the passed string, vesselName, should have their MMSI string added to the passed vector matches AIS. matchesAIS is a vector that only contains the MMSI of vessels. return value the number of records found that match the passed string, vesselName. This function performs a linear search on all records in dataBase for records whose vesselName member contains the string passed in vessel Name. The search should be done from 0 to maximum valid index. This function calls addUniqueString() to add the MMSI to the matchesAIS vector. string makeStringUpper( string s); string s - the string to be converted to upper case. return value - upper case version of passed string. double string Convert(string s) string s the string to be converted into a double return value the double converted from the string This function converts a string to it's corresponding double value. Implementation of the function must use string stream. All other implementations are not allowed and will have significant point deductions. int findLastOccurrance(string mmsi, vector &d) string mmsi - the mmsi value being searched for vector &d the vector being searched. return value the index of the last record in the vector that contains the mmsi value passed in. This function performs a linear search to locate the last record in the passed vector with the specified MMSI. For efficiency, the search should be started at the last element and work toward the first. int findFirstOccurrance(string mmsi, vector &d) string mmsi - the mmsi value being searched for vector &d the vector being searched. return value the index of the first record in the vector that contains the mmsi value passed in. This function performs a linear search to locate the first record, in the passed vector, with the specified MMSI. The search should be started at the first element and work toward the last. void addUniqueString( vector &s, string value) vector &s the vector that the string should be added to string value the string to be added This function first searches the vector to determine if the string in already present. If the string is already present, the function simply returns. If the string is not present, the it is added at the end of the vector. A string will never appear twice in the vector. void saveField( int fieldNumber, string subString, AISType &tempItem ) int fieldNumber the number of the field, starting at zero string subString - the value to be saved in the field, may require conversion to double inside the function. AISType &tempItem- the record to which the field will be added This function saves the subString in to fieldNumber in the record passed, tempItem. The subString may need to be converted to a double, depending on the fieldNumber. See the definition of struct AISType for specific field data types. This function must use string Convert() to perform the conversion from string to double. A error message should be output from the function if an unknown field number is specified. double distance Traveled( vector & dataBase, int first, int last) vector & dataBase - vector containing the AIS data records int first - index of starting location record int last index of ending location record return value - calculated distance. In the case of a bad index value, 0.0 should be returned. Use the haversine formula to calculate the great circle distance between the vessel location in records first and last, using latitude and longitude. Remember that C++ functions, sine, cosine, and arc tangent require the input values to be in radians. a=sin 2 (Ap/2)+cos Q1. cos q2.sin 2 (11/2) c=2. atan 2(Va,V(1-a)) distance=R.c w is latitude , 2 is longitude R=3958.8(mean radius of earth , miles) PI=3.14159 radians =(degrees * PI)/ 180.0 Example Output dolly@Snoopy: Assignment 1$ g++ main_solution.cpp -Wall -pedantic dolly@Snoopy: Assignment 1$ ./a.out Enter input File Name/ (q-quit): AISData.csv File opened correctly 1800000--- End of file reached ---Items read: 1899355 1899355 records read Enter vessel name: bill Searching for records with names containing "bill" 1543 vessels found with name containing "bill", Unique vessels: 2 2 vessels found. Would you like to see their first records? [y] y MMSI: 367007060 Base Date Time: 2019-01-01T00:00:00 Lattitude: 30.2898 Longitude: -91.2234 SOG: 0.2 COG: -196.3 Heading: 511 Vessel Name: BILL S imo: IMO 7030963 Call Sign: WDC3383 Vessal Type: 31 Status: 15 Length: 30 Width: 8 Draft: 3.6 Cargo: 0 Transceiver Class: B MMSI: 366751770 Base Date Time: 2019-01-01T11:27:05 Lattitude: 49.9244 Longitude: -125.12 SOG: 6.5 COG: 131.1 Heading: 132 Vessel Name: BILLIE H imo: IMO8 964719 Call Sign: WCY4992 Vessal Type: 31 Status: 0 Length: 27 Width: 8 Draft: 4.7 Cargo: 32 Transceiver Class: B Would you like to find the distance traveled for a vessel? [y] y MMSI for vessel: 366751770 Vessel: "BILLIE H" MMSI: 366751770 Trip Starting time: 2019-01-01T11:27:05 Distance traveled from (49.9244, -125.12) to (49.1369, -123.534) 89.5485 Miles Enter vessel name: maersk Searching for records with names containing "maersk" 15706 vessels found with name containing "maersk", Unique vessels: 29 29 vessels found. Would you like to see their first records? [y] n Enter vessel name: atomic Searching for records with names containing "atomic" 363 vessels found with name containing "atomic", Unique vessels: 1 1 vessels found. Would you like to see their first records? [y] y MMSI: 319071600 Base Date Time: 2019-01-01T00:04:11 Lattitude: 26.7489 Longitude: -80.0499 SOG: 0 COG: 186.5 Heading: 182 Vessel Name: ATOMIC imo: IMO1009807 Call Sign: ZGEG8 Vessal Type: 37 Status: 5 Length: 45 Width: 9 Draft: 2.8 Cargo: Transceiver Class: A Would you like to find the distance traveled for a vessel? [y] y MMSI for vessel: 319071600 Vessel: "ATOMIC" MMSI: 319071600 Trip Starting time: 2019-01-01T00:04:11 Distance traveled from (26.7489, -80.0499) to (26.7489, -80.0499) 0.00151337 Miles Enter vessel name: mldf Searching for records with names containing "mldf" Vessel "mldf" not found Enter vessel name: 9 8 // Include libraries 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 16 using namespace std; 17 18 // Structure to hold item data 19 struct AISType 20 21 string MMSI; 22 string baseDateTime; // 1 23 double lattitude; // 2 24 double longitude; // 3 25 double sogi 26 double cogi // 5 27 double heading; 28 string vessel Name; // 7 29 string imo; 30 string callsign; // 9 31 string vesseltype; // 10 32 string status; // 11 33 double length; // 12 34 double width; // 13 35 double draft; // 14 36 string cargo; // 15 37 string transceiverclass; // 16 38 }; 39 40 // Prototypes for functions 41 void readFile( ifstream & infile, vector &item, int& count); 42 bool openInputFile( ifstream & inFile ); 43 string makeStringUpper( string s); 44 int searchForvesselByName( vector & database, string vesselName, 45 vector & s); 46 void printRecord ( AISType & item ); 47 bool getNextField(string &line, int &index, string &substring); 48 double stringConvert(string); 49 int findlastoccurrance(string mmsi, vector &d); 50 int findFirstOccurrance(string mmsi, vector &d); void addUniqueString( vector &s, string value); 52 void saveField( int fieldNumber, string substring, AISType &tempItem ); 53 double distanceTraveled( vector & database, int first, int last ); 54 55 int main() 56 { 57 // number of records read into the database 58 int count=0; 59 60 // the database 61 // vector type is used because it's too big for an array. 62 // con my computer anyway) 63 vector dataBase; vector mmsi; 65 66 // input file ifstream inFile; 68 69 // temporary strings 70 string tempi 71 string ansYN; 72 73 int found=0; 74 string stars=""; 75 int first =, last =; 76 77 // open the input file 78 if (openInputFile( infile ) ) 79 cout > ansYN; 135 136 if (ansYN =="y" or ansYN == "Y"){ 137 138 // print all the first records for the ships found 139 for (unsigned int i=0; i> ansYN; 155 156 if ( ans YN == "y" or ansYN == "Y") { 157 cout > temp; 159 cout & database, int first, int last ) { , ) 189 190 191 return @.@; // Keeps the compiler happy, students should replace 192 193) 194 195 // Example functon header comment. 196 /* findlastoccurrance - finds the last occurrance of an entry in the database 197 using the MMSI as the matching criterion. The vector is search from the 198 last entry forward. The vector data is in time sequential order. 199 string mmsi - the MMSI of the desired vessel. 200 vector &d) { 214 return -1; // keeps the compiler happy, students should replace 215) 216 217 /* comments here 218 */ 219 int findFirstOccurrance(string mmsi, vector &d) { 220 return -1;// keeps the compiler happy, students should replace 221 } 222 223 /* comments here 224 */ 225 int searchForvesselByName( vector & database, string vesselName, 226 vector &s) 227 { 228 229 return a; // keeps the compiler happy, students should replace 230 231 ) 232 233 /* comments here 234 */ 235 void addUniqueString( vector &s, string value) { 236 auto it = std::find (s.begin(), s.end(), value); 237 if (it != s.end() 238 { 239 return; 240 } 241 else 242 s.push_back(value); 243 244 ) 245 246 /* comments here 247 */ 248 double stringConvert(string s){ 249 stringstream dou(s); 250 251 double res = 0.0; 252 dou >> res; 253 254 return res; 255 } 256 257 258 /* comments here 259 */ 260 void printRecord ( AISType &item ) 261 { 262 263} 264 265 /* comments here 266 */ 267 bool openInputFile( ifstream & infile ) 268 { 269 270 return false; // keeps the compiler happy, students should replace 271 } 272 273 /* comments here 274 275 */ 276 void readFile( ifstream & infile, vector &item, int& count) 277 { 278 string line; 279 string field; 280 int index; 281 int fieldNumber; 282 AISType temporary Record; 283 284 //read loop 285 while(1) { 286 getline(infile, line, ' '); 287 288 if(infile.eof()) 289 break; File Data Format The file is comma separated, strings in the order listed below. Name Description Example Units Resolution Type Size 1 MMSI Maritime Mobile Service Identity value 477220100 Text 8 2 BaseDateTime Full UTC date and time 2017-02-01T20:05:07 YYYY-MM-DD HH-MM-SS DateTime - 3 LAT Latitude 42.35137 decimal degrees XXXXXXX Double 8 | 4 LON Longitude -71,04182 decimal degrees XXX.XXXXX Double 8 5 SOG Speed Over Ground 5.9 knots XXXX Float 6 COG Course Over Ground 47.5 degrees XXXX Float 7 Heading True heading angle 45.1 degrees XXX.X Float 8 VesselName Name as shown on the station radio license OOCL Malaysia Text 32 9 IMO International Maritime Organization Vessel number IM09627980 - Text 16 10 CallSign Call sign as assigned by FCC VRMET Text 8 11 VesselType Vessel type as defined in NAIS specifications 70 Integer 2 12 Status Navigation status as defined by the COLREGS Active Text 64 13 Length Length of vessel (see NAIS specifications) 71,0 meters XXXX Float 4 14 Width Width of vessel (see NAIS specifications) 120 meters XXXX Float 15 Draft Draft depth of vessel (see NAIS specifications) 3.5 meters XXX.X Float 16 Cargo Cargo type (see NAIS specification and codes) 70 Text 4 17 TransceiverClass Class of AIS transceiver AIS Class A Text 16 Table 1. Products File Format Example Line from Input File 367430850,2019-01-01T00:00:05,30.40366,-88.85801,0.0,126.3,511.0,MISS NIZ,IMO8987539,WDF2697,31,0,22,8,2.4,52,B Prototypes for Student Supplied Functions void readFile( ifstream & inFile, vector &item, int& count); bool open InputFile( ifstream & inFile ); string makeStringUpper( string s); int searchFor VesselByName( vector & dataBase, string vesselName, vector &s); void printRecord( AISType & item); bool getNextField(string &line, int &index, string &subString); double string Convert(string); int findLastOccurrance(string mmsi, vector &d); int findFirstOccurrance(string mmsi, vector &d); void addUniqueString( vector &s, string value); void saveField( int fieldNumber, string subString, AISType &tempItem); double distance Traveled( vector & database, int first, int last); Detailed Function Descriptions void readFile( ifstream & inFile, vector &item, int& count); inFile - already open file to be read item - vector to store data count - the number of records read This function uses the previously opened filestream to read in the data from the file. You must use getline() to read the data from the file with a ' ' as the terminating character. Once the line of data has been read, use getNextField() to parse the line, one field at a time. The initial call after the getline() should have the index set to zero, so it starts at the beginning of the string. getNextField() will update the index, so it won't need to be reset to zero until after the next call to getline(). The data should be read until end of file is reached. See example output. The function should start with a locally declared and initialized AISType temporary record. As the line is read/parsed and the fields are parsed, set the corresponding value in the temporary record. Once parsing the line is complete, and the temporary record is filled, use push_back() to add the temporary record to the AISType vector. Reset all values in the temporary record to default before reusing the temporary record on the next line. The function must keep a counter of the number of records read. The counter is used for outputting the number or records read. readFile() uses the following C++ functions getline() - C++ library getNextField() - student provided saveField() - student provided Algorithm for readFile() 1. use getline() to read the line into a string 2. check for eofo 3. set index to 0, fieldNumber to 0 4. call getNextField() on the string read in 1, passing index, and a string to receive the field 5. use saveField() to store the data in a temporary struct of type AISType (use fieldNumber to determine which field gets the data) 6. increment fieldNumber 7. loop back to 4 until the entire string has been processed. 8. add the temporary struct holding the data to the vector using .push_back() 9. reset fieldNumber and index to 0 10. loop back to 1 till eof() To get the progress count, place the following code at the bottom of the read loop. // Output the number processed. If the number update stops, then // their may be problem with the program. char eraseLine[]={" ',27,'1','1','K'}; if( (count % 100000) == 0){ cout & dataBase, string vesselName, vector & matchesAIS ); vector & dataBase - The database of AIS records read from the file string vesselName - the string to be searched for in the vessel names. vector & matches AIS all vessel whose names contains the passed string, vesselName, should have their MMSI string added to the passed vector matches AIS. matchesAIS is a vector that only contains the MMSI of vessels. return value the number of records found that match the passed string, vesselName. This function performs a linear search on all records in dataBase for records whose vesselName member contains the string passed in vessel Name. The search should be done from 0 to maximum valid index. This function calls addUniqueString() to add the MMSI to the matchesAIS vector. string makeStringUpper( string s); string s - the string to be converted to upper case. return value - upper case version of passed string. double string Convert(string s) string s the string to be converted into a double return value the double converted from the string This function converts a string to it's corresponding double value. Implementation of the function must use string stream. All other implementations are not allowed and will have significant point deductions. int findLastOccurrance(string mmsi, vector &d) string mmsi - the mmsi value being searched for vector &d the vector being searched. return value the index of the last record in the vector that contains the mmsi value passed in. This function performs a linear search to locate the last record in the passed vector with the specified MMSI. For efficiency, the search should be started at the last element and work toward the first. int findFirstOccurrance(string mmsi, vector &d) string mmsi - the mmsi value being searched for vector &d the vector being searched. return value the index of the first record in the vector that contains the mmsi value passed in. This function performs a linear search to locate the first record, in the passed vector, with the specified MMSI. The search should be started at the first element and work toward the last. void addUniqueString( vector &s, string value) vector &s the vector that the string should be added to string value the string to be added This function first searches the vector to determine if the string in already present. If the string is already present, the function simply returns. If the string is not present, the it is added at the end of the vector. A string will never appear twice in the vector. void saveField( int fieldNumber, string subString, AISType &tempItem ) int fieldNumber the number of the field, starting at zero string subString - the value to be saved in the field, may require conversion to double inside the function. AISType &tempItem- the record to which the field will be added This function saves the subString in to fieldNumber in the record passed, tempItem. The subString may need to be converted to a double, depending on the fieldNumber. See the definition of struct AISType for specific field data types. This function must use string Convert() to perform the conversion from string to double. A error message should be output from the function if an unknown field number is specified. double distance Traveled( vector & dataBase, int first, int last) vector & dataBase - vector containing the AIS data records int first - index of starting location record int last index of ending location record return value - calculated distance. In the case of a bad index value, 0.0 should be returned. Use the haversine formula to calculate the great circle distance between the vessel location in records first and last, using latitude and longitude. Remember that C++ functions, sine, cosine, and arc tangent require the input values to be in radians. a=sin 2 (Ap/2)+cos Q1. cos q2.sin 2 (11/2) c=2. atan 2(Va,V(1-a)) distance=R.c w is latitude , 2 is longitude R=3958.8(mean radius of earth , miles) PI=3.14159 radians =(degrees * PI)/ 180.0 Example Output dolly@Snoopy: Assignment 1$ g++ main_solution.cpp -Wall -pedantic dolly@Snoopy: Assignment 1$ ./a.out Enter input File Name/ (q-quit): AISData.csv File opened correctly 1800000--- End of file reached ---Items read: 1899355 1899355 records read Enter vessel name: bill Searching for records with names containing "bill" 1543 vessels found with name containing "bill", Unique vessels: 2 2 vessels found. Would you like to see their first records? [y] y MMSI: 367007060 Base Date Time: 2019-01-01T00:00:00 Lattitude: 30.2898 Longitude: -91.2234 SOG: 0.2 COG: -196.3 Heading: 511 Vessel Name: BILL S imo: IMO 7030963 Call Sign: WDC3383 Vessal Type: 31 Status: 15 Length: 30 Width: 8 Draft: 3.6 Cargo: 0 Transceiver Class: B MMSI: 366751770 Base Date Time: 2019-01-01T11:27:05 Lattitude: 49.9244 Longitude: -125.12 SOG: 6.5 COG: 131.1 Heading: 132 Vessel Name: BILLIE H imo: IMO8 964719 Call Sign: WCY4992 Vessal Type: 31 Status: 0 Length: 27 Width: 8 Draft: 4.7 Cargo: 32 Transceiver Class: B Would you like to find the distance traveled for a vessel? [y] y MMSI for vessel: 366751770 Vessel: "BILLIE H" MMSI: 366751770 Trip Starting time: 2019-01-01T11:27:05 Distance traveled from (49.9244, -125.12) to (49.1369, -123.534) 89.5485 Miles Enter vessel name: maersk Searching for records with names containing "maersk" 15706 vessels found with name containing "maersk", Unique vessels: 29 29 vessels found. Would you like to see their first records? [y] n Enter vessel name: atomic Searching for records with names containing "atomic" 363 vessels found with name containing "atomic", Unique vessels: 1 1 vessels found. Would you like to see their first records? [y] y MMSI: 319071600 Base Date Time: 2019-01-01T00:04:11 Lattitude: 26.7489 Longitude: -80.0499 SOG: 0 COG: 186.5 Heading: 182 Vessel Name: ATOMIC imo: IMO1009807 Call Sign: ZGEG8 Vessal Type: 37 Status: 5 Length: 45 Width: 9 Draft: 2.8 Cargo: Transceiver Class: A Would you like to find the distance traveled for a vessel? [y] y MMSI for vessel: 319071600 Vessel: "ATOMIC" MMSI: 319071600 Trip Starting time: 2019-01-01T00:04:11 Distance traveled from (26.7489, -80.0499) to (26.7489, -80.0499) 0.00151337 Miles Enter vessel name: mldf Searching for records with names containing "mldf" Vessel "mldf" not found Enter vessel name: 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions