Trace the following code segment, and give the resulting output: int arr[8] = {2
ID: 3553167 • Letter: T
Question
Trace the following code segment, and give the resulting output:
int arr[8] = {2, 6, 2, 9, 8, 8, 2, 7};
int k = 0, i, index, target;
for (i=0; i < 8; i++)
{
target = arr[i];
if ((index = seqSearch(arr, 0, i, target)) == i)
if ((index == seqSearch(arr, i+1, 8, target)) == 8)
k++
}
cout << k; // Output:
b) If the code is applied for an arbitrary array arr, what does it compute?
Explanation / Answer
a). Output: 3
b).
The program will compute the number of unique elements in the array.
In the above example, there are 3 unique elements: 6, 9, 7, so the output is 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.