I need help in this problem on having a better explanation, as well as having a
ID: 3741755 • Letter: I
Question
I need help in this problem on having a better explanation, as well as having a working with the correct output. thank you
S. This problem is regarding the practice exam (Problem 5- Reversed Elements Method). The problem is restated here for your information: (10 Points) Implement a method that reverses elements in the array provided as an argumer publis class ReverseEelements ( public static void reverse(doublell a) // write your code here public static vold main(Stringl aces) doublell a -(1.1, 5.3, 3.4, 8.9,0.0, 1.3, 5.2, 7.8, 9.9 System.out.printin("initial array-Arrays.toString(a)) // call reverse method reverse(a) System.out.printin("reversed array-Arrays.toStringla): Example program output initial array [1.1, 5.3, 3.4, 8.9, 0.0, 1.3, 5.2, 7.8,9.9 reversed array [9.9, 7.8, 5.2, 1.3, 0.0, 8.9, 3.4, 5.3,1.1] To practice this problem, a CSC 20 Student arrived at the following solution: public static void reverse (doublelJ a) t be // write your code here double[] b = new double [a.length); for (int i-a.length-1,indexForB-0 i>-0 i-,indexForB++) VblindexForB]-ali]; a bix t has to he in Cerms aa Please analyze the provided code to determine if it is w providing a short explanation to the student. eny has painteo k. The student lumn2 ?o tle (.llvwh? explanaHen na index tewExplanation / Answer
public class Main {
public static void reverse(double[] a) // called function where a contain {1.1, 5.3, 3.4, 8.9, 0.0, 1.3, 5.2, 7.8, 9.9}
{
double [] b = new double[a.length]; // Create a new array to store the elements in reverse order, where size of a and b arrays are same so declare size of array is [a.length]
for(int i = a.length-1, indexForB=0; i>=0; i--, indexForB++) {
// initially i value is a.length-1 which means index at last postion, and the element in that position must be first element of revese array so declare indexForB=0, i index travel from last to first and reverse array increase index from first to last
b[indexForB] = a[i]; // Here last eleent assign to first position of b so the array become reverse
}
a = b; // Assign a is equal to b, a is replaced with revrse elemnts of the array
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.