Results 1 to 4 of 4
Thread: Array help, thanks in advance
 02142011, 08:12 AM #1Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
Array help, thanks in advance
So i have an array that has a size that is based on the users input (order0) and the cells up to (order01) are all randomly generated and the last cell must be a value that when added to the sum of the (order01) 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<(order01); i++){
sumArray[i] = generator.nextInt(s/order0);
}
for (int i=0; i<(order01); i++){
System.out.println(sumArray[i]);
}
// sums all the arrays up until the last value
for (int i=0; i<(order01); 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
 02142011, 10:38 AM #2
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.
 02142011, 08:30 PM #3Member
 Join Date
 Feb 2011
 Posts
 3
 Rep Power
 0
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#sumsumofrandoms. 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
 02152011, 03:03 AM #4Member
 Join Date
 Nov 2010
 Posts
 6
 Rep Power
 0
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 05. You are assigning in your final calculated element (100sum(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[order01] = lastsum;
Similar Threads

question. thanks in advance for your help
By farahm in forum New To JavaReplies: 4Last Post: 11222010, 12:25 PM 
small problem, thanks in advance for your help
By farahm in forum New To JavaReplies: 4Last Post: 11222010, 10:26 AM 
How to advance ?
By House in forum New To JavaReplies: 14Last Post: 08292010, 03:29 AM 
Advice for Advance projects?
By suyash89 in forum Advanced JavaReplies: 7Last Post: 01022010, 11:51 PM
Bookmarks