Mathematics 1201: Programming in C Solutions to Reading and Homework Assignment #3 Prof. Wickerhauser Chapter 3 Exercises: Exercise 1, p.74 Program: #include main(void){ int A, X, Y, Z; A = X = Y = Z = 1; A = ++X || ++Y && ++Z; printf("A=%d, X=%d, Y=%d, Z=%d\n", A, X, Y, Z); return 0; } Output: % a.out A=1, X=2, Y=1, Z=1 Exercise 9, p.75 Program: #include main(void){ int x,y; /* Sums: */ printf("x+y|"); /* Top line identifies table and x values */ for(x=1; x<10; x++) printf("%4d", x); /* 9 nums, 4 places/num. */ printf("\n----------------------------------------\n"); /* 40 dashes */ for(y=1; y<10; y++) { printf("%3d|", y); /* Identify y in the leftmost col. */ for(x=1; x<10; x++) printf("%4d", x+y); /* 9 sums/line, 4 places/sum */ putchar('\n'); /* Go to the next line in the table */ } putchar('\n'); /* Blank line before the next table */ /* Products: */ printf("x*y|"); /* Top line identifies table and x values */ for(x=1; x<10; x++) printf("%4d", x); /* 9 nums., 4 places/num. */ printf("\n----------------------------------------\n"); /* 40 dashes */ for(y=1; y<10; y++) { printf("%3d|", y); /* Identify y in the leftmost col. */ for(x=1; x<10; x++) printf("%4d", x*y); /* 9 prod./line, 4 places/p. */ putchar('\n'); /* Go to the next line in the table */ } return(0); } Output: % a.out x+y| 1 2 3 4 5 6 7 8 9 ---------------------------------------- 1| 2 3 4 5 6 7 8 9 10 2| 3 4 5 6 7 8 9 10 11 3| 4 5 6 7 8 9 10 11 12 4| 5 6 7 8 9 10 11 12 13 5| 6 7 8 9 10 11 12 13 14 6| 7 8 9 10 11 12 13 14 15 7| 8 9 10 11 12 13 14 15 16 8| 9 10 11 12 13 14 15 16 17 9| 10 11 12 13 14 15 16 17 18 x*y| 1 2 3 4 5 6 7 8 9 ---------------------------------------- 1| 1 2 3 4 5 6 7 8 9 2| 2 4 6 8 10 12 14 16 18 3| 3 6 9 12 15 18 21 24 27 4| 4 8 12 16 20 24 28 32 36 5| 5 10 15 20 25 30 35 40 45 6| 6 12 18 24 30 36 42 48 54 7| 7 14 21 28 35 42 49 56 63 8| 8 16 24 32 40 48 56 64 72 9| 9 18 27 36 45 54 63 72 81 Exercise 12, p.75 Program: #include main(void){ int a,b; for(b=1; b<1000; b++) for(a=1; a main(void){ unsigned int N; int i, n; for( n=0; n<16; n++ ) { N = n; for( i=0 ; N &= N-1 ; i++ ) ; printf("initial N=%u ==> i=%d.\n", n, i); } return(0); } Output: % a.out initial N=0 ==> i=0. initial N=1 ==> i=0. initial N=2 ==> i=0. initial N=3 ==> i=1. initial N=4 ==> i=0. initial N=5 ==> i=1. initial N=6 ==> i=1. initial N=7 ==> i=2. initial N=8 ==> i=0. initial N=9 ==> i=1. initial N=10 ==> i=1. initial N=11 ==> i=2. initial N=12 ==> i=1. initial N=13 ==> i=2. initial N=14 ==> i=2. initial N=15 ==> i=3. The value `i' winds up with is one less than the number of 1-bits in the base-two representation of `N'. This may be proved by observing that the bitwise conjunction N & N-1 yields N-(1< #define ORDER(x,y,z,S) printf("Increasing order %lg,%lg,%lg (%s).\n",x,y,z,S) main(void){ double a,b,c; printf("Enter A: "); scanf("%lf", &a); printf("Enter B: "); scanf("%lf", &b); printf("Enter C: "); scanf("%lf", &c); if(a