Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By JosAH

Thread: sum of array

  1. #1
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default sum of array

    Hello friends,
    am trying to find the sum of thenumbers in the array i trie the following code but didnt get the answer


    public static void main(String args[]){
    int k[] = {2,5,6,7,12,34,6,7,8,9,20,21,2,2};
    int t = sum(k,0);
    System.out.println("Sum = "+t);


    can you guys help me

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: sum of array

    What is the sum method? If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing, along with any errors or exceptions you receive.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: sum of array

    cannot find symbol method sum(int[],int), thats the error i getting

  4. #4
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: sum of array

    the code is

    public class sum {
    public static void main(String args[]){
    int k[] = {2,5,6,7,12,34,6,7,8,9,20,21,2,2};
    int t = sum(k,0);
    System.out.println("Sum = "+t);
    }
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: sum of array

    Makes sense, since sum() is not a standard method that you can just use whenever you want. Sounds like your assignment is to write the sum method yourself. Recommended reading: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: sum of array

    If you haven't written a sum() method then Java won't create one for you.
    You need to write the code to loop around that array adding the numbers up.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: sum of array

    thank you,but my intention is to get the sum of all numbers in the array
    can you give me the right code,or my code is wrong or not?

  8. #8
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: sum of array

    thank you

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: sum of array

    We aren't just going to "give you the code" and asking us to do so is pretty rude. Make an attempt. Did you read the link I already gave you?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    rinopjoseph06 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: sum of array

    sorry thank you

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

    Default Re: sum of array

    Quote Originally Posted by Tolls View Post
    If you haven't written a sum() method then Java won't create one for you.
    It doesn't? darn, that's why the compiler keeps on whining about my 'makeMeFilthyRichNow()' method call.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: sum of array

    Try looking in the tutorials I have written. I believe one shows how to add an array recursively. This should be easily adapted to do w/o recursion.

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  2. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

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