Declaring and Manipulating Data into Arrays

Arrays

Lab 9.1 Declaring and Manipulating Data into Arrays

Question

Answer
(Circle the correct answer)

1. How many components are contained in the following array declarations?

double values [14];

a. 14

b. 15

2. Write a for loop that sums the numbers in an array with 5 components.

int sum = 0

for( int i=0; i<5; i++)

sum+=array[i];

3. An array _________________.

a. is a simple data type

b. is a structured data type

4. Simple data types are building blocks for arrays.

yes

5. An array index starts at 1.

no

6. In C++, array is a reserved word.

no

7. The following code allows the size of an array to be determined at execution:

int n;

cout << “Enter the size of the array: “;

cin >> n;

int array[n];

yes

Lab 9.2 Checking Index Bounds and Initializing Arrays

Question

Answer
(Circle the correct answer)

1. How many components are contained in the following array declarations?

double values [14];

a. 14

b. 15

  1. The following statements assign 5 to the last value in the array.

int array [10];

array [10] = 5;

No,

Array[9]=5;

  1. Given:

int array[5] = { 1, 2, 3}

What are the values of each array component?

{1,2,3,0,0}

4. The following code initializes each component to 0:

int array[3];

array[3] = {0};

No

5. The following code initializes each component to 0:

int array[3] = {0};

yes

6. The following code correctly initializes each component to 0:

int array[3];

for (int count = 0; count <= 3; count++)

array[count] = 0;

It does initializes each component too but because the array maximum index is 2

7. The following code initializes each component to 0:

int array[3];

for (int count = 1; count <= 3; count++)

array[count] = 0;

No, _IndexOutOf BoundException

Lab 9.3 Passing Arrays as Parameters to Functions

Question

Answer
(Circle the correct answer)

1. Given the prototype:

void function(int array[5]);

The formal parameter is an array of 5 elements.

T

  1. When passing an array to a function, the size of the array is automatically passed.

F

3. Given the declaration:

int array[5];

and the prototype:

void function(int []);

Which code calls a function with array as the argument?

a. function (array);

b. function (array[]);

c. function (array[5]);

4. Which prototype for a function reads in an integer array and returns the array to the calling function?

a. int function();

b. int[] function();

c. void function(int array[]);

d. void function(int &array[]);

citation generator
citaion generator
make money online