75.1k views
5 votes
Add a method named problem10 that accepts an array of integers. Move all 0's to the end of the array if any. Give two references, start and end, to initially point at the first and last elements in the array. Swap the elements at start and end if necessary. Make them gradually meet in the middle. Return the result.

User SimonRH
by
7.9k points

1 Answer

2 votes

Final answer:

The student needs to write a method to move all zeros in an integer array to the end by using start and end references to swap elements where necessary and moving those references towards the middle until they meet.

Step-by-step explanation:

The question is about adding a method named problem10 that will manipulate an array of integers to move all the zeros to the end of the array. The method will make use of two reference points, start and end, that initially point to the first and last element of the array, respectively. We will implement an algorithm that swaps elements as necessary while moving start forward and end backward until they meet in the middle. Finally, the modified array with all zeros moved to the end is returned.

To accomplish this, you will check the value at the start index; if it is not zero, you increment start. If it is zero and the value at end is not zero, you swap them and then move start forward and end backward. If the value at end is also zero, you just move end backward. You continue this process until start and end meet.

User R Arun
by
7.7k points