Adding every 2 number in an array?

Hi, I have an array of double numbers eg. double[] myNum = {1.1, 4.3, 3.1, 6.2}; How do I add up every 2 number in the array and store it into another array so the outcome would be something like this: double[] outC = {5.4, 9.3}; This is what I ve tried: double outC[] = new double[2]; for (int... show more Hi,



I have an array of double numbers eg.

double[] myNum = {1.1, 4.3, 3.1, 6.2};



How do I add up every 2 number in the array and store it into another array so the outcome would be something like this:



double[] outC = {5.4, 9.3};



This is what I ve tried:



double outC[] = new double[2];

for (int i=0;i<myNum.length;i++)

{

outC[i] = myNum[i] + myNum[i+1];

i++;

}



But it doesn t seems to work. Thanks.
1 answer 1