Question: /* * This program generates the alternating sum of double numbers stored in an array * For example, if the array contains 5 3 8

/*
*  This program generates the alternating sum of double numbers stored in an array
*  For example, if the array contains 5 3 8 4 then the alternating sum is:  5-3+8-4 = 6
*  the elem = 5 is at index 0 in the array (even index), elem 3 is at index 1 (odd index) etc
*  
*  Difficulty: Easy
*/
public class AlternatingSum
{
/**
Computes the alternating sum of the values in an array list
@param data an array list of values
@return the alternating sum of the values in data
*/
public static int alternating sum(int[] data)
{
  //-----------Start below here. To do: approximate lines of code = 1
  // Declare an integer variable sum to hold the sum and initialize it
 
  //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

//-----------Start below here. To do: approximate lines of code = 5
// Use a for loop to go through each integer element in the array data[]
// if the element is in an even location in the array (i.e. index 0 2 4 6 8 ...) then add it to sum
// else subtract it from the sum












//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
return sum;
}

public static void main(String[] args)
{
int[] data = { 1, 4, 9, 16, 9, 7, 4, 9, 11 };
int total = alternatingSum(data);
System.out.println("Alternating sum: " + total);
System.out.println("Expected:Alternating sum: -2");

int[] data1 = { 5, 3, 8, 4 };
total = alternatingSum(data1);
System.out.println("Alternating sum: " + total);
System.out.println("Expected:Alternating sum: 6");

int[] data2 = { 0 };
total = alternatingSum(data2);
System.out.println("Alternating sum: " + total);
System.out.println("Expected:Alternating sum: 0");
}
}


Step by Step Solution

3.40 Rating (144 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

int sum 0 End here Please do not remove this comment Reminder no changes outside the todo regions S... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!