3_Answers_8-1 - CIS 15AG Answers Chapter 8: Arrays 1 1....

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

View Full Document Right Arrow Icon
CIS 15AG Answers Chapter 8: Arrays 1. Fill in the blanks. Assume list is containing: 15, 90, 50, 60, 20, 55, 15, 25, 45, 35 and size stores the actual number of elements in the list, in this case 10. // Code a printf statement to print the first element in the list. printf( "%5d\n", list[0] ); // ...the third element in the list: 50 printf( "%5d\n", list[2] ); // ... the last element in the list: 35 printf( "%5d\n", list[9] ); // list[size-1] is more general // ... the seventh element in the list : 15 printf( "%5d\n", list[6] ); 2. Fill in the blanks. Assume list is containing: 15, 90, 50, 60, 20, 55, 15, 25, 45, 35 and size stores the actual number of elements in the list, in this case 10. // Change the second element in the list: add 10 to it, then print it. list[1] += 10; printf( "%5d\n", list[1] ); // Find the average of the first and last element in the list. avg = (list[0] + list[size-1]) / 2.0; printf( " average: %.2f\n", avg ); 3. Fill in the blanks. Assume list is containing: 15, 90, 50, 60, 20 and it is defined with a maximum size of 100: int list[100]; also size stores the actual number of elements in the list, in this case 5; // Write code to add num , a number entered by the user, at the end of the list printf( "Enter num: " ); scanf ( "%d", &num ); list[5] = num; // approach 1 list[size] = num; // approach 2 size++; if (size < 100) // approach 3 list[size++] = num; else prinf( "full list!" ); 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
Answers Chapter 8: Arrays 4. Indicate if the following are valid references to the array a, and if valid, specify the value of the element referenced (show how you arrived at the answer) Answer: int a[10] = { 1, 4, 6, 8, 9, 3, 7, 10, 2, 8 }; index: 0 1 2 3 4 5 6 7 8 9 a[ 4 ] = 9 a[ 0 ] = 1 a[a[ 0 ] + a[ 4 ]] = a[9 + 1] = a[ 10 ] NOT VALID! 10 is not a valid index; 10 is the size of the array: a valid index is any integer value between 0 and 9, including 9! a[ 2 + 6.0 / 3 ] = a[ 2 + 2.0 ] = a[ 4.0 ] NOT VALID! 4.0 is not a valid index: it must be of an integral type! 4.0 is of type double (a floating-point type!) a[ 1 + 11 % 2 ] = a[ 1 + 1 ] = a[ 2 ] = 6 5. Which statement best describes the following program segment, assuming the declarations shown below? int list[1000]; int i; for ( i = 1; i <= 500; i++ ) printf( "%3d", list[ 2*i - 1 ] ); a. The segment displays all elements with odd values. b. The segment contains a subscript error. c. The segment displays all the elements in the first half of the array. d. The segment displays all elements with an odd subscript. e. The segment displays all elements in the first half of the array with an odd subscript. 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/02/2011 for the course CIS 15BG taught by Professor Staff during the Spring '10 term at DeAnza College.

Page1 / 18

3_Answers_8-1 - CIS 15AG Answers Chapter 8: Arrays 1 1....

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

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