With that in mind, indicate the values of each element of the array after the 1st, 2nd and 3rd swap.
Keeping in mind that some of these swaps may be cases of an element being swapped with itself—those still "count" as swaps!
a[0] |
a[1] |
a[2] |
a[3] |
a[4] |
|
initial values | 27 |
6 |
42 |
9 |
17 |
after 1st swap | |||||
after 2nd swap | |||||
after 3rd swap | |||||
after 4th swap (final array) | 6 |
9 |
17 |
27 |
42 |
int main() { int a[5]={15,35,25,30,20}; int i, j;If the swap is correct, the output will be:
int temp = 0; // iniialized to zero for no good reason.
// initialize i and j i = 1; j = 4; printf("Before: a[i] = %d a[j]=%d \n",a[i],a[j]); // Now swap a[i] with a[j]
some code goes here...
// a[i] and a[j] are now swapped!
printf("After: a[i] = %d a[j]=%d \n",a[i],a[j]); }
Before: a[i]=35 a[j]=20 After: a[i]=20 a[j]=35Below and on the next page are several attempts at writing this swap. In some cases, there is a function definition that goes along with the code.
After: a[i] = _____ a[j] = ______
function definition goes earlier in file | put this at some code goes here |
---|---|
void swap(int a, int b) |
swap(a[i],a[j]); |
After: a[i] = _____ a[j] = ______
function definition goes earlier in file | put this at some code goes here |
---|---|
void swap(int *a, int *b) |
swap(&a[i],&a[j]); |
After: a[i] = _____ a[j] = ______
function definition goes earlier in file | put this at some code goes here |
---|---|
(None) |
a = temp; temp = b; b = a; |
After: a[i] = _____ a[j] = ______
function definition goes earlier in file | put this at some code goes here |
---|---|
None |
temp = a; a = b; b = temp; |
After: a[i] = _____ a[j] = ______
function definition goes earlier in file | put this at some code goes here |
---|---|
void swap(int *a, int i, int j) |
swap(a,i,j); |
Total points: ?