-1

//so basically for all that is below, I'm trying to sort the random numbers that have been generated, and then 'sort' then into bins, and then for how many numbers there are in the bin, a star * will print out for every number. it will look like a histogram at the end. like this:

12 random integers in [0, 10) sorted into 2 bins:

******* 7 0.5833333 [5.0, 10.0)

***** 5 0.41666666 [0.0, 5.0) but its like its skips that last two methods - generateBins, and printBins. how would i sort the random numbers into bins depending on the number (like above) and print a * for every number in that array bin?

public class BinSort {
    final int totalBins;
    final int totalRandom;
    final float widthBin;
    int [] storeNumbers;
    int [] binCount;

public BinSort (int nBins, int nSamples, int max) {
    totalBins = nBins; //total # of bins, ie 2
    totalRandom = nSamples; //total random number generated, ie 12
    widthBin = (float) (max/totalBins); ie 2


    int [] storeNumbers = new int [max];
    for (int i = 0; i < totalRandom-1; i++) {
storeNumbers[i] = Random.rand(i, max);
System.out.println(storeNumbers[i]);
}
}
void generateBins () {
    int [] binCount = new int [totalBins];
    for (int i=0; i < totalRandom-1; i++) {
    int bin = (int)(storeNumbers[i]/ totalBins);
    Math.floor(bin);
    bin  = binCount [i];
    }
}

void printBins () {
for (int i = 0; i < binCount.length - 1; i++) {
        for (int j=0; j < binCount[j]; j ++) {
           System.out.print("*");
           System.out.println(); }
    float freq = (binCount[i]/totalRandom);
    float binMin = (i * widthBin);
    float binMax = (binMin * widthBin);
    System.out.print(binCount[i] + freq + binMin + binMax);
    System.out.println();
    }
  }
}
  • 3
    I'm sorry, what exactly is the issue you are having? – KevinO Oct 03 '18 at 02:30
  • This class doesn't skip anything -- nothing in the displayed code instantiates a `BinSort` object nor calls any methods on it. Where is the driver or test code? – KevinO Oct 03 '18 at 03:10

1 Answers1

0

In your constructor you have

int [] storeNumbers = new int [max];

The problem here is that this will create a new local variable with the same name as your instance variable, storeNumbers. Also, the size should be totalRandom, not max. You need to create a Random object that you'll use to generate random numbers. Putting this together we get:

public BinSort (int nBins, int nSamples, int max) {
  totalBins = nBins; //total # of bins, ie 2
  totalRandom = nSamples; //total random number generated, ie 12
  widthBin = (float) (max/totalBins); //ie 2

  storeNumbers = new int [totalRandom];
  Random rand = new Random();  
  for (int i = 0; i < totalRandom; i++) {
    storeNumbers[i] = rand.nextInt(max);
  }
}

This will generate totalRandom random numbers between 0 and max(exclusive) and store them the instance variable storeNumbers.

Next, in generateBins you have the same issue with

int [] binCount = new int [totalBins];

Which again will hide your instance variable binCount. The bin that a storeNumber falls into will be given by (int)(storeNumbers[i] / widthBin), and you need to increment the resulting bin by 1.

void generateBins()
{
  binCount = new int[totalBins];
  for (int i = 0; i < totalRandom; i++)
  {
    int bin = (int)(storeNumbers[i] / widthBin);
    binCount[bin] += 1;
  }
}

Finally, to the printing of the bins. This line

for (int j=0; j < binCount[j]; j ++) 

should be

for (int j=0; j < binCount[i]; j ++) 

Also, you should use printf to format the numbers you want to print.

void printBins()
{
  for (int i = 0; i < binCount.length; i++)
  {
    for (int j = 0; j < binCount[i]; j++)
    {
      System.out.print("*");
    }
    float freq = (float)binCount[i] / totalRandom;
    float binMin = i * widthBin;
    float binMax = (i+1) * widthBin;
    System.out.printf(" %d %.3f %.3f %.3f\n", binCount[i], freq, binMin, binMax);
  }
}

Test:

public static void main(String[] args)
{
  BinSort bs = new BinSort(2, 12, 10);
  bs.generateBins();
  bs.printBins();
}

Output:

***** 5 0.417 0.000 5.000
******* 7 0.583 5.000 10.000

Which I think is what you were looking for.

Be sure to compare your original code with the changes above and make sure you understand what the issues were and why the changes work.

RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16