Array Manipulation

Array Manipulation

17 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

From the answer group below, select the correct answer to be inserted into blank in the description.

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

[Program]
integer: x ← 1
integer: y ← 2
integer: z ← 3
x ← y
y ← z
z ← x
output the value of y and the value of z in this order, separated by a comma

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 makeNewArray receives an integer-type array with two or more elements as its argument and returns an integer-type array. When the function makeNewArray is called as makeNewArray({3, 2, 1, 6, 5, 4}), the value of the element at index 5 of the returned array is blank.

[Program]
○ integer []: makeNewArray(integer []: in)
integer []: out ← {} // array with 0 elements
integer: i, tail
add the value of in[1] to the end of out
for (increase i from 2 to the number of elements in in by 1)
tail ← out[the number of elements in out]
add the result of (tail + in[i]) to the end of out
endfor
return out

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 search returns the index of the element if the value specified by the argument target is contained in the array specified by the argument data, and returns −1 if it is not contained. data is sorted in ascending order, and there are no duplicate values.
The function search has a defect. For example, in the case of data where blank, an infinite loop occurs.

[Program]
○ integer: search(integer []: data, integer: target)
integer: low, high, middle
low ← 1
high ← the number of elements in data
while (low ≤ high)
middle ← the quotient of (low + high) ÷ 2
if (data[middle] < target)
low ← middle
elseif (data[middle] > target)
high ← middle
else
return middle
endif
endwhile
return -1

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 following program reverses the order of the elements of the integer-type array array.

[Program]
integer []: array ← {1, 2, 3, 4, 5}
integer: right, left
integer: tmp
for (increase left from 1 to (the quotient of the number of elements in array ÷ 2) by 1)
right ← A
tmp ← array[right]
array[right] ← array[left]
B ← tmp
endfor

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.

The following program moves the value of the last element of the integer-type array data to the first element. In doing so, the value of each element other than the last one is moved to the element one position after it.

[Program]
integer []: data ← {1, 2, 3, 4, 5, 6, 7, 8, 9}
integer: top, i
integer: len ← the number of elements in data
top ← data[len]
for (blank)
data[i] ← data[i - 1]
endfor
data[1] ← top

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 add stores the positive integer value specified by the argument in the global integer-type array hashArray. It returns true if the value has been stored, and returns false if the value could not be stored. Here, which element of hashArray the integer value should be stored in is determined by using the functions calcHash1 and calcHash2.
The procedure test calls the function add to store positive integers in hashArray. The content of hashArray immediately after the processing of the procedure test has finished is blank.

[Program]
global: integer []: hashArray
○ boolean: add(integer: value)
integer: i ← calcHash1(value)
if (hashArray[i] = -1)
hashArray[i] ← value
return true
else
i ← calcHash2(value)
if (hashArray[i] = -1)
hashArray[i] ← value
return true
endif
endif
return false
○ integer: calcHash1(integer: value)
return (value mod the number of elements in hashArray) + 1
○ integer: calcHash2(integer: value)
return ((value + 3) mod the number of elements in hashArray) + 1
○ test()
hashArray ← {5 elements of -1}
add(3)
add(18)
add(11)

Answer group