Solutions to End of Chapter Problems

advertisement

Solutions to End of Chapter Problems

Problem 1. (a) char (b) string (an array of characters) Problem 2. (a) The base-10 value of 150 is equivalent to 0x96. Since integers are stored in four bytes, this value will be stored as 0x00000096. Since values are stored in little-Endian order, the picture is: (b) (c) (d) (e)

00003D14 00003D15 00003D16 00003D17 00003D19 00003D1A 00003D1B 00003D1C

See picture above. 0x96 0x0096 0x00000096

96 00 00 00

Problem 3. Problem 4. Problem 5. (a) (b) (c) (a) (b) (c) (d) (e) (f) (g) The program uses printf(str_a); to display

Hello World!

on the monitor 0x080483c4 mov DWORD PTR [esp+4],0x80484c4 The address of the next instruction to be executed. 0x0804838d 0x9b 0xbffff810 0x08048475 0x59 Yes C doesn’t check/prevent access of an element outside the range of an array. This is the bane of many a programmer's existence: There is no protection against reading from or writing beyond the bounds of an array. 5

Problem 6. (a) (b) 24 bytes 7 (c) The (garbage) value stored in memory immediately below the array. Problem 7. #include int main() { int num_students = 5; float EC310midterm[ num_students ]; int number; float sum = 0; float average; for ( number = 0 ; number <= 4 ; number = number+1 ) { printf( "Enter score for student %d : " , number + 1); scanf( "%f" , &EC310midterm[ number ] ); sum = sum + EC310midterm[number] ; } average = sum / num_students ; printf( "Class midterm average: %f \n" , average ); } Problem 8. YES, it would compile! But, problems may ensue. The compiler has reserved enough room in memory to accommodate five elements (floating point values, in our case) for the EC310midterm array. Since EC310midterm[5] refers to the 6th element in the array, you would be over-writing data outside of the intended array boundary in memory! 6

Download