Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cumulative array

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Cumulative array

    Hi,

    I'm a beginner at java programming and I've got some trouble with creating an array containing cumulative values of the original array.

    For example - given { 1, 2.01, 3, 4, 5, 6 }, use code to get an array {1, 3.01, 6.01, 10.01, 15.01, 21.01 }.

    The code I have at the moment is:
     public double[] accumulate() {
            double[] accumulate;
            accumulate = new double[observations.length]; 
            accumulate[0] = observations[0];
            for (int i=1; i < observations.length.length; i++) {
               accumulate[i] = observations[i]+ observations[i-1] ;
            }
             return accumulate;
      }

    where "observations[]" is the original array we're given.

    I don't know where I'm going wrong, but where I'm supposed to get 6.01 I get 5.01 instead. Any help or advice would be appreciated


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Cumulative array

    You're only adding up the current element and the immediate previous element of observations. Instead, you should be taking the current element of observations and adding that to the previous element in accumulate (since that holds the sum of all previous observations).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    spaggwoo (April 23rd, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cumulative array

    Ahh that makes a lot more sense now! Thank you so much!!

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  3. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 11:54 AM
  4. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM