SUM67 Return the sum of the numbers in the array except igno

SUM67 Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers. sum67([l, 2, 2]) rightarrow 5 sum67([l, 2, 2, 6, 99, 99, 7]) rightarrow 5 sum67([1, 1, 6, 7, 2]) rightarrow 4 TENRUN For each multiple of 10 in the given array, change all the values    following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}. tenRun([2, 10, 3, 4, 20, 5]) rightarrow [2, 10, 10, 10, 20,    20] tenRun([10, 1, 20, 2]) rightarrow [10, 10, 20, 20] tenRun([10, 1, 9, 20]) rightarrow [10, 10, 10, 20] TRIPLEUP Return true if the array contains, somewhere, three Increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25. tripleUp([1, 4, 5, 6, 2]) rightarrow true trlpleUp([1, 2, 3]) rightarrow true tripleUp([l, 2, 4]) rightarrow false ZEROMAX Return a version of the given array where each zero value In the array is replaced by the largest odd value to the right of the zero In the array. If there is no odd value to the right of the zero, leave the zero as a zero. zeroMax([0, 5, 0, 3]) rightarrow [5, 5, 3, 3] zeroMax([0, 4, 0, 3]) rightarrow [3, 4, 3, 3] zeroMax([0, 1, 0]) rightarrow [1, 1, 0]

Solution

Here is the solution to the first question:

public static int sum67(int[] arr)
{
int sum = 0;
for(int i = 0; i < arr.length; i++)   //For each value of array.
{
if(arr[i] == 6)   //If value is 6.
{
while(arr[i] != 7)   //Till you reach the value 7.
i++;       //Keep forwarding.
i++;        //Move another step beyond 7.
}
if(i >= arr.length)   //If you\'re out of array bound.
break;                //Stop computation.
sum += arr[i];        //Add the number to sum.
}
return sum;            //Return sum.
}

 SUM67 Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site