C++ hand trace help.... perform a hand-trace of the code by making a table of va
ID: 3820896 • Letter: C
Question
C++ hand trace help....
perform a hand-trace of the code by making a table of values by hand or using Microsoft Word’s table feature. Write down the contents of the array after each pass of the outside for loop. Keep track of all changing variales.
const int SIZE = 6;
int main() {
int minIndex, minValue,
numValues = SIZE;
int a[SIZE] = {3, 2, 1, 5, 6, 4};
for (int j = 0; j < numValues - 1; j++) {
minIndex = j;
minValue = a[j];
for (int i = j + 1; i < numValues; i++)
if (a[i] < minValue) {
minValue = a[i];
minIndex = i;
}
a[minIndex] = a[j];
a[j] = minValue;
}
Explanation / Answer
Content of array after each pass
1 2 3 5 6 4
1 2 3 5 6 4
1 2 3 5 6 4
1 2 3 4 6 5
1 2 3 4 5 6
3 2 1 5 6 4
j = 0
minIndex = 0
minValue = a[0] = 3
i = 1, a[i] = 2 (if condition true), minIndex = 1, minValue = 2 increment i
i = 2, a[i] = 1 (if condition true), minIndex = 2, minValue = 1 increment i
i = 3, a[i] = 5 (if condition false), increment i
i = 4, a[i] = 6 (if condition false), increment i
i = 5, a[i] = 4 (if condition false), increment i
a[2] = 3
a[0] = 1
1 2 3 5 6 4
j = 1
minIndex = 1
minValue = a[1] = 2
i = 2, a[i] = 3 (if condition false),
i = 3, a[i] = 5 (if condition false), increment i
i = 4, a[i] = 6 (if condition false), increment i
i = 5, a[i] = 4 (if condition false), increment i
a[1] = 2
a[1] = 2
1 2 3 5 6 4
j = 2
minIndex = 2
minValue = a[2] = 3
i = 3, a[i] = 5 (if condition false), increment i
i = 4, a[i] = 6 (if condition false), increment i
i = 5, a[i] = 4 (if condition false), increment i
a[2] = 3
a[2] = 3
1 2 3 5 6 4
j = 3
minIndex = 3
minValue = a[3] = 5
i = 4, a[i] = 6 (if condition false), increment i
i = 5, a[i] = 4 (if condition true), minIndex = 5,minValue = 4 increment i
a[5] = 5
a[3] = 4
1 2 3 4 6 5
j = 4
minIndex = 4
minValue = a[4] = 6
i = 5, a[i] = 5 (if condition true), minIndex = 5,minValue = 6 increment i
a[5] = 6
a[4] = 5
1 2 3 4 5 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.