# include # include # include # include # include # include using namespace std; struct result { string city; int count; double sum; double min; double max; }; int main ( int argc, const char **argv ); int getcity ( string city, struct result results[], int nresults ); void timestamp ( ); //****************************************************************************80 int main ( int argc, const char **argv ) //****************************************************************************80 // // Purpose: // // brc_naive() is a naive solution of the one billion record challenge. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 October 2024 // // Author: // // Original C version by Danny van Kooten. // This version by John Burkardt. // { int c; string filename; int i; ifstream input; double measurement; int nrecords; int nresults; ofstream output; struct result results[413]; double run_time; clock_t tstart; clock_t tstop; timestamp ( ); cout << "\n"; cout << "brc_naive():\n"; cout << " C++ version.\n"; cout << " Carry out 1 Billion Row Challenge (brc).\n"; tstart = clock ( ); if ( 1 < argc ) { filename = argv[1]; } else { filename = "measurements.txt"; } // // Open the data file. // input.open ( filename.c_str ( ) ); if ( ! input ) { cout << "\n"; cout << "brc_naive(): Fatal error!\n"; cout << " Could not open file '" << filename << "'"; exit ( EXIT_FAILURE ); } cout << " Reading data from file '" << filename << "'\n"; // // Read and process each record. // nrecords = 0; nresults = 0; string buf; while ( getline ( input, buf ) ) { nrecords = nrecords + 1; size_t pos = buf.find ( ';' ); size_t posp1 = pos + 1; string city = buf.substr ( 0, pos ); measurement = stod ( buf.substr ( posp1 ) ); c = getcity ( city, results, nresults ); if ( c < 0 ) { results[nresults].city = city; results[nresults].sum = measurement; results[nresults].max = measurement; results[nresults].min = measurement; results[nresults].count = 1; nresults++; } else { results[c].sum += measurement; results[c].count += 1; if ( measurement < results[c].min ) { results[c].min = measurement; } if (results[c].max < measurement) { results[c].max = measurement; } } } input.close ( ); // // Report results. // tstop = clock ( ); run_time = ( double ) ( tstop - tstart ) / ( double ) CLOCKS_PER_SEC; cout << "\n"; cout << " Number of records read was " << nrecords << "\n"; cout << " Number of distinct cities encountered was " << nresults << "\n"; cout << " Run time was " << run_time << " seconds.\n"; // // This data goes to a special output file. // filename = "results.txt"; output.open ( filename ); for ( i = 0; i < nresults; i++) { output << " " << results[i].city << " " << results[i].min << " " << results[i].sum / results[i].count << " " << results[i].max << "\n"; } output.close ( ); cout << "\n"; cout << " Unsorted output written to " << filename << "\n"; // // Terminate. // cout << "\n"; cout << "brc_naive():\n"; cout << " Normal end of execution.\n"; timestamp ( ); return 0; } //****************************************************************************80 int getcity ( string city, struct result results[], int nresults ) //****************************************************************************80 // // Purpose: // // getcity() returns the index I of a given city in the list of cities. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 May 2024 // // Author: // // Original C version by Danny van Kooten. // This version by John Burkardt. // { int i; for ( i = 0; i < nresults; i++ ) { if ( results[i].city == city ) { return i; } } return -1; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }