# include # include # include # include "candy_count.h" int main ( ); void candy_count_box_test ( ); void candy_count_matrix_test ( ); void candy_count_vector_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: candy_count_test() tests candy_count(). Licensing: This code is distributed under the MIT license. Modified: 24 June 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "candy_count_test():\n" ); printf ( " Fortran90 version\n" ); printf ( " Test candy_count().\n" ); candy_count_vector_test ( ); candy_count_matrix_test ( ); candy_count_box_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "candy_count_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void candy_count_box_test ( ) /******************************************************************************/ /* Purpose: candy_count_box_test() tests candy_count_box(). Licensing: This code is distributed under the MIT license. Modified: 24 June 2024 Author: John Burkardt */ { int c; int *counts; int i; int l; int m; int n; printf ( "\n" ); printf ( "candy_count_box_test():\n" ); printf ( " candy_count_box() counts candy types in a 3D box.\n" ); printf ( " There are LxMxN entries in the box A().\n" ); printf ( " There are C candy types.\n" ); printf ( " Candy types are assigned cyclically to matrix entries:\n" ); printf ( " A(I,J,K) = mod ( i + j + k, c )\n" ); printf ( " Count the number of candies of each type.\n" ); /* Test 1 */ c = 4; l = 7; m = 10; n = 13; printf ( "\n" ); printf ( " Count using candy_count_box()\n" ); printf ( "\n" ); printf ( " C L M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_box ( c, l, m, n ); printf ( " %2d %2d %2d %2d ", c, l, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); c = 4; l = 7; m = 10; n = 13; printf ( "\n" ); printf ( " Repeat calculation using candy_count_box_sum()\n" ); printf ( "\n" ); printf ( " C L M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_box_sum ( c, l, m, n ); printf ( " %2d %2d %2d %2d ", c, l, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); /* Test 2 */ c = 5; l = 12; m = 13; n = 19; printf ( "\n" ); printf ( " Count using candy_count_box()\n" ); printf ( "\n" ); printf ( " C L M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_box ( c, l, m, n ); printf ( " %2d %2d %2d %2d ", c, l, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); c = 5; l = 12; m = 13; n = 19; printf ( "\n" ); printf ( " Repeat calculation using candy_count_box_sum()\n" ); printf ( "\n" ); printf ( " C L M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_box_sum ( c, l, m, n ); printf ( " %2d %2d %2d %2d ", c, l, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); return; } /******************************************************************************/ void candy_count_matrix_test ( ) /******************************************************************************/ /* Purpose: candy_count_matrix_test() tests candy_count_matrix(). Licensing: This code is distributed under the MIT license. Modified: 24 June 2024 Author: John Burkardt */ { int c; int *counts; int i; int m; int n; printf ( "\n" ); printf ( "candy_count_matrix_test():\n" ); printf ( " candy_count_matrix() counts candy types in a matrix.\n" ); printf ( " There are MxN entries in the matrix A().\n" ); printf ( " There are C candy types.\n" ); printf ( " Candy types are assigned cyclically to matrix entries:\n" ); printf ( " A(I,J) = mod ( i + j, c )\n" ); printf ( " Count the number of candies of each type.\n" ); /* Test 1 */ c = 4; m = 10; n = 13; printf ( "\n" ); printf ( " Count using candy_count_matrix()\n" ); printf ( "\n" ); printf ( " C M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_matrix ( c, m, n ); printf ( " %2d %2d %2d ", c, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); c = 4; m = 10; n = 13; printf ( "\n" ); printf ( " Repeat calculation using candy_count_matrix_sum()\n" ); printf ( "\n" ); printf ( " C M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_matrix_sum ( c, m, n ); printf ( " %2d %2d %2d ", c, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); /* Test 2 */ c = 5; m = 13; n = 19; printf ( "\n" ); printf ( " Count using candy_count_matrix()\n" ); printf ( "\n" ); printf ( " C M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_matrix ( c, m, n ); printf ( " %2d %2d %2d ", c, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); c = 5; m = 13; n = 19; printf ( "\n" ); printf ( " Repeat calculation using candy_count_matrix_sum()\n" ); printf ( "\n" ); printf ( " C M N " ); for ( i = 0; i < c; i++ ) { printf ( "%4d", i ); } printf ( "\n" ); counts = candy_count_matrix_sum ( c, m, n ); printf ( " %2d %2d %2d ", c, m, n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); return; } /******************************************************************************/ void candy_count_vector_test ( ) /******************************************************************************/ /* Purpose: candy_count_vector_test() tests candy_count_vector(). Licensing: This code is distributed under the MIT license. Modified: 23 June 2024 Author: John Burkardt */ { int c; int *counts; int i; int n; printf ( "\n" ); printf ( "candy_count_vector_test():\n" ); printf ( " candy_count_vector() counts candy types in a vector.\n" ); printf ( " There are N entries in the vector A().\n" ); printf ( " There are C candy types.\n" ); printf ( " Candy types are assigned cyclically to vector entries:\n" ); printf ( " A(I) = mod ( i, c )\n" ); printf ( " Count the number of candies of each type.\n" ); c = 4; printf ( "\n" ); printf ( " Count using candy_count_vector()\n" ); printf ( " Fix value of C = %d\n", c ); printf ( " Consider a range of values of N:\n" ); printf ( "\n" ); printf ( " N #0 #1 #2 #3\n" ); printf ( "\n" ); for ( n = 3; n <= 10; n++ ) { counts = candy_count_vector ( c, n ); printf ( " %2d ", n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); } c = 4; printf ( "\n" ); printf ( " Repeat calculation, using candy_count_vector_sum()\n" ); printf ( " Fix value of C = %d\n", c ); printf ( " Consider a range of values of N:\n" ); printf ( "\n" ); printf ( " N #0 #1 #2 #3\n" ); printf ( "\n" ); for ( n = 3; n <= 10; n++ ) { counts = candy_count_vector_sum ( c, n ); printf ( " %2d ", n ); for ( i = 0; i < c; i++ ) { printf ( "%4d", counts[i] ); } printf ( "\n" ); free ( counts ); } return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }