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 04:40
Amain idea is supported, explained, or expanded on with a numbers b. a concluding statement c. details d. a topic sentence. im e paragraphs reset nex next
Answers: 2
question
Computers and Technology, 22.06.2019 08:50
Can online classes such as gradpoint track your ip location like if im taking a final and i give somebody else my account and they take the final for me will it show where they are taking the final from? and can this be countered with a vpn
Answers: 1
question
Computers and Technology, 22.06.2019 11:00
What are two of the most common reasons that peolpe who need mental health care do not access it?
Answers: 1
question
Computers and Technology, 23.06.2019 18:30
The computers in the sales department did not have enough data storage capacity to contain all the information the department needed to store, and it was taking a long time for team members to access the data they needed. to fix the problem, the technician installed new, larger hard drives on all the computers.
Answers: 1
You know the right answer?
Translate the following sum function from iterative to

* recursive.

*
<...
Questions
question
Mathematics, 15.10.2019 05:20
question
Mathematics, 15.10.2019 05:20
question
Biology, 15.10.2019 05:20
Questions on the website: 13722361