# Array help, thanks in advance

• 02-14-2011, 08:12 AM
OriginalKopy
So i have an array that has a size that is based on the users input (order0) and the cells up to (order0-1) are all randomly generated and the last cell must be a value that when added to the sum of the (order0-1) cells equals a certain number... here is the method i am having trouble with: public static int[] sumsarray(int s, int o){
int order0=o*2;
int sumall = 0;
int lastsum;
int sumArray[];
int lasti = order0;

//creates the one dimensional array for the sums
sumArray = new int[(order0)];

Random generator = new Random();
for (int i=0; i<(order0-1); i++){
sumArray[i] = generator.nextInt(s/order0);
}

for (int i=0; i<(order0-1); i++){
System.out.println(sumArray[i]);
}
// sums all the arrays up until the last value
for (int i=0; i<(order0-1); i++){
sumall += sumArray[i];
}
System.out.println("the sum of the first sums is:" + sumall);

lastsum = s - sumall;

System.out.println(" the last sum is:" + lastsum);

sumArray[order0]=lastsum;

return sumArray;

my question is how do i get the last sumArray value to be equal to the lastsum. thanks in advance
• 02-14-2011, 10:38 AM
j2me64
when i test your method sumarray i got a ArrayIndexOutOfBoundsException. furthermore your last question is not clear: the code generate a bunch of ints that are used for a summation in variable sumAll. then the code is computing lastsum = s - sumall. now you want to know how do i get the last sumArray value to be equal to the lastsum. i don't see any logic in your question but i hope you can illuminate me with some explanations.
• 02-14-2011, 08:30 PM
OriginalKopy
sorry if i wasnt clear enough before. the assignment that was given to me was to create a string of random numbers that will always add up to the same number (aka the magic number). this is a clever math trick when you manipulate these numbers into a two dimensional array. so the point of the program is to first ask the user what the magic sum should be, then they are asked the order (or the number of rows and columns that should be in the program) and then the program takes these two values and generates (order*2)-1 values of random numbers. for example, if the magic sum entered was 100 and the order entered was 3, the program will generate 5 random numbers but the sixth has to have a value of the magic#sum-sumofrandoms. and that last value i want to be placed into the sumArray[6] (assuming the order was 3).

hope this clears things up, thanks for the reply
• 02-15-2011, 03:03 AM
gandrew
Dear Sir: The problem is that you are assigning an array element an index equal to the array size. If you use numbers 100 & 3 in your example, the array size is set to 3*2=6. Your indices must range from 0-5. You are assigning in your final calculated element (100-sum(randoms)) the index value of 6, since order0=6. Your array has a size of 6, but it's last element can't exceed an index value of 5. That is why there is an array out of bounds error.

sumArray[order0]=lastsum;

Put in: sumArray[order0-1] = lastsum;