5 Replies - 212 Views - Last Post: 07 November 2011 - 09:01 AM Rate Topic: -----

#1 chica911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • 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
Was This Post Helpful? 2
  • +
  • -

#3 chica911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5439
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 chica911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1