CIS170C WEEK 5 QUIZ.docx

30 July, 2024 | 5 Min Read

Grading Summary

These are the automatically computed results of your exam. Grades for essay questions, and comments from your instructor, are in the “Details” section below.

Question 1. Question :

(TCO 11) If n represents the size of an array, what would be the index of the last element in the array?

Student Answer:	 n-2

 n

CORRECT	 n-1

 Cannot be determined

Instructor Explanation: Lecture; Chapter 7, Section 7.2.2 Array Indexed Variables

Points Received:	3  of  3  	Comments:

Question 2. Question :

(TCO 11) When working with an array, the easiest way to traverse or visit all elements of the array is to _____.

Student Answer:	CORRECT	 use a loop statement

sort the array and read it do a binary search use sequential coding

Instructor Explanation: Lecture and Chapter 7

Points Received: 3 of 3

Comments:

Question 3. Question :

( TCO 11) A three-row, four-column array has a total of how many elements?

Student Answer:	 7

CORRECT	 12

 16

 1

Arrays Instructor Explanation: Lecture; Chapter 8, Section 8.2 Declaring Two-Dimensional

Points Received:	3  of  3

Comments:

Question 4. Question :

( TCO 12) Given the following array : int profit [5] = {10, 20, 31, 55, 66};

The following statement would replace which value?

profit [4] = profit[1] + 3;

Student Answer: 55 with 13

66 with 13

CORRECT 66 with 23

55 with 23

Instructor Explanation: Lecture; Chapter 7, Section 7.2.3 Array Initializers

Points Received:	3  of  3  	Comments:

Question 5. Question :

( TCO 12) An array of characters is known as a _____.

Student Answer:	 character set

CORRECT	 string

 constant

 character matrix

Instructor Explanation: Lecture; Chapter 7, Section 7.10 C-strings

Points Received:	3  of  3  	Comments:

Question 6. Question :

( TCO 11) In the following array, what is the value of table[2][1]? int table[4][3]={3,7,0,2,4,9,8,1,3,6,5,4};

Student Answer:	 4

7

6

CORRECT 1

Instructor Explanation: Lecture; Chapter 8, Section 8.2 Declaring Arrays

Points Received: 3 of 3

Comments:

Question 7. Question :

( TCO 11) What does it mean when C++ arrays are zero-index based?

Student Answer: CORRECT The first element of a C++ array is accessed using zero as an index.

 The first element of a C++ array must contain the value of zero.

 C++ arrays have pointers to the location at \[1].

 None of the above

Instructor Explanation: Lecture; Chapter 7, Section 7.2.1 Declaring Arrays

Points Received:	3  of  3  	Comments:

Question 8. Question :

(TCO 11) Is it possible to write code in this manner for a C++ program?

int break; cout << “What time is your break?"; cin » break; int numbers[break];

Student Answer: Yes, this is perfectly acceptable C++ code.

No, the array dimension cannot be a variable.

CORRECT The compiler would report that there is an illegal use of a

keyword.

 Yes, this is perfectly acceptable C++ code, but the compiler would report that there is an illegal use of a keyword.

Instructor Explanation: p. 141: Break is a keyword and cannot be used.

Points Received:	3  of  3  	Comments:

Question 9. Question :

(TCO 11) Given this function prototype void CalcTotal(float numbers[]), what is wrong with this function?

void CalcTotal(float numbers) {

 float total;      for(int I = 0; I \< 6 ++I)

      {

           total = total + numbers\[I];

      }

}

Student Answer:	 The function is correct.

 There is a data type in the function header line.

 I is capitalized.

CORRECT	 The function header line doesn't match the prototype.

Instructor Explanation: Lecture and Chapter 7

void CalcTotal(float numbers)

{ float total; for(int I = 0; I { total = total + numbers[I];

 }

}

Points Received:	3  of  3  	Comments:

Question 10. Question :

(TCO 12) Assuming that t is an array and tPtr is a pointer to that array, which expression refers to the address of element 3 of the array?

Student Answer:	INCORRECT	 \*( tPtr + 3 )

 tPtr\[ 3 ]

CORRECT	 \&t\[ 3 ]

 \*( t + 3 )

Instructor Explanation: Lecture and Chapter 8

Points Received:	0  of  3  	Comments:

Question 11. Question :

(TCO 11) Explain how to determine the number of elements in a previously defined singledimension array. Provide a C++ program segment that illustrates your answer.

Student Answer:	 	To determine the number of elements in a previously 

defined single-dimension array one may simply use a loop and an accumulator variable taken into consideration the size of the array. #include <iostream> using namespace std; int main () { int arr[4]={1,2,3,4}; int counter = 0; for ( int i = 0; i < 4 ; i ++) { cout << arr[i] << endl; counter++; } // output the number of elements in array cout << “Array has " << counter << " elements " << endl; return 0; } Here in the program the array elements are outputted using a for loop and an accumulator variable that keeps tracks of the number of times loop runs relative to size.Outside the loop the value stored in accumulator variable after running for loop is outputted to the screen.

Instructor Explanation: The number of elements is an array can be determined by using the sizeof function. The sizeof function returns the number of bytes contained in a data item. The number of elements in an array can be determined by calculating the number of bytes the entire array consumes and then dividing that number by the number of bytes an individual element of an array consumes. 

Here is a program segment that calculates the number of elements in an array:

int myArray[5] = {1,2,3,4,5}; int arraySize = (sizeof(myArray)) / (sizeof(myArray [0]));

Lecture and Chapter 7

Points Received:	1  of  5

Comments:	Use the sizeof function.

Powered by TCPDF (www.tcpdf.org)

Related posts