Reputation: 31
Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data
1 4 9 16 9 7 4 9 11 then it computes
1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = - 2
I have below code so far:
import java.util.Arrays;
/**
This class computes the alternating sum
of a set of data values.
*/
public class DataSet
{
private double[] data;
private int dataSize;
/**
Constructs an empty data set.
*/
public DataSet()
{
final int DATA_LENGTH = 100;
data = new double[DATA_LENGTH];
dataSize = 0;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(double x)
{
if (dataSize == data.length)
data = Arrays.copyOf(data, 2 * data.length);
data[dataSize] = x;
dataSize++;
}
/**
Gets the alternating sum of the added data.
@return sum the sum of the alternating data or 0 if no data has been added
*/
public double alternatingSum()
{
. . .
}
}
I have to use the following class as the tester class:
/**
This program calculates an alternating sum.
*/
public class AlternatingSumTester
{
public static void main(String[] args)
{
DataSet data = new DataSet();
data.add(1);
data.add(4);
data.add(9);
data.add(16);
data.add(9);
data.add(7);
data.add(4);
data.add(9);
data.add(11);
double sum = data.alternatingSum();
System.out.println("Alternating Sum: " + sum);
System.out.println("Expected: -2.0");
}
}
Upvotes: 0
Views: 13861
Reputation: 26
int[] a = {50, 60, 60, 45, 70};
int sum = IntStream.range(0, a.length).filter(i -> i % 2 == 0).map(i -> a[i]).sum()
- IntStream.range(0, a.length).filter(i -> i % 2 == 1).map(i -> a[i]).sum();
System.out.println("Sum= " + sum);
I did this using stream, first I used Intstream, then filter out the even indexes and get the mapped the value and added them, and did the same for odd indexes as well, then subtracted it.
Upvotes: 1
Reputation: 4180
I implemented the method alternatingSum for you:
public double alternatingSum() {
double alternatingSum = 0;
if(data != null || dataSize > 0) {
for(int i = 0; i < dataSize; i = i + 2) {
alternatingSum += data[i];
}
for(int i = 1; i < dataSize; i = i + 2) {
alternatingSum -= data[i];
}
}
return alternatingSum;
}
Upvotes: 2
Reputation: 1
If You have 4 numbers, for example a[]={1, 3, 5, 6}
, there are several cases:
operation:
+ + +
+ + -
+ - +
+ - -
- + +
- + -
- - +
- - -
In your case "operation" will be only + - +
use array with this symbols and calculate your result.
int k=a[0];
for(int i = 1; i<= 3; i++){
if(operation[i-1]=="+".charAt(0))
{k=k+a[i];}
etc...
}
It's not hard :) good luck.
Upvotes: 0
Reputation: 4446
I would solve this using a for loop and a boolean flag:
set flag to false
set sum to zero
for alle elements in array
if flag is set
add to sum
else
subtract from sum
When loop is done you have your sum.
Upvotes: 1
Reputation: 8767
I would use this simple logic to achieve the goal. First add all the odd numbers in the array. Then add all the even numbers from the same. Now subtract the both values n you will get your answer. Hope this helps.
Upvotes: 1