Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) How do you add up all of the elements in an array with NUM_ELEMENTS elements

ID: 674692 • Letter: #

Question

(1)

How do you add up all of the elements in an array with NUM_ELEMENTS elements? (fill in the blank)

float arr[NUM_ELEMENTS];

double total=0;

...

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

________________________________;

Select one:

a. double total = total + arr[i];

b. total = total + arr[i];

c. total = total[i];

d. total = arr[i];

e. int total = total + arr[i];

f. float total = total + arr[i];

g. total = total + arr;

h. none of the other answers

(2)

Suppose you have an array of seven int values called test containing the following data:

Trace the execution of the following code:

int x = 0;

int y = 0;

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

{

    if (test[i] % 2 == 1)

{

y += test[i];

    }

    else

     {

x += test[i];

}

}

What is the value of x when this loop is done?

(3)

The following code for a function "minVal" contains a logic error on a single line in the function

body, on one of the four lines indicated by comments:

/* This function returns the value of the minimum element in the subsection of the array "y", starting * at position "first" and ending at position "last".

*/

int minVal(int y[ ], int sizeOfArray, int first, int last)

{

  int bestSoFar = y[first];   // line 1

  for (int i = first+1; i <= last; i++)

{

  if ( y[i] < y[bestSoFar] )   // line 2

bestSoFar = y[i];   // line 3

} // for

return bestSoFar;   // line 4

} // function minVal

Which one of the four lines indicated by the comments contains the logic error?

Select one:

a. line 3

b. line 4

c. line 2

d. line 1

(4)

After the following code snippet executes, what would be the value of the variable eq?

int a[ ] = { 1, 2, 3, 4 };
int b[ ] = { 1, 2, 3, 4 };

bool eq = (a == b);

Select one:

True

False

(5)

For this array, what do you put in the brackets to assign the last element in the array to value?

int numbers[100];

value = numbers[ ];

(6)

Trace the execution of the following code and answer the question below:

int fib[ ] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };

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

{

fib[0] = fib[ fib[i] ];

fib[ fib[i] ] = fib[i];

fib[i] = fib[0];

}

cout << "fib[0] = " << fib[0];

What value is printed by the code above (i.e., what is the content of fib[0] after executing

the code)?

index 0 1 2 3 4 5 6 test 8 6 0 3 4 9 2

Explanation / Answer

total = total + arr[i];

20

line 2

False

99

8