BG_Test_2_Answers

BG_Test_2_Answers - CIS 15BG 1 Test 2 Answers: Chapters...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CIS 15BG Test 2 Answers: Chapters 8-12, 15, emphasizing 11, 12, and 15. "Computers are good at following instructions, but not at reading your mind." - Donald Knuth _____________________________________________________________________________________ 1. Pointers: Draw the memory map for the following program fragment. Predict the output. char ch1 = 'O'; char ch2 = 'B'; char *q; char *r; char **p; q = &ch2; // q points to ch2 r = &ch1; // r points to ch1 p = &q; // p points to q printf( “%c %c %c\n”, *q, *r, **p ); // B O B *p = r; // q points to ch1 printf( “%c %c %c\n”, *q, *r, **p ); // O O O _____________________________________________________________________________________ 2 . Pointers and Arrays: Predict the output. // Local Declarations int list[100] = {10, 11, 30, 20, 15, 40, 25}; int length = 7; int *ptr; int num; // Statements for( ptr = list; ptr < list + length; ptr++ ) { *ptr = *ptr * 2 ; printf( "%d\n", *ptr ); } Output: 20 22 60 40 30 80 50 Every element in the array is doubled using a pointer to traverse the array. _____________________________________________________________________________________ 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CIS 15BG Test 2 Answers: Chapters 8-12, 15, emphasizing 11, 12, and 15. 3 . Strings and Pointers (A). Find the value of *x, *(x + 1), *x + 1, and *(x + 5) for the following declaration: char *x = "This is a beautiful day!" *x T *(x + 1) h *x + 1 U // 'T' + 1 next letter after 'T' is 'U' *(x + 5) i (B). What is the error in the following program fragment? How would you correct it? char *x = "This is a beautiful day!" char s[50]; s = x; s is not a left value (it represents the address of the first character in the string – it is a pointer constant)! Use strcpy: strcpy( s, x ); (C). What is the mistake in the following program fragment? How would you correct it? char *x = "This is a beautiful day!" char *y = "This is a sunny day!"; if( x < y ) // if( strcmp(x,y) < 0 ) printf( "%s", x ); else if( x > y ) // if( strcmp(x,y) > 0 ) printf( "%s", y ); else printf("The two strings are identical!\n"); The intention here is to compare the contents of the two string variables but instead their addresses are being compared! To compare the contents of two string variables we must use strcmp ! 2
Background image of page 2
CIS 15BG Test 2 Answers: Chapters 8-12, 15, emphasizing 11, 12, and 15. _____________________________________________________________________________________ 4 . Review Cohesion (729) and Coupling (A). Which one of the three good program design principles is not used in the following function definition? Each module must do only one task!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

BG_Test_2_Answers - CIS 15BG 1 Test 2 Answers: Chapters...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online