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

Hand Execution of Arrays Question1) D emonstrate how arrays work by hand executi

ID: 3753958 • Letter: H

Question


Hand Execution of Arrays

Question1) Demonstrate how arrays work by hand executing a number of small code snippets.

Program 1: Demonstrate how the following code works with the supplied values double ( senst vector &data ) int result-0; if data[i] - 0 ) result++ return result; 1. Include the following on the first image 1. Hand execute the above with a vector that has the followingdata data: 1 70 1 3 I 12 I 2. Hand execute the above with a vector that has the following data data: 1 02 0 11I -1 I 3. In the image, note an appropriate name you could use for this function

Explanation / Answer

Program 1:
=================
The function iterates the elements in the input vector and whenever it finds an element = 0, it counts it by incrementing result

case 1. when input data = 7, 0 , 1, -3, 12
--------------------------------------
result = 0
for i = 0, data[i] = 7, no change to result
for i = 1, data[i] = 0, increment result, result = 1
for i = 2, data[i] = 1, no change to result
for i = 3, data[i] = -3, no change to result
for i = 4, data[i] = 12, no change to result

The function returns 1

case 1. when input data = 0, 2, 0, 11, -1
--------------------------------------
result = 0
for i = 0, data[i] = 0, increment result, result = 1
for i = 1, data[i] = 2, no change to result
for i = 2, data[i] = 0, increment result, result = 2
for i = 3, data[i] = 11, no change to result
for i = 4, data[i] = -1, no change to result

The function returns 2


Program 2:
==========
The function receives a reference to input vector and it modifies the vector, such that each -ve element is converted to +ve number by multiplying by -1

case 1: when input data is = 2, -5, 0, -3, 23
----------------------------------------------
for i = 0, data[i] = 2, no change to element
for i = 1, data[i] = -5, set data[1] = 5
for i = 2, data[i] = 0, no change to element
for i = 3, data[i] = -3, set data[3] = 3
for i = 4, data[i] = 24,no change to element


So the function modifies the vector as 2, 5, 0, 3, 23

case 2: when input data is = 0, -2, -4, 11, -1
----------------------------------------------
for i = 0, data[i] = 0,no change to element
for i = 1, data[i] = -2, set data[1] = 2
for i = 2, data[i] = -4, set data[2] = 4
for i = 3, data[i] = 11, no change to element
for i = 4, data[i] = -1, set data[4] = 1

So the function modifies the vector a s 0, 2, 4, 11, 1


Program 3:
==========
The function checks if the index idx is a valid index i.e. in range from 0 to size-1. If idx is valid, then the last element in the vector is copied over to index idx i.e. element at idx is overwritten with the last element. Then the last element is popped off i.e. removed from the vector

case 1: when input data = 2, -5, 0, 1, 23 and idx = 2
----------------------------------
The element at data[2] i.e. element 0 is replaced with last element i.e. 23. The last element 23 is later removed.
The vector gets modified to 2, -5, 23, 1

case 2: when input data = 0, -2 and idx = 1
----------------------------------
The element at data[1] i.e. element -2 is replaced with last element i.e. -2. The last element -2 is later removed.
The vector gets modified to 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote