Mathematics 1201: Programming in C Solutions to Homework Assignment #6 Prof. Wickerhauser Exercise 1, Chapter 6, p. 138. The function and a program to test it are shown below: #include void Swap( int *p1, int *p2) { int tmp; tmp = *p1; *p1 = *p2; *p2 = tmp; return; } void Sort3( int *A, int *B, int *C ) { if(*A>*B) Swap( A, B ); /* now A<=B */ if(*A>*C) Swap( A, C ); /* now A<=C and A<=B */ if(*B>*C) Swap( B, C ); /* now B<=C */ return; } main(void) { int a, b, c; a=1; b=2; c=3; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); a=2; b=1; c=3; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); a=2; b=3; c=1; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); a=3; b=2; c=1; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); a=3; b=1; c=2; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); a=1; b=3; c=2; Sort3(&a, &b, &c); printf("a,b,c=%d,%d,%d\n",a,b,c); return 0; } Exercise 8, Chapter 6, p. 139. (a) The address of A, which is machine-dependent (b) The value dereferenced by Ptr, namely the value of A, which is 5 (c) False, namely 0 (d) True, namely nonzero (e) The address of Ptr, which is machine-dependent (f) The content of memory location 5, which is machine-dependent and may be invalid (g) The value of A, namely 5 (h) The value dereferenced by Ptr, namely the value of A, which is 5 Extra Problem 1: main(void) { const int * x; int const * y; int * const z; x = 0; y = 0; z = 0; <----- first warning: trying to assign a constant *x = 1; <----- second warning: trying to assign a constant *y = 2; <----- third warning: trying to assign a constant *z = 3; return 0; } If the program compiles, then at run time it will crash at the line *x = 1; since x is 0 and *0 is guaranteed to be an invalid dereference. Extra Problem 2: (a) % a.out xp1 = 67107140, xp2 = 67107144, xp2-xp1 = 1 (b) 4 memory locations, the difference between xp1 and xp2, are used to store a float. The value of xp2-xp1 is the number of floats that can be stored in the space between xp1 and xp2.