# sum of array

• 03-08-2012, 05:43 PM
rinopjoseph06
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
• 03-08-2012, 05:44 PM
KevinWorkman
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.
• 03-08-2012, 05:46 PM
rinopjoseph06
Re: sum of array
cannot find symbol method sum(int[],int), thats the error i getting
• 03-08-2012, 05:47 PM
rinopjoseph06
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);
}
}
• 03-08-2012, 05:47 PM
KevinWorkman
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)
• 03-08-2012, 05:49 PM
Tolls
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.
• 03-08-2012, 05:53 PM
rinopjoseph06
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?
• 03-08-2012, 05:55 PM
rinopjoseph06
Re: sum of array
thank you
• 03-08-2012, 06:21 PM
KevinWorkman
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?
• 03-08-2012, 06:59 PM
rinopjoseph06
Re: sum of array
sorry thank you
• 03-08-2012, 07:43 PM
JosAH
Re: sum of array
Quote:

Originally Posted by Tolls
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
• 03-09-2012, 01:03 AM
sunde887
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.