Suppose an array a contains 3 9 6 12. Walk through the following code that swaps two elements in the array.123456789int i = 1;int j = 2;// Good swapdouble temp = a[i];a[i] = a[j];a[j] = temp;// Bad swapa[i] = a[j];a[j] = a[i];Track variables in a trace table. On a sheet of paper, make the table below (the first row is filled in with initial values).i j a[0] a[1] a[2] a[3] temp1 2 3 9 6 12 1)What is the value of temp after line 4 executes?CheckShow answer2)What is the value of a[1] after line 5 executes?CheckShow answer3)What is the value of a[2] after line 6 executes?CheckShow answer4)What is the value of a[1] after line 8 executes?CheckShow answer5)What is the value of a[2] after line 9 executes?CheckShow answer
Question
Suppose an array a contains 3 9 6 12. Walk through the following code that swaps two elements in the array.123456789int i = 1;int j = 2;// Good swapdouble temp = a[i];a[i] = a[j];a[j] = temp;// Bad swapa[i] = a[j];a[j] = a[i];Track variables in a trace table. On a sheet of paper, make the table below (the first row is filled in with initial values).i j a[0] a[1] a[2] a[3] temp1 2 3 9 6 12 1)What is the value of temp after line 4 executes?CheckShow answer2)What is the value of a[1] after line 5 executes?CheckShow answer3)What is the value of a[2] after line 6 executes?CheckShow answer4)What is the value of a[1] after line 8 executes?CheckShow answer5)What is the value of a[2] after line 9 executes?CheckShow answer
Solution
-
The value of temp after line 4 executes is 9. This is because temp is assigned the value of a[i], and i is 1, so a[i] is a[1] which is 9.
-
The value of a[1] after line 5 executes is 6. This is because a[i] is assigned the value of a[j], and i is 1 and j is 2, so a[i] is a[1] and a[j] is a[2] which is 6.
-
The value of a[2] after line 6 executes is 9. This is because a[j] is assigned the value of temp, and j is 2, so a[j] is a[2] and temp is 9.
-
The value of a[1] after line 8 executes is 9. This is because a[i] is assigned the value of a[j], and i is 1 and j is 2, so a[i] is a[1] and a[j] is a[2] which is 9.
-
The value of a[2] after line 9 executes is 9. This is because a[j] is assigned the value of a[i], and i is 1 and j is 2, so a[j] is a[2] and a[i] is a[1] which is 9.
Similar Questions
Suppose the array a contains 3 4 6 9 12 13 and currentSize is 5. Walk through the following code that removes the element at position 1.123456int pos = 1;for (int i = pos + 1; i < currentSize; i++){ a[i - 1] = a[i];}currentSize--;Track variables in a trace table. On a sheet of paper, make the table below (the first row is filled in with initial values):currentSize i a[0] a[1] a[2] a[3] a[4] a[5]5 3 4 6 9 12 13Add a row to the table after every iteration of the for loop.1)Lines 1 and 2 execute. What is the value of i?CheckShow answerCorrect22)Line 4 executes. What is the value of a[1]?CheckShow answer3)The execution flow returns to line 2. What is the value of i?CheckShow answer4)Line 4 executes. What is the value of a[2]?CheckShow answer5)The execution flow returns to line 2. What is the value of i?CheckShow answer6)Line 4 executes. What is the value of a[3]?CheckShow answer7)The execution flow returns to line 2. What is the value of i?CheckShow answer8)The for loop completes, and line 6 executes. What is the value of currentSize?
Suppose the array a contains 2 8 11 14 and currentSize is 4. What would happen if one moved the elements starting at the insertion position instead of starting at the end of the array? Explore with the following code walkthrough.12345678int pos = 1;int newElement = 6;currentSize++;for (int i = pos + 1; i < currentSize; i++){ a[i] = a[i - 1];}a[pos] = newElement;Track variables in a trace table. On a sheet of paper, make the table below (the first row is filled in with initial values):currentSize i a[0] a[1] a[2] a[3] a[4]4 2 8 11 14 Add a row to the table after every iteration of the for loop.1)Lines 1, 2, and 3 execute. What is the new value of currentSize?CheckShow answer2)Next, line 4 executes. What is the value of i?CheckShow answer3)Line 6 executes. What is the value of a[2]?CheckShow answer4)The execution flow returns to line 4. What is the new value of i?CheckShow answer5)Next, line 6 executes. What is the value of a[3]?CheckShow answer6)The execution flow returns to line 4. What is the new value of i?CheckShow answer7)Next, line 6 executes. What is the value of a[4]?CheckShow answer8)The execution flow returns to line 4. The next line executed after line 4 is line _____.CheckShow answer9)What is the value of a[1]?
What is the purpose of the swap function in this program?*1 pointA. To swap the positions of two adjacent elements in the array.B. To swap the values of two variables in the main function.C. To swap the elements at the beginning and end of the array.D. To swap the positions of every two elements in the array.After the fourth pass, what is the array state?*1 pointA. {1, 2, 4, 3, 5, 6, 7, 8, 9, 10}B. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}C. {2, 1, 4, 5, 7, 3, 8, 6, 9, 10}D. {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Given an array arr of n positive integers. The task is to swap every ith element of the array with (i+2)th element.
Ben is studying matrices and wants to write a program that swaps two rows of a matrix (N, M), the program takes an input matrix, swaps the specified rows, and then displays the modified matrix.Assist him with the program.Input format :The first line consists of two space-separated integers N and M, representing the number of rows and columns in the matrix.The next N lines contain M space-separated integers each, representing the elements of the matrix.The last line consists of two space-separated integers r1 and r2, representing the row numbers to be interchanged.Output format :If the input row numbers r1 or r2 are invalid (less than 1 or greater than the number of rows), the output prints "Invalid row numbers".Otherwise, the modified matrix is printed after swapping the specified rows.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.