Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started