# include # include # include # include # include "knapsack_values.h" int main ( ); int i4vec_dot_product ( int n, int a[], int b[] ); int i4vec_sum ( int n, int a[] ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: knapsack_values_test() tests knapsack_values(). Licensing: This code is distributed under the MIT license. Modified: 19 November 2024 Author: John Burkardt */ { int i; int k; int n; int n_data; double r; int *s; int *v; int *w; timestamp ( ); printf ( "\n" ); printf ( "knapsack_values_test():\n" ); printf ( " C version]n" ); printf ( " Test knapsack_values().\n" ); n_data = 0; while ( true ) { /* First call to get the problem size N. */ n = 0; knapsack_values ( &n_data, &n, v, w, s, &k ); if ( n == 0 ) { break; } /* Allocate array space, and call again for array data. */ s = ( int * ) malloc ( n * sizeof ( int ) ); v = ( int * ) malloc ( n * sizeof ( int ) ); w = ( int * ) malloc ( n * sizeof ( int ) ); knapsack_values ( &n_data, &n, v, w, s, &k ); printf ( "\n" ); printf ( " Problem #%d\n", n_data ); printf ( " Number of items is %d\n", n ); printf ( " Knapsack weight limit is %d\n", k ); printf ( "\n" ); printf ( " Item 0/1 Value Weight Value/Weight\n" ); printf ( "\n" ); for( i = 0; i < n; i++ ) { r = ( double ) v[i] / ( double ) w[i]; printf ( " %5d %2d %8d %8d %7.2f\n", i, s[i], v[i], w[i], r ); } printf ( "\n" ); printf ( " Taken %2d %8d %8d %7.2f\n", i4vec_sum ( n, s ), i4vec_dot_product ( n, s, v ), i4vec_dot_product ( n, s, w ), ( double ) i4vec_dot_product ( n, s, v ) / ( double ) i4vec_dot_product ( n, s, w ) ); free ( s ); free ( w ); free ( v ); } /* Terminate. */ printf ( "\n" ); printf ( "knapsack_values_test():\n" ); printf ( " Normal end of execution.\n" ); timestamp ( ); return 0; } /******************************************************************************/ int i4vec_dot_product ( int n, int x[], int y[] ) /******************************************************************************/ /* Purpose: i4vec_dot_product() computes the dot product of two I4VEC's. Discussion: An I4VEC is a vector of I4's. Licensing: This code is distributed under the MIT license. Modified: 19 December 2011 Author: John Burkardt Input: int N, the size of the array. int X[N], Y[N], the arrays. Output: int I4VEC_DOT_PRODUCT, the dot product of X and Y. */ { int i; int value; value = 0; for ( i = 0; i < n; i++ ) { value = value + x[i] * y[i]; } return value; } /******************************************************************************/ int i4vec_sum ( int n, int a[] ) /******************************************************************************/ /* Purpose: i4vec_sum() sums the entries of an I4VEC. Discussion: An I4VEC is a vector of I4's. Example: Input: A = ( 1, 2, 3, 4 ) Output: I4VEC_SUM = 10 Licensing: This code is distributed under the MIT license. Modified: 29 May 2003 Author: John Burkardt Input: int N, the number of entries in the vector. int A[N], the vector to be summed. Output: int I4VEC_SUM, the sum of the entries of A. */ { int i; int sum; sum = 0; for ( i = 0; i < n; i++ ) { sum = sum + a[i]; } return sum; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* 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: 24 September 2003 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 }