3

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?

Kailua Bum
  • 1,368
  • 7
  • 25
  • 40
Jordan Westlund
  • 501
  • 2
  • 5
  • 5

6 Answers6

50

Once is out (March 2014) you'll be able to use streams:

int sum = IntStream.of(a).sum();

or even

int sum = IntStream.of(a).parallel().sum();
msayag
  • 8,407
  • 4
  • 30
  • 29
  • *So* excited for Java 8. I doubt you'll see any performance benefit from `parallel` in this case, though. In fact, I bet performance will be significantly slower that way. – StriplingWarrior Oct 15 '13 at 16:32
  • 1
    You'll be surprised. Here is a benchmarks I took that shows that the parallel version is much faster: http://www.thecoderzone.com/arrays-and-streams – msayag Oct 16 '13 at 23:15
16

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:

int i = 0;        // Create a separate integer to serve as your array indexer.
while(i < 10) {   // The indexer needs to be less than 10, not A itself.
   sum += A[i];   // either sum = sum + ... or sum += ..., but not both
   i++;           // You need to increment the index at the end of the loop.
}

The above example uses a while loop, since that's the approach you took. A more appropriate construct would be a for loop, as in Bogdan's answer.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • @JordanWestlund: Welcome to StackOverflow. If I answered your question sufficiently, please mark the question as answered so other users know they don't need to pay attention to it anymore. – StriplingWarrior Mar 21 '12 at 22:36
  • Oh sorry I didn't know I was suppose to do that. Were can i mark it as answered? – Jordan Westlund Mar 21 '12 at 22:42
  • 1
    There is a grey tick below the score for this question. Click it and it will turn green. – Mike Chamberlain Mar 21 '12 at 22:50
  • 1
    Should not assume that you have actually been given an array of 10, should do a check for length i.e. A.length – Clarkey Oct 15 '13 at 16:11
  • @Clarkey: While this may not be as pretty or maintainable as you'd like, it does answer the question very directly. I could tell that the OP was struggling with the simplest of principles, so I opted not to confuse him by throwing in a `for` loop or a `.length` check (although I referred him to an answer that does). Please review http://stackoverflow.com/help/privileges/vote-down, and consider whether this question really deserves your down-vote. – StriplingWarrior Oct 15 '13 at 22:58
  • Or get rid of the i++ in the body by using a for loop. – MarcusJ Feb 22 '16 at 03:16
8
int sum=0;
for(int i:A)
  sum+=i;
kasavbere
  • 5,873
  • 14
  • 49
  • 72
4
int sum = 0;
for(int i = 0; i < A.length; i++){
  sum += A[i];
}
Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57
4

When you declare a variable, you need to declare its type - in this case: int. Also you've put a random comma in the while loop. It probably worth looking up the syntax for Java and consider using a IDE that picks up on these kind of mistakes. You probably want something like this:

int [] numbers = { 1, 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 is: " + sum);
Mark Rhodes
  • 10,049
  • 4
  • 48
  • 51
1

Here is an efficient way to solve this question using For loops in Java

 public static void main(String[] args) {

    int [] numbers = { 1, 2, 3, 4 };
    int size = numbers.length;

    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += numbers[i];
    }

    System.out.println(sum);
}
ella2469
  • 11
  • 1