// 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
I have to find the total result of 10 numbers
Page 1 of 15 Replies - 212 Views - Last Post: 07 November 2011 - 09:01 AM
#1
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
Replies To: I have to find the total result of 10 numbers
#2
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 -
Which will give you an array containing the following values -
0,1,2,3,4,5,6,7,8,9
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
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.
i tried that but now it only prints 9 and i dont think thats the total of the 10 numbers.
#4
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,
3) You should do the following, the total should be initialized to zero and all elements should be successively added to it.
Hope this helps.
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
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:
What would you expect this code to print?
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
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!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|