Mathematics 1201: Programming in C Reading and Homework Assignment #2 Prof. Wickerhauser Due 8 September 1999 You are encouraged to collaborate on homework, and to work additional exercises from the indicated problem sections, although the homework grade will be based only on the exercises listed below. Please return your solutions to me by the end of class. LATE HOMEWORK WILL NOT BE ACCEPTED. Read Chapter 2 of the textbook. Do Exercises 4, 7, 12, and 17 of Chapter 2, pp. 45--46. [Exercise 4, p.45] Write a program that prints out the following numbers in Decimal, octal, and hexadecimal: 37, 037, 0x37. [Exercise 7, p.45] Let B have the value of 5 and C have the value of 8. What is the value of A, B, and C after each line of the following program fragment? A = B++ + C++; A = B++ + ++C; A = ++B + C++; A = ++B + ++C; [Exercise 12, p.45] What are the values assigned on lines 1 through 6? int A, B = 5, C = 7; double X, Y = 2.8, Z = 16.8; /* 1 */ A = Y; /* 2 */ A = -Y; /* 3 */ X = A / B + C; /* 4 */ X = A / B + (double) C; /* 5 */ X = (double) A / B + C; /* 6 */ A = Y - Z; [Exercise 17, p. 46] For 16-bit integers, suppose that A=0xC0B3 and B=0x2435. Determine the following values, assuming that A and B are both unsigned, and indicate which answers would be affected if A were signed. (a) ~A (b) A | B (c) A & B (d) A ^ B (e) A >>2 (f) A <<2 (g) A<<-2 Extra Problems: 1. Find the errors in the following (alleged) Standard C program: /* 1 */ /* This program contains several errors. /* 2 */ * Can you find them all? (Hint: compile me) */ /* 3 */ #include /* 4 */ #define Pi=3.141592653589793; /* 5 */ main( void ) /* 6 */ { /* 7 */ double x, y; /* 8 */ float z; /* 9 */ int i, /* 10 */ if ( argc > 1 ) { /* 11 */ printf('No Arguments!\n'); /* 12 */ return(1); /* 13 */ } /* 14 */ x = z = Pi; /* 15 */ y = x; /* 16 */ printf ( "%f is zero.\n", x-y ); /* 17 */ printf ( "%d is pi.\n", z ); /* 18 */ i = 1/Pi; /* 19 */ i =* Pi; /* 20 */ printf ( "%d is one.\n", i ); /* 21 */ return(0) /* 22 */ }