Given an array A of 10 ints, initialize a local variable called sum and use a loop to find the sum of all numbers in the array A.
This was my answer that I submitted:
sum = 0;
while( A, < 10) {
sum = sum += A;
}
I didn’t get any points on this question. What did I do wrong?
Your syntax and logic are incorrect in a number of ways. You need to create an index variable and use it to access the array’s elements, like so:
The above example uses a
whileloop, since that’s the approach you took. A more appropriate construct would be aforloop, as in Bogdan’s answer.