I have to find the total result of 10 numbers

Page 1 of 1

5 Replies - 212 Views - Last Post: 07 November 2011 - 09:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254688&amp;s=8741a0999a282803ee94d8934aa5b28d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 chica911

Reputation: 1
• Posts: 15
• Joined: 07-November 11

I have to find the total result of 10 numbers

Posted 07 November 2011 - 08:29 AM

this is what i have so far. It compiles but the out put is always Total of arrayName elements: 0. please help me thank you
// Find the total result of 10 numbers

public class SumArray
{
public static void main(String args[])
{
int[] arrayName;
arrayName = new int [10];
int total = 0;

// add each element's value to total
for (int counter = 0; counter <arrayName.length; counter++)
total= arrayName[counter];
System.out.printf("Total of arrayName elements:" + " " + total);

}  // end main
}  // end of class InitArray2

Is This A Good Question/Topic? 0

Replies To: I have to find the total result of 10 numbers

#2 Ryano121

• D.I.C Lover

Reputation: 1055
• Posts: 2,234
• Joined: 30-January 11

Re: I have to find the total result of 10 numbers

Posted 07 November 2011 - 08:32 AM

Well you haven't given the values of the array elements any other values. Therefore the default value - zero - will be used. Essentially at the moment you are adding up ten lots of zero.

You will probably want to have another loop that gives values to the array elements, like -

for(int i = 0; i < arrayName.length; i++)
{
arrayName[i] = i;
}

Which will give you an array containing the following values -

0,1,2,3,4,5,6,7,8,9

#3 chica911

Reputation: 1
• Posts: 15
• Joined: 07-November 11

Re: I have to find the total result of 10 numbers

Posted 07 November 2011 - 08:50 AM

i tried that but now it only prints 9 and i dont think thats the total of the 10 numbers.

i tried that but now it only prints 9 and i dont think thats the total of the 10 numbers.

#4 AMZDeCoder

Reputation: 25
• Posts: 90
• Joined: 04-November 11

Re: I have to find the total result of 10 numbers

Posted 07 November 2011 - 08:54 AM

My Solution:
1) You should assign some values to your array.
2) The statement should be outside your for loop,
System.out.printf("Total of arrayName elements:" + " " + total);

3) You should do the following, the total should be initialized to zero and all elements should be successively added to it.
total += arrayName[counter];

Hope this helps.

#5 jon.kiparsky

• Pancakes!

Reputation: 5439
• Posts: 8,756
• Joined: 19-March 11

Re: I have to find the total result of 10 numbers

Posted 07 November 2011 - 08:56 AM

Pretend that this is totally new code to you:

arrayName[counter] = 42
total= arrayName[counter];
System.out.printf("Total of arrayName elements:" + " " + total);

What would you expect this code to print?

This post has been edited by jon.kiparsky: 07 November 2011 - 08:57 AM

#6 chica911

Reputation: 1
• Posts: 15
• Joined: 07-November 11

Re: I have to find the total result of 10 numbers

Posted 07 November 2011 - 09:01 AM

Thank you, moving that statement outside my for loop fixed the problem. it works great!