C Primer Plus Sixth Edition Programming Exercise
Selected Answers
srand((unsigned int) time(0)); /* randomize rand() */
printf(\while (scanf(\ {
printf(\if (scanf(\ {
puts(\break; }
printf(\for (set = 0; set < sets; set++) {
for (roll = 0, count = 0; count < dice; count++) roll += rollem(sides); /* running total of dice pips */ printf(\if (set % 15 == 14) putchar('\\n'); } if
(set % 15 != 0) putchar('\\n');
printf(\ } puts(\YOU!\\n\}
int rollem(int sides) { int roll;
roll = rand() % sides + 1; return roll; }
Chapter 13
PE 13---2
Programming Exercises
/* Programming Exercise 13-2 */ #include
int main(int argc, char *argv[]) { int byte; FILE * source;
FILE * target;
if (argc != 3) {
printf(\exit(EXIT_FAILURE);
} if ((source = fopen(argv[1], \ {
printf(\exit(EXIT_FAILURE); }
if ((target = fopen(argv[2], \ {
printf(\exit(EXIT_FAILURE);
31
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
}
while ((byte = getc(source)) != EOF) {
putc(byte, target); }
if (fclose(source) != 0)
printf(\
if (fclose(target) != 0)
printf(\ return 0; }
PE 13---4
/* Programming Exercise 13-4 */ #include
int main(int argc, char *argv[]) {
int byte; FILE * source; int filect;
if (argc == 1) {
printf(\exit(EXIT_FAILURE); }
for (filect = 1; filect < argc; filect++) {
if ((source = fopen(argv[filect], \ {
printf(\continue; }
while ((byte = getc(source)) != EOF) {
putchar(byte); }
if (fclose(source) != 0)
printf(\ } return 0; }
PE 13---5
/* Programming Exercise 13-5 */
#include
#define BUFSIZE 4096 #define SLEN 81
void append(FILE *source, FILE *dest);
32
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
int main(int argc, char *argv[]) {
FILE *fa, *fs; int files = 0; int fct;
if (argc < 3)
{ printf(\argv[0]); exit(EXIT_FAILURE); } if ((fa = fopen(argv[1], \== NULL) {
fprintf(stderr, \exit(EXIT_FAILURE); }
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0) { fputs(\stderr); exit(EXIT_FAILURE); }
for (fct = 2; fct < argc; fct++) {
if (strcmp(argv[fct], argv[1]) == 0) fputs(\
else if ((fs = fopen(argv[fct], \fprintf(stderr, \else {
if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0) {
fputs(\continue; } append(fs, fa); if (ferror(fs) != 0)
fprintf(stderr,\argv[fct]); if (ferror(fa) != 0)
fprintf(stderr,\argv[1]); fclose(fs); files++; printf(\ } }
printf(\fclose(fa);
return 0; }
void append(FILE *source, FILE *dest) { size_t bytes;
static char temp[BUFSIZE]; // allocate once
while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0) fwrite(temp, sizeof (char), bytes, dest); }
PE 13---7
/* Programming Exercise 13-7a */
/* code assumes that end-of-line immediately precedes end-of-file */
#include
33
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
#include
int main(int argc, char *argv[]) { int ch1, ch2; FILE * f1;
FILE * f2;
if (argc != 3) {
printf(\exit(EXIT_FAILURE); }
if ((f1 = fopen(argv[1], \
{ printf(\argv[1]); exit(EXIT_FAILURE); }
if ((f2 = fopen(argv[2], \ {
printf(\exit(EXIT_FAILURE); } ch1 =
getc(f1); ch2 = getc(f2);
while (ch1 != EOF || ch2 != EOF) {
while (ch1 != EOF && ch1 != '\\n') /* skipped after EOF reached */
{ putchar(ch1); ch1 = getc(f1); }
if (ch1 != EOF) {
putchar('\\n'); ch1 = getc(f1); }
while (ch2 != EOF && ch2 != '\\n') /* skipped after EOF reached */
{ putchar(ch2); ch2 = getc(f2); }
if (ch2 != EOF) {
putchar('\\n'); ch2 = getc(f2); }
} if (fclose(f1) != 0)
printf(\if (fclose(f2) != 0)
printf(\ return 0; }
/* Programming Exercise 13-7b */
/* code assumes that end-of-line immediately precedes end-of-file */
#include
34
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
#include
int main(int argc, char *argv[]) { int ch1, ch2; FILE * f1;
FILE * f2; if (argc != 3) {
printf(\exit(EXIT_FAILURE); }
if ((f1 = fopen(argv[1], \
{ printf(\argv[1]); exit(EXIT_FAILURE); }
if ((f2 = fopen(argv[2], \ {
printf(\exit(EXIT_FAILURE); } ch1 =
getc(f1); ch2 = getc(f2);
while (ch1 != EOF || ch2 != EOF) {
while (ch1 != EOF && ch1 != '\\n') /* skipped after EOF reached */
{ putchar(ch1); ch1 = getc(f1); }
if (ch1 != EOF) {
if (ch2 == EOF) putchar('\\n');
else putchar(' '); ch1 = getc(f1); }
while (ch2 != EOF && ch2 != '\\n') /* skipped after EOF reached */
{ putchar(ch2); ch2 = getc(f2); }
if (ch2 != EOF) {
putchar('\\n'); ch2 = getc(f2); }
} if (fclose(f1) != 0)
printf(\if (fclose(f2) != 0)
printf(\ return 0; }
35