Objective a) You will be processing a data file in order to build some statistic
ID: 3833632 • Letter: O
Question
Objective a) You will be processing a data file in order to build some statistics. The data file is 100% random, therefore, this is only a fictitious exercise. Your code should work on all data files that are organized in the same format as the provided example. b) The data files has a header row that must be ignored, see ignore(1000,' ') usage in textbook. c) Each following line is first_name, last_name, gender (Male or Female), age, (Integer), Programming Language Preference (C++,Haskell,Java,Python,or JavaScript). Each item on a line is separated by a tab. d) Your code will read in a filename provided on the command-line. The first argument will be the source datafile. e) Your code will send the results to the filename provided as the second argument. f) Some code to help you start off: #include #include //For ifstream and ofstream #include #include //Has EXIT_SUCCESS AND EXIT_FAILURE using namespace std; int main(int argc, char *argv[]) { ifstream ins; ofstream outs; if ( argc != 3 ) { cerr << "Usage " << argv[0] << " srcfile logfile" << endl; return EXIT_FAILURE; } //The following two lines will make sense after chapter 9 string dataFilename = argv[1]; string logFilename = argv[2]; ins.open(dataFilename.c_str()); if ( ins.fail() ) { cerr << "Unable to open " << logFilename << " for read." << endl; return EXIT_FAILURE; } //The rest is for you to complete g) The processing will be as follows: • Number of records • Average Age • Most popular language for all programmers
Explanation / Answer
Code with comments explaining the code:
#include <string>
#include <map>
#include <iostream>//For ifstream and ofstream
#include <stdlib> //Has EXIT_SUCCESS AND EXIT_FAILURE
using namespace std;
int main(int argc, char *argv[]) {
ifstream ins; // input file stream
ofstream outs; // output file stream
if ( argc != 3 ) { // check for 3 arguments
cerr << "Usage " << argv[0] << " srcfile logfile" << endl;
return EXIT_FAILURE;
}
//The following two lines will make sense after chapter 9
string dataFilename = argv[1];
string logFilename = argv[2];
ins.open(dataFilename.c_str()); // open the datafile
if ( ins.fail() ) { // check if there is a failure in openeing the file
cerr << "Unable to open " << logFilename << " for read." << endl;
return EXIT_FAILURE;
}
cin.ignore(10000, ' '); // ignore the header
string column;
int records = 0;
string first_name, last_name, gender, age, langPref;
float avgAge;
map<string, int> langPopularity; // Hash map to store language popularity.
// read the file column by column until end of file
while (!ins.eof()) {
ins >> first_name;
ins >> last_name;
ins >> gender;
ins >> age;
avgAge = atoi(age);
ins >> langPref;
// update the popularity of the language for each record.
if (langPopularity.find(langPref) != langPopularity.end()) {
langPopularity[langPref] = langPopularity[langPref] + 1;
} else {
langPopularity[langPref] = 1;
}
records++; // increment the record count for each record read.
}
string popularLang;
int popularity = 0;
// iterate over the map to find the popular language from the hash map.
for (map<string, int>::iterator it = mymap.begin(); it != mymap.end(); it++) {
string key = it->first;
int value = it->second;
if (value > popularity) {
popularity = value;
popularLang = key;
}
}
// Output the required statistics.
cout << "Number of records: " << records << ' ';
cout << "Average Age: " << avgAge / records << ' ';
cout << "Most popular language for all programmers" << popularLang << ' ';
// close the streams
ins.close();
outs.close();
// return succeess
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.