tags:

views:

586

answers:

7

Histogram

--------------------------------------------------------
  1 ****(4)
  2 ******(6)
  3 ***********(11)
  4 *****************(17)
  5 **************************(26)
  6 *************************(25)
  7 *******(7)
  8 ***(3)
  9 (0)
 10 *(1)
--------------------------------------------------------

basically above is what my prgram needs to do.. im missing something somewhere any help would be great :)

import java.util.Random; 
public class Histogram
{

    /*This is a program to generate random number histogram between
    1 and 100 and generate a table */

    public static void main(String args[])
    {

     int [] randarray = new int [80];
     Random random = new Random();
     System.out.println("Histogram");
     System.out.println("---------");

     int i ;
     for ( i = 0; i<randarray.length;i++)
     {  
      int temp = random.nextInt(100); //random numbers up to number value 100
      randarray[i] = temp;

     }

     int [] histo = new int [10];
     for ( i = 0; i<10; i++)
     {
      /* %03d\t, this generates the random numbers to
      three decimal places so the numbers are generated
      with a full number or number with 00's or one 0*/


      if (randarray[i] <= 10) {
       histo[i] = histo[i] + 1;
      //System.out.println("*");
      }
      else if ( randarray[i] <= 20){
      histo[i] = histo[i] + 1;
      }
      else if (randarray[i] <= 30){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <= 40){
      histo[i] = histo[i] + 1;
      }
      else if (randarray[i] <= 50){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <=60){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <=70){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <=80){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <=90){
      histo[i] = histo[i] + 1;
      }
      else if ( randarray[i] <=100){
      histo[i] = histo[i] + 1;
      }

      switch (randarray[i])
      {
      case 1: System.out.print("0-10 | "); break;
      case 2: System.out.print("11-20 | "); break;
      case 3: System.out.print("21-30 | "); break;
      case 4: System.out.print("31-40 | "); break;
      case 5: System.out.print("41-50 | "); break;
      case 6: System.out.print("51-60 | "); break;
      case 7: System.out.print("61-70 | "); break;
      case 8: System.out.print("71-80 | "); break;
      case 9: System.out.print("81-90 | "); break;
      case 10: System.out.print("91-100 | "); 
      }
       for (int i = 0; i < 80; i++)
      {
        randomNumber = random.nextInt(100)
        index = (randomNumber - 1) / 2;
        histo[index]++;
      }
    }
   }
 }
A: 

If I read that right then I think your switch statement is messed up. Your randarray values are from 0 < randarray[i] < 100 but you are only giving values up to 10 for your switch. That would probably throw something off. Just my guess.

Poindexter
A: 

You are only looping through 10 times, though you have 80 random numbers in your array.

This line is also flawed. You don't want to use the same counter variable for going through the array and determining which histogram bin to increment.

 if (randarray[i] <= 10) {
                        histo[i] = histo[i] + 1;

UPDATE:

You should try to come up with an algorithm to turn a random value into a bin, as your solution isn't scalable, and you will be getting used to bad programming habits.

James Black
hey dud could u possible without giving me the answer a sample please? ive only done 2 lessons on arrays and one on the switch statement
Chewart
+1  A: 

Your random data contains 80 values but you are only iterating through the first 10. You should be iterating through all 80. You would use histo[1], histo[2], etc instead of histo[i].

Also, the whole big switch block could be simplified into

histo[randarray[i] / 10]++;

And instead of creating the randarray and then looping through it, you could simply do this:

for(int i = 0; i < 80; i++)
{
    histo[random.nextInt(100) / 10]++;
}
Brian Schroth
<else if ( randarray[i] <=100){ histo[i] = histo[i] + 1; } switch (randarray[i]) { histo[randarray[i] / 10]++; } }> hey dude i added that in but im gettin error messages :S
Chewart
You don't need the switch statement at all. You could replace the whole program, minus the output parts, with the for loop I posted. I actually had an error in the loop which I have fixed, so maybe it will make more sense now. You are getting variables mixed up. When you do "histo[i] = histo[i] + 1;" it will not work because "i" is the random number you generated. Let's say i was 56, "histo[i]" would represent the 56th position in the histo array. But the histo array was defined to be 10 positions long! 56 is out of bounds.
Brian Schroth
Then the switch statement is just the wrong syntax. I suggest you write out the entire program in english ( http://en.wikipedia.org/wiki/Pseudocode ) before you go any further. Then go through it line by line and figure out how to represent that line in code. This will help you get a clear understanding of what you are doing.
Brian Schroth
A: 

im just new to java and a novice so finding this part quite difficult. Hey poindexter thanks for the reply :) where did you mean buddy?

Chewart
You should reply to someone using comments, not by adding an answer to the question.
iammichael
A: 

Your last for loop won't compile - there are a lot of syntax errors there.

You need to fill the hists with your random numbers, THEN print them out.

You want to print the counts in your hist array out in each case statement before you break as well.

Good luck if you continue to learn programming!

cwash
cheers man for the positive message i rly do want to learn java.. any pointers where im goin wrong? i take the switch statement out and my program is compilin no wurys. just the switch statement
Chewart
It's the last 'for' statement - the variables you have no types specified. You need to call them ints. But I
cwash
definitely think you mean to be filling up your histo array *before* you print them out in the switch statement.
cwash
thats what i need to do im so lost at moment, you can tell ive only had 2 lessons lol
Chewart
A: 

Another point to consider is that your histogram will be a flat, uniform distribution, not a normal distribution like you show in your question.

erickson
A: 

Look at the jHepWork project http://jwork.org/jhepwork/ It has Java classes H1D and H2D to build professional histograms.

Sergei