Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (javk):

Simple java ques about addition in array in a loop: //sum the elements in an array public static void loop7() { int[] numbers = {2,3,4,5,6,7,8,9,10}; int sum = 0; for (int i = 0; i < numbers.length; ++i) { sum = sum + numbers[i]; } System.out.println("The sum of the array is " + sum); Why is it, that when we are adding we say: sum = sum + numbers[i]; and not: sum = numbers[i]; ?

OpenStudy (turingtest):

if you do ``` sum = numbers[i] ``` that means that on each iteration through the loop you are assigning the variable sum the value numbers[i]. No addition is occurring. If you want to see what is happening try printing out the value of sum as you iterate through the loop by adding a print statement ``` //sum the elements in an array public static void loop7() { int[] numbers = {2,3,4,5,6,7,8,9,10}; int sum = 0; for (int i = 0; i < numbers.length; ++i) { sum = numbers[i]; System.out.println("The current value of sum is " + sum); } System.out.println("The sum of the array is " + sum); ```

OpenStudy (anonymous):

.sum = numbers[i]; means you are assigning the array to the variable sum, and therefore there is no calculation being executed. on the other hand, since sum has been assign a value of 0, sum = sum + numbers[i]; means 0+the array. meaing it will go 0+2=2+3=5+4=9 and so on....

OpenStudy (javk):

Thankyou very very much, just one more eensy weensy question: What does numbers.length mean?

OpenStudy (turingtest):

.length is the attribute that corresponds to the length of the array

OpenStudy (turingtest):

in other words, it's how many things are in the array `numbers`

OpenStudy (javk):

okay...so I'm kinda stuck I had this code that I was supposed to fix with minimal changes. It looked like this: //sum the elements in an array public static void loop7() { int[] numbers = {2,3,4,5,6,7,8,9,10}; int sum = 0; for(int i = 0; i <= numbers.length; ++i) { sum = numbers[i]; } System.out.println("The sum of the array is " + sum); } The comment at the start tells what it should do. So I changed it, and it works just fine now, public static void loop7() { int[] numbers = {2,3,4,5,6,7,8,9,10}; int sum = 0; for (int i = 0; i < numbers.length; ++i) { sum = sum + numbers[i]; } System.out.println("The sum of the array is " + sum); } but now I have to give an explanation... What I can't understand is what the implication was of removing the equals sign from the guard of the for loop.

OpenStudy (turingtest):

If you don't use `<` and instead use `<=` for the condition in the `for` loop, you will get an indexing error. This is because indexes of arrays start at zero. For example, if you have `int numbers[] = [4,5,6]`, then `number[0]` is `4`, `number[1]` is `5`, `number[2]` is `6`, and if you try to access `number[3]` you will get an error for going beyond the end of the array. This is called an "off by one error." Notice that the highest you could index was 2, and there were 3 elements in the array ( `numbers.length` was 3). This implies that if the array is length `numbers.length`, then the highest index it will have is `numbers.length - 1`. If you say `for (int i = 0; i <= numbers.length; ++i)`, then on the last iteration we will have `i` equal to `numbers.length`, and we will get an error for indexing beyond the end of the array. If instead you say `for (int i = 0; i < numbers.length; ++i)`, then the highest `i` will get is `numbers.length - 1`, and you will get no error.

OpenStudy (javk):

thankyou very very very much

OpenStudy (turingtest):

you're welcome

OpenStudy (javk):

I know I'm being very presumptuous when I ask this, but can you plz help me with another ques?

OpenStudy (turingtest):

if it is the one you posted above, I'm afraid I don't think I can. I am a c++ an python guy, and feel uncomfortable guessing how for each is used in java. I tried to read up on it but it was confusing to me >.<

OpenStudy (javk):

awww, :) , thanks anyways, it was really kind of you to do so, I reeeeally do appreciate it

OpenStudy (turingtest):

my pleasure :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!