Array Manipulation
11 questions · Fundamental Engineering
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”.
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.
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.
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 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.
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.
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.
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.
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.
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”.
Answer group
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.
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.
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(, the number of times the string “call” is output is blank.
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}.
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 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.
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 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.
Answer group