Question: You have been given an array; however, two of the elements have been stored in the incorrect position. You need to write the code

You have been given an array; however, two of the elements have

You have been given an array; however, two of the elements have been stored in the incorrect position. You need to write the code to allow for two indexes in an array to be swapped without having to reinitialise the array. The final result will be the same array, but the swapped indexes will now be in the correct positions. An example has been shown for reference below. 10 10 30 30 20 20 20 20 40 40 30 40 40 Before Swap After Swap Note: you cannot reinitialise the array and assign the values in the correct order, the above is only an example. The code must work irrespective of which indexes in the array need to be swapped. In your main() method, you may assume any default values for your array and what positions need to be swapped. Alternatively, you can also allow the user to enter these (which will require more code). As long as the indexes in the swap method are not hard-coded, the solution should suffice. You can also assume the array to be of any data type and your code should be reusable.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!