# sum of elements in array

• 07-12-2010, 07:29 PM
myst
sum of elements in array
The task is to write a program that returns the index of the cell in the array where the sum of the elements in the array until that cell's element equals the sum of the elements after it until the end. This is for any size array.
If there is no index like that, return -1.

for example: 2 1 6 5 4 returns 2 since 2+1+6=5+4 and 6 is in place 2
however: 3 2 7 returns -1...

Attempt:

I tried writing a program that would check the array like this...
Code:

```2+1+6+5 =4, 2+1+6=5+4, 2+1=6+5+4, 2=1+6+5+4.```

Code:

```public class EqualSum {     public static int equalSum(int[]a){         int counter=0; // counter saves the values of a[j]         for (int j=a.length-1; j>0; j--){             if (sum(a, a[j-1]) == (a[j] + counter)) // this checks if the sum from the first cell through the j-1 cell equals the j cell. Since a[j] gets 1 smaller every time, counter keeps the values of the previous a[j]'s...                 return j-1;             counter += a[j];            }         return -1;     }         public static int sum(int[]array, int until){         int counter=0;         for (int i=0; i<until; i++)             counter+= array[i];         return counter;        }    }```
Can anyone find the error here (or most likely errors...)?

Thanks.
• 07-12-2010, 08:59 PM
JosAH
This little problem is so full of preconditions and loop invariant conditions that defining them solves your problem. Here goes:

suppose that 'left' is the sum of the array elements a+a ... a[j] and 'right' is the sum of the elements a[j+1]+a[j+2] ...

suppose the length of the array 'a' is 'n'.

suppose we have a method sum(int[] a) that calculates the sum of all elements in array 'a'.

For starters we have j= n-1, left= sum(a) and right= 0.

If left < right we have to return -1 (but we can ignore this condition)

If left == right we return j

If left > right we repeat the test after we have adjusted the values as follows:

left-= a[j]
right+= a[j]
j= j-1

if after the adjustments j < 0 we have to return -1

All this babbling translates to code as:

Code:

```public static int equalSum(int[] a) {         int right = 0;         int left= sum(a);                         for (int j = a.length - 1; j >= 0; j--) {                 if (left == right)                         return j;                 right+= a[j];                 left-= a[j];         }         return -1; }```
kind regards,

Jos
• 07-12-2010, 09:27 PM
Norm
Quote:

Can anyone find the error here
Have you tried debugging it by adding println()s to show how values are computed.

For example:
Code:

```  public static int sum(int[]array, int until){         int counter=0;         for (int i=0; i<until; i++)             counter+= array[i];         System.out.println("counter=" + counter + ", for until=" + until); // show values         return counter;        }```
• 07-12-2010, 09:40 PM
JosAH
Quote:

Have you tried debugging it by adding println()s to show how values are computed.

That's just empirism; I like Donald Knuth's remark: "I didn't test it, I just proved it to be correct". ;-)

kind regards,

Jos
• 07-15-2010, 05:42 AM
al_Marshy_1981
Quote:

That's just empirism; I like Donald Knuth's remark: "I didn't test it, I just proved it to be correct". ;-)

kind regards,

Jos

:D thats ok if you wrote a masterpiece of barely decipherable to the average man piece of work like the art of computer programming or you know if the guy is you :p
• 07-15-2010, 08:53 AM
JosAH
Quote:

:D thats ok if you wrote a masterpiece of barely decipherable to the average man piece of work like the art of computer programming or you know if the guy is you :p

No, programming should be more science and less engineering; it makes better code and if a bit of math can give a correct answer why try to cook something up by trial and error?

kind regards,

Jos
• 07-17-2010, 02:59 AM
al_Marshy_1981
Quote:

No, programming should be more science and less engineering; it makes better code and if a bit of math can give a correct answer why try to cook something up by trial and error?

kind regards,

Jos

I agree with your philosophy, I like math but I find it hard to get enthusiastic about it, I think math courses are thought way too abstract and teachers very rarely enthuse about a problems application just it's solution. I tried researching math topics for myself and their uses but even that area seem's fairly limited, any recommendations of books or websites?
• 07-17-2010, 09:36 AM
JosAH
Quote:

I agree with your philosophy, I like math but I find it hard to get enthusiastic about it, I think math courses are thought way too abstract and teachers very rarely enthuse about a problems application just it's solution. I tried researching math topics for myself and their uses but even that area seem's fairly limited, any recommendations of books or websites?

Agreed, math by itself is dry as straw, but it's a nice toolkit: it helps you solving real world problems; that's what I like about Donald Knuth's books: he abstracts away from a problem and drills down to the bottom of its mathematical foundations, then builds up a solution in the math domain and creates a program for it, solving the original problem. The entire analysis and construction process was in the math domain where only rock hard proofs count, there's no room for speculations, engineering or just plain guessing.

kind regards,

Jos