The following statements are supposed to swap two values in
The following statements are supposed to swap two values in an array. Are they correct? if not, please fix it. data[index2] = temp; data[index1) = data[index2]; temp = data[index1];
Solution
2)
A) In the above given code, values of data at index1 and index2 are not exchanging this is because the value temp is stored in data[index2] hence the value at data[index2] will be overwrite hence it cannot be retrieved....
Given code can be modified as below for swapping of two numbers::
temp=data[index1];
data[index1]=data[index2];
data[index2]=temp;
Above given statements will complete the process of swapping by considering the third variable and one value is temporarily stored in this varaible and another value is moved to the first variable and then second variable is loaded with the temp variable value. Hence swap will be done
![The following statements are supposed to swap two values in an array. Are they correct? if not, please fix it. data[index2] = temp; data[index1) = data[index2] The following statements are supposed to swap two values in an array. Are they correct? if not, please fix it. data[index2] = temp; data[index1) = data[index2]](/WebImages/26/the-following-statements-are-supposed-to-swap-two-values-in-1068407-1761559265-0.webp)