# include # include # include # include using namespace std; # include "candy_count.hpp" int main ( ); void candy_count_box_test ( ); void candy_count_matrix_test ( ); void candy_count_vector_test ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // candy_count_test() tests candy_count(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 June 2024 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "candy_count_test():\n"; cout << " C++ version\n"; cout << " Test candy_count().\n"; candy_count_vector_test ( ); candy_count_matrix_test ( ); candy_count_box_test ( ); // // Terminate. // cout << "\n"; cout << "candy_count_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void candy_count_box_test ( ) //****************************************************************************80 // // Purpose: // // candy_count_box_test() tests candy_count_box(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 June 2024 // // Author: // // John Burkardt // { int c; int *counts; int i; int l; int m; int n; cout << "\n"; cout << "candy_count_box_test():\n"; cout << " candy_count_box() counts candy types in a 3D box.\n"; cout << " There are LxMxN entries in the box A().\n"; cout << " There are C candy types.\n"; cout << " Candy types are assigned cyclically to matrix entries:\n"; cout << " A(I,J,K) = mod ( i + j + k, c )\n"; cout << " Count the number of candies of each type.\n"; // // Test 1 // c = 4; l = 7; m = 10; n = 13; cout << "\n"; cout << " Count using candy_count_box()\n"; cout << "\n"; cout << " C L M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_box ( c, l, m, n ); cout << " " << setw(2) << c << " " << setw(2) << l << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; c = 4; l = 7; m = 10; n = 13; cout << "\n"; cout << " Repeat calculation using candy_count_box_sum()\n"; cout << "\n"; cout << " C L M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_box_sum ( c, l, m, n ); cout << " " << setw(2) << c << " " << setw(2) << l << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; // // Test 2 // c = 5; l = 12; m = 13; n = 19; cout << "\n"; cout << " Count using candy_count_box()\n"; cout << "\n"; cout << " C L M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_box ( c, l, m, n ); cout << " " << setw(2) << c << " " << setw(2) << l << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; c = 5; l = 12; m = 13; n = 19; cout << "\n"; cout << " Repeat calculation using candy_count_box_sum()\n"; cout << "\n"; cout << " C L M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_box_sum ( c, l, m, n ); cout << " " << setw(2) << c << " " << setw(2) << l << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; return; } //****************************************************************************80 void candy_count_matrix_test ( ) //****************************************************************************80 // // Purpose: // // candy_count_matrix_test() tests candy_count_matrix(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 June 2024 // // Author: // // John Burkardt // { int c; int *counts; int i; int m; int n; cout << "\n"; cout << "candy_count_matrix_test():\n"; cout << " candy_count_matrix() counts candy types in a matrix.\n"; cout << " There are MxN entries in the matrix A().\n"; cout << " There are C candy types.\n"; cout << " Candy types are assigned cyclically to matrix entries:\n"; cout << " A(I,J) = mod ( i + j, c )\n"; cout << " Count the number of candies of each type.\n"; // // Test 1 // c = 4; m = 10; n = 13; cout << "\n"; cout << " Count using candy_count_matrix()\n"; cout << "\n"; cout << " C M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_matrix ( c, m, n ); cout << " " << setw(2) << c << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; c = 4; m = 10; n = 13; cout << "\n"; cout << " Repeat calculation using candy_count_matrix_sum()\n"; cout << "\n"; cout << " C M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_matrix_sum ( c, m, n ); cout << " " << setw(2) << c << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; // // Test 2 // c = 5; m = 13; n = 19; cout << "\n"; cout << " Count using candy_count_matrix()\n"; cout << "\n"; cout << " C M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_matrix ( c, m, n ); cout << " " << setw(2) << c << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; c = 5; m = 13; n = 19; cout << "\n"; cout << " Repeat calculation using candy_count_matrix_sum()\n"; cout << "\n"; cout << " C M N "; for ( i = 0; i < c; i++ ) { cout << setw(4) << i; } cout << "\n"; counts = candy_count_matrix_sum ( c, m, n ); cout << " " << setw(2) << c << " " << setw(2) << m << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; return; } //****************************************************************************80 void candy_count_vector_test ( ) //****************************************************************************80 // // Purpose: // // candy_count_vector_test() tests candy_count_vector(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 25 June 2024 // // Author: // // John Burkardt // { int c; int *counts; int i; int n; cout << "\n"; cout << "candy_count_vector_test():\n"; cout << " candy_count_vector() counts candy types in a vector.\n"; cout << " There are N entries in the vector A().\n"; cout << " There are C candy types.\n"; cout << " Candy types are assigned cyclically to vector entries:\n"; cout << " A(I) = mod ( i, c )\n"; cout << " Count the number of candies of each type.\n"; c = 4; cout << "\n"; cout << " Count using candy_count_vector()\n"; cout << " Fix value of C = " << c << "\n"; cout << " Consider a range of values of N:\n"; cout << "\n"; cout << " N #0 #1 #2 #3\n"; cout << "\n"; for ( n = 3; n <= 10; n++ ) { counts = candy_count_vector ( c, n ); cout << " " << setw(2) << n << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; } c = 4; cout << "\n"; cout << " Repeat calculation, using candy_count_vector_sum()\n"; cout << " Fix value of C = " << c << "\n"; cout << " Consider a range of values of N:\n"; cout << "\n"; cout << " N #0 #1 #2 #3\n"; cout << "\n"; for ( n = 3; n <= 10; n++ ) { counts = candy_count_vector_sum ( c, n ); cout << " " << setw(2) << " "; for ( i = 0; i < c; i++ ) { cout << setw(4) << counts[i]; } cout << "\n"; delete [] counts; } return; } //****************************************************************************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 } //****************************************************************************80 char *timestring ( ) //****************************************************************************80 // // Purpose: // // timestring() returns the current YMDHMS date as a string. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 08 July 2009 // // Author: // // John Burkardt // // Output: // // char *TIMESTRING, a string containing the current YMDHMS date. // { # define TIME_SIZE 40 const struct std::tm *tm_ptr; std::time_t now; char *s; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); s = new char[TIME_SIZE]; std::strftime ( s, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); return s; # undef TIME_SIZE }