# include # include # include "ppc_array.h" # include "ppc_netpbm_random.h" /******************************************************************************/ char **make_random_matrix ( int m, int n, double f ) /******************************************************************************/ /* Purpose: make_random_matrix() creates an MxN matrix and fills it with random values. Licensing: This code is distributed under the MIT license. Modified: 08 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: int m, n: the number of rows and columns of the matrix. double f: the percentage of nonzero elements to be created. Output: char M[m][n]: the random matrix. */ { int i; int j; int k; char **M; make_matrix ( M, m, n ); for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { M[i][j] = 0; } } k = 0; while ( k < f * m * n ) { i = random_index ( m ); j = random_index ( n ); if ( M[i][j] == 0 ) { M[i][j] = 1; k = k + 1; } } return M; } /******************************************************************************/ int random_index ( int n ) /******************************************************************************/ /* Purpose: random_index() returns a random index in a given range. Discussion: We have 0 <= random_index() < n Licensing: This code is distributed under the MIT license. Modified: 08 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: int n: the upper limit for the index. Output: int random_index: a randomly chosen index value. */ { int value; value = rand ( ) / ( RAND_MAX / n + 1 ); return value; } /******************************************************************************/ int write_pbm ( char **M, int m, int n, char *filename ) /******************************************************************************/ /* Purpose: write_pbm() writes an MxN matrix of 0's and 1's as a PBM graphics file. Licensing: This code is distributed under the MIT license. Modified: 08 September 2023 Author: Original C version by Rouben Rostamian. This version by John Burkardt. Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: char M[m][n]: the matrix of 0's and 1's. int m, n: the number of rows and columns of the matrix. char *filename: the name of the output file. Output: int write_pbm: 1 on success, 0 on failure. */ { int i; int j; FILE *output; /* Open the file. */ output = fopen ( filename, "wt" ); if ( ! output ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "write_pbm(): Fatal error!\n" ); fprintf ( stderr, " Cannot open the output file '%s'.\n", filename ); return 0; } /* Write header. */ fprintf ( output, "%s\n", "P1" ); fprintf ( output, "# %s\n", filename ); fprintf ( output, "%d %d\n", n, m ); /* Write data. */ for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { fprintf ( output, " %d", M[i][j] ); } fprintf ( output, "\n" ); } /* Close the file. */ fclose ( output ); return 1; }