Mathematics 1201: Programming in C Model Solutions to Homework Assignment #12 Prof. Wickerhauser Exercise 2 of Chapter 12, p.361 (a),(b),(c): int x,y,z; scanf("%d %d %d", &x,&y,&z); (d) #include main(void) { char s[2]; /* string for the names X, Y, Z */ int x,y,z, v, n; for(n=0; n<3; n++) { scanf("%*[ ]%1[XYZ]%*[ =]%d", s,&v); /* read, e.g., `Z= 23' */ switch(*s) { /* was it X, Y, or Z? */ case 'X': x = v; break; case 'Y': y = v; break; case 'Z': z = v; break; } } printf("x=%d, y=%d, z=%d\n", x, y, z); exit(0); } Exercise 4 of Chapter 12, p.361 (a) #include main(void) { int total=0; while( scanf("%*c") != EOF) ++total; printf("total=%d\n", total); exit(0); } (b) #include main(void) { int totallines=0; while( scanf("%*[^\n]") != EOF) { /* skip to end of the current line */ ++totallines; /* count this line */ scanf("%*1[\n]"); /* skip one newline */ } printf("total lines = %d\n", totallines); exit(0); } (c) #include main(void) { int chars = 0, lines=0, Ch; while( scanf("%*[^\n]%n", &Ch) != EOF) { chars += Ch; /* count these characters */ scanf("%*1[\n]"); /* skip one newline */ ++lines; /* count this line */ ++chars; /* count the newline character */ } printf("total lines = %d; total characters = %d\n", lines, chars); exit(0); } Exercise 5 of Chapter 12, p.361 "r" : a,c "w" : b,d "a" : e "r+": a,c,d "w+": b,c,d "a+": c,e Exercise 8 of Chapter 12, p.361 On my system, fclose(stdin) sets the end-of-file flag on stdin, so subsequent scanf() calls return without assigning anything.