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 01:30
Someone wishes to run the software on another computer system that runs an operating system that does not support the software what can he do
Answers: 3
question
Computers and Technology, 22.06.2019 11:30
Andrina writes letters that are regularly sent to hundreds of her company’s customers. because of this, she would like for the mail merge command to be in her quick access toolbar, and she wants it to be the first button on the left. what should andrina do to place the mail merge button there?
Answers: 1
question
Computers and Technology, 22.06.2019 17:30
1. before plugging in a new device to a computer you should unplug all other devices turn off the computer turn on the computer 2. many of the maintenance tools for a computer can be found in the control panel under administrative tools display personalization
Answers: 1
question
Computers and Technology, 22.06.2019 23:30
For her science class, elaine is creating a presentation on weather in the united states. she wants to make the presentation beautiful and interesting by drawing simple cloud or wave shapes. which is the best way for elaine to draw these shapes?
Answers: 1
You know the right answer?
Translate the following sum function from iterative to

* recursive.

*
<...
Questions
question
Chemistry, 11.02.2020 04:02
question
Mathematics, 11.02.2020 04:02