# include # include # include # include using namespace std; # include "closest_pair_brute.hpp" int main ( ); double closest_pair_brute_test01 ( int n ); double cpu_time ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // closest_pair_brute_test() tests closest_pair_brute(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 03 April 2024 // // Author: // // John Burkardt // { int i; int nvec[8] = { 100, 200, 400, 800, 1600, 3200, 6400, 12800 }; double svec[8]; timestamp ( ); cout << "\n"; cout << "closest_pair_brute_test()\n"; cout << " C++ version\n"; cout << " Test closest_pair_brute().\n"; // // Solve a sequence of problems of increasing size N. // for ( i = 0; i < 8; i++ ) { svec[i] = closest_pair_brute_test01 ( nvec[i] ); } // // Print the final table. // cout << "\n"; cout << " Test Points Time\n"; cout << "\n"; for ( i = 0; i < 8; i++ ) { cout << " " << setw(4) << i << " " << setw(6) << nvec[i] << " " << setw(14) << svec[i] << "\n"; } /* Terminate. */ cout << "\n"; cout << "closest_pair_brute_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 double closest_pair_brute_test01 ( int n ) //****************************************************************************80 /* Purpose: closest_pair_brute_test01() tests the closest_pair_brute program. Licensing: This code is distributed under the MIT license. Modified: 03 April 2024 Author: John Burkardt Input: int n: the number of points. Output: double s: execution time in seconds. */ { double d; int i; int j; double s; long int seed; double *xy; cout << "\n"; cout << "closest_pair_brute_test01():\n"; cout << " closest_pair_brute() on a set of N points in 2D.\n"; xy = new double[n*2]; seed = 123456789L; srand48 ( seed ); for ( j = 0; j <= 1; j++ ) { for ( i = 0; i <= n; i++ ) { xy[i+j*n] = drand48 ( ); } } s = cpu_time ( ); closest_pair_brute ( n, xy, d, i, j ); s = cpu_time ( ) - s; cout << "\n"; cout << " n = " << n << "\n"; cout << " time in seconds = " << s << "\n"; cout << " xy(" << i << ") = (" << xy[i+0*n] << "," << xy[i+1*n] << ")\n"; cout << " xy(" << j << ") = (" << xy[j+0*n] << "," << xy[j+1*n] << ")\n"; cout << " distance = " << d << "\n"; delete [] xy; return s; } //****************************************************************************80 double cpu_time ( ) //****************************************************************************80 // // Purpose: // // cpu_time() returns the current reading on the CPU clock. // // Discussion: // // The CPU time measurements available through this routine are often // not very accurate. In some cases, the accuracy is no better than // a hundredth of a second. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 June 2005 // // Author: // // John Burkardt // // Output: // // double cpu_time(): the current reading of the CPU clock, in seconds. // { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; } //****************************************************************************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 }