Results 1 to 4 of 4
  1. #1
    OriginalKopy is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default 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 (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

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  3. #3
    OriginalKopy is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    gandrew is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    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;

Similar Threads

  1. question. thanks in advance for your help
    By farahm in forum New To Java
    Replies: 4
    Last Post: 11-22-2010, 11:25 AM
  2. small problem, thanks in advance for your help
    By farahm in forum New To Java
    Replies: 4
    Last Post: 11-22-2010, 09:26 AM
  3. How to advance ?
    By House in forum New To Java
    Replies: 14
    Last Post: 08-29-2010, 03:29 AM
  4. Advice for Advance projects?
    By suyash89 in forum Advanced Java
    Replies: 7
    Last Post: 01-02-2010, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •