# include # include # include # include int main ( int argc, char *argv[] ); void node_write ( char *output_filename, int m, int n, double table[] ); void timestamp ( ); /******************************************************************************/ int main ( int argc, char *argv[] ) /******************************************************************************/ /* Purpose: random_nodes() creates random nodes and writes them in a triangle file. Licensing: This code is distributed under the GNU LGPL license. Modified: 21 September 2016 Author: John Burkardt */ { char filename[255]; int i; int j; int m; int n; int seed; bool verbose = false; double *x; if ( verbose ) { timestamp ( ); printf ( "\n" ); printf ( "random_nodes():\n" ); printf ( " C version\n" ); printf ( " Create a file of N random nodes in 2D.\n" ); } m = 2; n = atoi ( argv[1] ); if ( verbose ) { printf ( " Number of nodes N = %d\n", n ); } /* Create data. */ x = ( double * ) malloc ( m * n * sizeof ( double ) ); seed = time ( 0 ); srand ( seed ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < m; j++ ) { x[j+i*m] = ( double ) rand ( ) / ( double ) RAND_MAX; } } /* Write data to triangle node file. */ sprintf ( filename, "nodes_n%d.node", n ); if ( verbose ) { printf ( " Created '%s'\n", filename ); } node_write ( filename, m, n, x ); /* Free memory. */ free ( x ); /* Terminate. */ if ( verbose ) { printf ( "\n" ); printf ( "random_nodes():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); } return 0; } /******************************************************************************/ void node_write ( char *filename, int m, int n, double x[] ) /******************************************************************************/ /* Purpose: node_write() writes a triangle node file. Discussion: An R8MAT is an array of R8's. Licensing: This code is distributed under the GNU LGPL license. Modified: 01 June 2009 Author: John Burkardt Input: char *FILENAME, the output filename. int M, the spatial dimension. int N, the number of points. double X[M*N], the data. */ { int i; int j; FILE *output; /* Open the file. */ output = fopen ( filename, "wt" ); if ( !output ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "node_write(): Fatal error!\n" ); fprintf ( stderr, " Could not open the file '%s'.\n", filename ); exit ( 1 ); } /* TRIANGLE requires a header line. */ fprintf ( output, "%d %d 0 0\n", n, m ); /* Write the data. */ for ( i = 0; i < n; i++ ) { fprintf ( output, "%d ", i ); for ( j = 0; j < m; j++ ) { fprintf ( output, " %24.16g", x[j+i*m] ); } fprintf ( output, "\n" ); } /* Close the file. */ fclose ( output ); return; } /******************************************************************************/ 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 GNU LGPL 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 }