logo Practice-It logo

delta

Language/Type: Java arrays
Author: Marty Stepp (on 2010/12/28)

Write a method named delta that accepts an array of integers as a parameter and returns a new array formed by inserting between each pair of values the difference between those values. For example, given this array:

int[] numbers = {3, 8, 15};

The call of delta(numbers) should return the following array:

{3, 5, 8, 7, 15}

In this example, 5 is inserted between 3 and 8 because (8 - 3) is 5, and 7 is inserted between 8 and 15 because (15 - 8) is 7. The difference should always be computed as the second value minus the first, so you can get negative values. For example, given the following array:

int[] numbers2 = {3, 8, 2, 5, 1, 9};

The call of delta(numbers2) should return the array:

{3, 5, 8, -6, 2, 3, 5, -4, 1, 8, 9}

You may assume that the array passed is not null. You may assume that the array contains at least one element.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.