subject

Translate the following sum function from iterative to

* recursive.

*

* You should write a helper method. You may not use any "fields" to solve

* this problem (a field is a variable that is declared "outside" of the

* function declaration --- either before or after).

*/

public static double sumIterative (double[] a) {

double result = 0.0;

int i = 0;

while (i < a. length) {

result = result + a[i];

i = i + 1;

}

return result;

}

public static double sum (double[] a) {

return 0; // TODO

}

/**

* PROBLEM 2: Do the same translation for this in-place reverse function

*

* You should write a helper method. You may not use any "fields" to solve

* this problem (a field is a variable that is declared "outside" of the

* function declaration --- either before or after).

*/

public static void reverseIterative (double[] a) {

int hi = a. length - 1;

int lo = 0;

while (lo < hi) {

double loVal = a[lo];

double hiVal = a[hi];

a[hi] = loVal;

a[lo] = hiVal;

lo = lo + 1;

hi = hi - 1;

}

}

public static void reverse (double[] a) {

// TODO

}

/**

* @param args

*/

public static void main (String[] args) {

double[] list0 = new double[] {};

double[] list1 = new double[] { 5 };

double[] list2 = new double[] { -3, 5 };

double[] list3 = new double[] { 2, -3, 5 };

double[] list4 = new double[] { -1, 2, -3, 5 };

double[] list5 = new double[] { 33, 44, 55 };

System. out. println("Display the sum of the array contents");

System. out. println ("list5: " +sum (list5));

System. out. println ("list0: " +sum (list0));

System. out. println ("list1: " +sum (list1));

System. out. println ("list2: " +sum (list2));

System. out. println ("list3: " +sum (list3));

System. out. println ("list4: " +sum (list4));

System. out. println("Reversing the lists");

reverse (list0);

System. out. println ("list0: " +Arrays. toString (list0));

reverse (list1);

System. out. println ("list1: " +Arrays. toString (list1));

reverse (list2);

System. out. println ("list2: " +Arrays. toString (list2));

reverse (list3);

System. out. println ("list3: " +Arrays. toString (list3));

reverse (list4);

System. out. println ("list4: " +Arrays. toString (list4));

reverse (list5);

System. out. println ("list5: " +Arrays. toString (list5));

}

}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 08:10
Technician a says that if a valve is open when a piston rises to the very top of a cylinder, the piston may actually strike the valve head and cause serious engine damage. technician b says if the camshaft is located in the engine block, then the engine is called an overhead valve engine, ohv engine, or an in-block camshaft. who is right? a. b only b. both a and b c. a only d. neither a nor b
Answers: 3
question
Computers and Technology, 22.06.2019 11:00
in 2007, floridians died in alcohol-related collisions.a.  501b.  1,051c.  5,015d.  10,839
Answers: 1
question
Computers and Technology, 22.06.2019 16:10
Drag each label to the correct location on the imagelist the do’s and don’ts of safeguarding your password.keep yourself loggedin when you leave your computer.don’t write your password down and leave it whereothers can find it.share your password with your friends.each time you visit a website,retain the cookies on your computer.use a long password with mixed characters.
Answers: 1
question
Computers and Technology, 22.06.2019 19:20
How might the success of your campaign be affected if you haven’t carefully completed all field data or if you accidentally insert the wrong merge field in the document?
Answers: 2
You know the right answer?
Translate the following sum function from iterative to

* recursive.

*
<...
Questions
question
Mathematics, 04.07.2019 01:00
question
Mathematics, 04.07.2019 01:00
question
Mathematics, 04.07.2019 01:00
Questions on the website: 13722367