Results 1 to 4 of 4
  1. #1
    D-X69's Avatar
    D-X69 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Question Sum up elements in an array?

    How do I sum up all values of an array with for loop? And how do I find the average?

    For example:
    Java Code:
    int[] testResults = {80, 82, 62, 49, 16, 18};
    How do I sum up all the elements in that array?

    Can anyone give me an example of the code and some explanation. Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: Sum up elements in an array?

    Can you define an int variable that can hold the sum? Can you loop over your array?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Sum up elements in an array?


  4. #4
    Evanxtreme is offline Member
    Join Date
    May 2012
    Posts
    16
    Rep Power
    0

    Default Re: Sum up elements in an array?

    [ spoonfeeding deleted - mod ]
    Last edited by JosAH; 05-30-2012 at 05:48 PM.

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2011, 01:38 AM
  2. keep the first N elements of an array only
    By aneuryzma in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 05:03 PM
  3. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 09:36 AM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 03:33 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •