diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-27 16:49:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-27 16:49:22 +0100 |
| commit | 9e3e295b48ecd4cd97c2e17f2fa98d5fee18c8da (patch) | |
| tree | 2cf6a6ef958a72006495ff60e23b5d3f8a20b2f1 /challenge-209/duncan-c-white/C/printarray.c | |
| parent | f00547cd9eb3f1cd8cd132e2a410b5bd5d2c5b7b (diff) | |
| parent | 8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff) | |
| download | perlweeklychallenge-club-9e3e295b48ecd4cd97c2e17f2fa98d5fee18c8da.tar.gz perlweeklychallenge-club-9e3e295b48ecd4cd97c2e17f2fa98d5fee18c8da.tar.bz2 perlweeklychallenge-club-9e3e295b48ecd4cd97c2e17f2fa98d5fee18c8da.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-209/duncan-c-white/C/printarray.c')
| -rw-r--r-- | challenge-209/duncan-c-white/C/printarray.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-209/duncan-c-white/C/printarray.c b/challenge-209/duncan-c-white/C/printarray.c new file mode 100644 index 0000000000..ddee597df3 --- /dev/null +++ b/challenge-209/duncan-c-white/C/printarray.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <string.h> + + +// print_int_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a <sep> separated +// list onto outfile with lines <= maxw chars long. +// produces a whole number of lines of output - without the trailing '\n' +void print_int_array( int maxw, int nel, int *results, char sep, FILE *out ) +{ + int linelen = 0; + for( int i=0; i<nel; i++ ) + { + char buf[100]; + sprintf( buf, "%d", results[i] ); + int len = strlen(buf); + if( linelen + len + 2 > maxw ) + { + fputc( '\n', out ); + linelen = 0; + } else if( i>0 ) + { + fputc( ' ', out ); + linelen++; + } + + linelen += len; + fprintf( out, "%s", buf ); + if( i<nel-1 ) + { + fputc( sep, out ); + linelen++; + } + } + //if( linelen>0 ) + //{ + // fputc( '\n', out ); + //} +} |
