Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

help me understand code

Options
  • 10-03-2004 12:30pm
    #1
    Closed Accounts Posts: 537 ✭✭✭


    public double[] bins(double[] data, int numBins)
    {
    double[] results = new double[numBins];
    int[] count = new int[numBins];
    double scale = (data.length+1)/(double)numBins;
    for(int i=0; i<data.length;i++)
    {
    int pos = (int)i/scale;
    results[pos]+=data;
    count[pos]+=1;
    }
    for(int i=0;i<results.length;i++)
    results=results/count;
    }


    I was wondering if anyone could explain to me whats happening here in this code it is supposed to be used to split a number up into a set amount of numbers ie for example 12 split it up into 6 equal variables would be 2 4 6 8 10 12


Comments

  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    Defines a function double

    takes in two paramaters: an array of doubles and a number of 'bins'

    Defines an array of doubles called results which represents the bins.
    Defines an array of integers representing the number of items in each bin.
    Defines a double called scale to map the size of the input array onto the number of bins.

    First for loop iterates through the input numbers adding each one to a location in result[] and incrememting the corresponding location in count[]. It decides the relevant location by dividing the iteration number by the scale and 'casting' to integer (this truncates the double removing the decimal part).

    The second for loop iterates through the array of results getting the average of each location (bin) by dividing by the corresponding count value. The summation in result is then replaced with this average.

    The result is returend as an array of doubles. You're missing the return statement and the closing parentheses in the above snippet.

    Essentially, the function reduces the size of an array while maintining the distribution of values across it. (It could also increase the size of the array but the results would contain additional empty locations).


  • Closed Accounts Posts: 537 ✭✭✭JohnnyBravo


    But as its input does it expect a list of numbers ???? ie data


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    Yeah, it requires two input paramaters:

    - A set of input data (double array)
    - A number of bins (integer)


  • Closed Accounts Posts: 537 ✭✭✭JohnnyBravo


    do you think there could be a way to modify this code so it only recieves a single number such as 24 and breaks down the number so it will fit into all the bins for example 6 bins containing the number four in each


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    If you want to split one number between n locations then you should just write a function with one for loop that splits it up based on your criteria.

    For example, you could use a random number generator as the basis to split off the first (n - 1) sections then the final section would be num(n) = num - ( num(n - 1) + ... + num(1) ). That would allow you to return a (pseudo)random split of the number.


  • Advertisement
  • Closed Accounts Posts: 537 ✭✭✭JohnnyBravo


    the criteria is simple i want it to start at zero and go up in 24 increments till it reaches the max or as close to the max as possible


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    is it missing a return statment? Maye its just me (it probably is im a crap programmer), but I thought if you method wasn't void then it had to return something, in this case an array (double [] )


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    no you're right. I think its just a code snippet, not the entire method.
    it should probably be returning results.
    return results;
    i'm guessing the code is java.


Advertisement