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:

00003D14

00003D15

00003D16

00003D17

00003D19

00003D1A

00003D1B

96

00

00

00

00003D1C

(b) See picture above.

(c) 0x96

(d) 0x0096

(e) 0x00000096

Problem 3. (a) The program uses printf(str_a); to display Hello World! on the monitor

(b) 0x080483c4

(c) mov DWORD PTR [esp+4],0x80484c4

Problem 4. (a) The address of the next instruction to be executed.

(b) 0x0804838d

(c) 0x9b

(d) 0xbffff810

(e) 0x08048475

(f) 0x59

(g) Yes

Problem 5.

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) 24 bytes

(b) 7

(c) The (garbage) value stored in memory immediately below the array.

Problem 7.

#include <stdio.h> 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