Array Manipulation

Array Manipulation

11 questions · Fundamental Engineering

Practice

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.

When the program is executed, the output is “blank”.

[Program]
integer: x ← 1
integer: y ← 2
integer: z ← 3
integer []: ar ← {0, 0}
y ← x
ar[x] ← y
z ← y
x ← z
ar[2] ← z + ar[1]
output ar[1], ar[2] // the values are separated by ", "

Answer group

From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array indexes start at 1.

The function distance receives integer arrays S and T of size n as arguments and returns the Manhattan distance of the two vectors that can be computed as ∑ni=1|Si − Ti| . Function abs, which is used by the function distance, returns the absolute value of an integer. Assume that n is 1 or greater.

[Program]
○ integer: distance(integer []: S, integer []: T, integer: n)
integer: p ← 0
integer: i
for (increase i from 1 to n by 1)
blank
endfor
return p

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The function linearSearch linearly searches the global array D using the recursive function recursive, and if an element with the same value as the argument searchKey exists, it returns that element index, otherwise -1 is returned. Assume that the array D has no duplicate elements.

[Program]
global: integer []: D ← {3, 4, 2, 5, 9}
global: integer: N ← the number of elements of D
○ integer: linearSearch(integer: searchKey)
return recursive(searchKey, N)
○ integer: recursive(integer: searchKey, integer: index)
if (index < 1)
return -1
elseif (D[index] = searchKey)
return A
else
return recursive(searchKey, B)
endif

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The function sum receives an integer array array with at least two elements and two positive integers k and m (k < m ≤ number of elements in array). It calculates the sum of even elements of the array array whose indices are from k to m.

[Program]
○ integer: sum(integer []: array, integer: k, integer: m)
integer: s ← 0
integer: i ← k
while (i ≤ m)
if (A)
s ← s + array[i]
endif
B
endwhile
return s

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

In statistics, the mode is the value that occurs most frequently in a dataset. For example, the mode of the integer array {2, 1, 1, 9, 6, 6, 2, 5, 6} is 6, as it occurs most often. The function findMode receives an integer array arr as a dataset and returns the mode for it.

[Program]
○ integer: findMode(integer []: arr)
integer: n ← the number of elements in arr
integer: m ← arr[1] /* Current mode value */
integer: m_c ← 1 /* Frequency count of mode */
integer: c, i, j
for (increase i from 1 to n - 1 by 1)
c ← 1
for (increase j from i + 1 to n by 1)
if (A)
c ← c + 1
endif
endfor
if (B)
m_c ← c
m ← arr[i]
endif
endfor
return m

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

Given an array of positive integers and a target sum, the problem is to find the subarray whose sum is equal to the target sum. A subarray is a part of the given array composed of contiguous elements. For example, when the target sum is 14, the shaded subarray in the figure satisfies the given condition.

63251173

Figure Example of an array

The procedure subArraySum receives an integer array arr and an integer value targetSum as arguments and finds the subarray whose sum is equal to the target sum. If a subarray that satisfies this condition is found, it prints their starting and ending indices, and finishes the task. Otherwise, it prints the message “No subarray found”.

[Program]
○ subArraySum(integer []: arr, integer: targetSum)
integer: sum, start, end
integer: N ← the number of elements in arr
for (increase start from 1 to N by 1)
sum ← 0
for (increase end from start to A by 1) // α
sum ← B
if (sum = targetSum)
output start, end
return
elseif (sum > targetSum)
exit the for block marked α
endif
endfor
endfor
output "No subarray found"

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array index starts at 1.

A 3 × 3 normal magic square is a 3 × 3 matrix where the sum of the elements for each row, each column, and each diagonal is the same. The square contains the numbers 1 to 9, exactly as shown in the figure.

4923578161515151515151515

Figure 3 × 3 normal magic square

The function checkMagicSquare receives a two-dimensional 3 × 3 integer array (matrix) m containing the numbers 1 to 9, and returns whether the given matrix is a magic square or not.

[Program]
○ boolean: checkMagicSquare(integer [,]: m)
integer: i, j, k
integer: dia1Sum ← 0
integer: dia2Sum ← 0
integer []: rowSum ← {0, 0, 0}
integer []: colSum ← {0, 0, 0}
for (increase i from 1 to 3 by 1)
for (increase j from 1 to 3 by 1)
rowSum[i] ← rowSum[i] + m[i,j]
colSum[i] ← colSum[i] + m[j,i]
endfor
dia1Sum ← dia1Sum + m[i,i]
dia2Sum ← dia2Sum + m[i,3 - i + 1]
endfor
if (dia1Sum ≠ dia2Sum)
return false
endif
for (increase k from 1 to 3 by 1)
if(blank)
return false
endif
endfor
return true

Answer group

From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.

The function binarySearch receives four arguments: the first argument is an array specified with the argument arr (the number of elements ≥ 1), the second argument is the value specified with the argument target, the third argument is the lower bound low of the array, and the fourth argument is the upper bound high of the array. The array arr has no duplicate elements and is sorted in ascending order. If arr has an element with the same value as target, this function returns the index of that element, and -1 otherwise.

When the function binarySearch is called as binarySearch({1, 2, 3, 4, 5, 6}, 5, 1, 6), the number of times the string “call” is output is blank.

[Program]
○ integer: binarySearch(integer []: arr, integer: target,
integer: low, integer: high)
integer: mid
if (low > high)
return -1
endif
mid ← integer part of ((low + high) ÷ 2)
if (arr[mid] > target)
output "call"
return binarySearch(arr, target, low, mid - 1)
elseif (arr[mid] < target)
output "call"
return binarySearch(arr, target, mid + 1, high)
else
return mid
endif

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The procedure maximumSubarray calculates the maximum sum of the subarray of the array T (the number of elements ≥ 1). A subarray is a contiguous portion of the array, for instance, from T[1] to T[3]. It can be as short as one element or as long as the entire array. The procedure finds one of the subarrays with the largest sum of values, and outputs the sum, the first, and the last indices of the subarray. For instance, if the content of the array is {-2, 1, -3, 4, -1, 2, 1, -5, 4}, the output will be 6, 4, and 7 representing the sum, first index, and last index, respectively. Here, the subarray from T[4] to T[7] is {4, -1, 2, 1}.

[Program]
○ maximumSubarray(integer []: T)
integer: n ← the number of elements in T
integer: i, j
integer: first, last /* the first and last indices of the subarray */
integer: sum
integer: max ← T[1] - 1
for (increase i from 1 to n by 1)
sum ← 0
for (increase j from A to n by 1)
sum ← sum + T[j]
if (sum > max)
first ← i
last ← B
max ← sum
endif
endfor
endfor
output max, first, last

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The program determines the second-largest element of an integer-type array and outputs its value. For instance, the second largest element of the array {2, 6, 9, 1, 7, 5} is 7. Here, we assume that the array has two or more elements and that no duplicate elements are present in the array.

[Program]
integer []: array ← {2, 6, 9, 1, 7, 5}
integer: i
integer: max1 ← -∞
integer: max2 ← -∞
for (increase i from 1
to the number of elements of array by 1)
if (A)
max2 ← max1
max1 ← array[i]
elseif (array[i] > max2)
B
endif
endfor
output max2

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array indexes start at 1.

Hashing in data structures is a fundamental concept used for efficient data retrieval and storage mechanisms. A program storing a key-data pairs in an array by transforming keys into array indexes using a hash function exists. A collision occurs when two keys hash to the same index in the array representing the hash table. A common method handling collisions, the probing mechanism (checking for an available element), is used in the function. The procedure insertData inserts a pair of key and data, if the element in the array is {undefined}. Assumptions are made that no data with the same key is stored, and that at least one element in the array hashTable is {undefined} when the procedure insertData is called. The function hashFunction takes a key as input and returns a hash value.

[Program]
global: integer [][]: hashTable ← {Elements comprising 1000 {undefined}}
global: integer: size ← 1000
○ integer: hashFunction(integer: key)
return (key mod size) + 1
○ insertData(integer: key, integer: data)
integer: index
index ← hashFunction(key)
while (A)
if (index = size)
B
else
index ← index + 1
endif
endwhile
hashTable[index] ← {key, data}

Answer group

OptionAB