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?

5
  • 2
    look up java for loops which is probably what your instructor wanted you to use. Mar 21, 2012 at 22:06
  • 3
    How far along are you in the course? Mar 21, 2012 at 22:07
  • 12
    What you did wrong was writing a program and then not running it to see if it compiled and did what you wanted.
    – Don Roby
    Mar 21, 2012 at 22:18
  • 1
    @scrappedcola unfortunately, that is not the only and main mistake
    – om-nom-nom
    Mar 21, 2012 at 22:19
  • int sum=Arrays.stream(A).sum(); May 31, 2018 at 7:14

6 Answers 6

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();
2
  • 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. Oct 15, 2013 at 16:32
  • 1
    You'll be surprised. Here is a benchmarks I took that shows that the parallel version is much faster: thecoderzone.com/arrays-and-streams
    – msayag
    Oct 16, 2013 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.

6
  • @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. Mar 21, 2012 at 22:36
  • Oh sorry I didn't know I was suppose to do that. Were can i mark it as answered? Mar 21, 2012 at 22:42
  • 1
    There is a grey tick below the score for this question. Click it and it will turn green. Mar 21, 2012 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, 2013 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 stackoverflow.com/help/privileges/vote-down, and consider whether this question really deserves your down-vote. Oct 15, 2013 at 22:58
8
int sum=0;
for(int i:A)
  sum+=i;
4
int sum = 0;
for(int i = 0; i < A.length; i++){
  sum += A[i];
}
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);
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);
}
1
  • I don't see what this adds to the existing answers.
    – Prune
    Apr 14, 2016 at 22:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.