2

This method converts a frequency array into a cumulative frequency array. For example if the initial array was { 1, 2, 3, 4} calling the method should give you { 1, 3, 6, 10}

This is what Ive written:

public void cumulate(int[] a)
{
   for (int i= 0; i < a.length; i ++){
          a[i] = a[i-1] + a[i];
    }
}

I'm certain its wrong, but I do need help with generating another set of codes. If anyone is able to assist me, that would be lovely!

4

1 回答 1

3

好吧,您将在 i = 0 的数组之外。所以从 1 开始:

public void cumulate(int[] a) {
   for (int i = 1; i < a.length; i++){
          a[i] = a[i - 1] + a[i];
    }
}

现在应该可以了。

于 2012-06-05T16:49:20.360 回答