Results 1 to 9 of 9
  1. #1
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Lightbulb Java Histogram program.. any adivce or hints wouls be great!!

    hey guys im tryin to figure out my latest program, and would possibly accept some input as to how and what im doing wrong at 'the moment' my aim is to generate a histogram of 80 random numbers between 1-100 and diplaying each random as an * in the index it should be in... i have done most of it but come to a halt with the final bit..


    Java Code:
    	public static void main(String args[])
    	{
    
    		int [] randarray = new int [80];
    		Random random = new Random();
        
    	 
    		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 || randarray[i] > 11)
    			histo[i] = histo[i] + 1;
    			//System.out.println("*");
    			else if (randarray[i] < 21 || randarray[i] > 30)
    			histo[i] = histo[i] + 1;
    			//System.out.println("*");
    			else if ( randarray[i] < 31 || randarray[i] > 40)
    			histo[i] = histo[i] + 1;
    			//System.out.println("*");
    			else if (randarray[i] < 41 || randarray[i] > 50)
    			histo[i] = histo[i] + 1;
    			//System.out.println("*");
    			else if ( randarray[i] < 51 || randarray[i] > 60)
    			histo[i] = histo[i] + 1;
    			//System.out.println("*");
    	
    			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 | "); 
    			}
    	}
    
     }
    }

  2. #2
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    A couple of things...

    Firstly you are confusing OR with AND

    The AND symbol is &&
    The OR symbol is ||

    Example AND usage...
    Java Code:
    if (hungry && haveMoney) {
      buyFood()
    }
    Example OR usage...
    Java Code:
    if (poor || homeless) {
      giveMoney()
    }

    You can simplify your conditional statements:
    Java Code:
    int x = 25;
    
    if (x <= 10) {
      // A number less than 10
    } else if (x <= 20) {
      // A number greater than 10, but less than 20
    } else if (x <= 30) {
      // A number greater than 20, but less than 30
    } else if ...etc...

    And finally you don't need to store the random values in an intermediate array because you can calculate where to store them in histo[] by...

    (0 - 1) / 2 = 0
    (1 - 1) / 2 = 0
    (2 - 1) / 2 = 0
    ...
    (10 - 1) / 2 = 0

    (11 - 1) / 2 = 1
    ...
    (20 -1) / 2 = 1
    etc

    So,
    Java Code:
    ...
    for (int i = 0; i < 80; i++)
    {
      randomNumber = random.nextInt(100)
      index = (randomNumber - 1) / 2;
      histo[index]++;
    }

    Try figure out how to use loops to print the ranges and *s :-)
    Last edited by Turtle; 11-18-2009 at 01:59 PM. Reason: "AND with OR" changed to "OR with AND". :-P

  3. #3
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    many thanks turtle will try and do it now will keep u posted =)

  4. #4
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    Hey turtle i have ammended my program and took in what you have said to me. Im finding this part quite difficult please could you point out where i may be going wrong. i know ive nearly finished it. so close but yet so far lol

    Java Code:
    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();
        
    	 
    		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]++;
    			}
    	}
    
     }
    }

  5. #5
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    I see you have:
    * simplified the conditional statements (good)
    * copy-and-pasted my example code at the end of the program (bad)

    You should look at your program, and examine what it is doing step-by-step, making sure that these steps are necessary and correct.

    The code I talked about was not meant to be tagged on the end of the program to make it work correctly. It was intended to show that you can replace randarray entirely :-)

    I will not talk about how to replace randarray now. Instead I will walk through the problems of your current program so that you might learn from them:

    This part is good. You populate an array of 80 elements with random number between 0 and 100.
    Java Code:
    		int[] randarray = new int [[B]80[/B]];
    		Random random = new Random();
    		for (int i=0; i<randarray.length; i++) { 	
    			randarray[i] = random.nextInt(100);
    		}

    The problem with the following code is that the loop stops too soon (after 10 times). You want to loop 80 times, once for every element in randarray. And if the element is within a certain range you want to add one to a certain index in histo.
    Java Code:
    		int [] histo = new int [10];
    		// looping too few times (should be 80)
    		for (int i = 0; i<[B]10[/B]; i++)
    		{
    			// Checking at index i (okay if looping 80 times)
    			if (randarray[i] <= 10) {
    				// Storing at index i (not okay, needs to be [B]0[/B])
    				histo[[B]i[/B]] = histo[[B]i[/B]] + 1;
    			}
    			else if ( randarray[i] <= 20){
    				// Storing at index i (not okay, needs to be [B]1[/B])
    				histo[[B]i[/B]] = histo[[B]i[/B]] + 1;
    			}
    			...

    So the code should read:
    Java Code:
    		int [] histo = new int [10];
    		for (int i = 0; i<[B]randarray.length[/B]; i++)
    		{
    			if (randarray[i] <= 10) {
    				// The value is less than 10...
    				// You want to increase the number of 0..10s
    				// Therefore, increase the first element of histo (not the ith element)
    				histo[[B]0[/B]] = histo[[B]0[/B]] + 1;
    				
    			}
    			else if ( randarray[i] <= 20){
    				// Increase the second...
    				histo[[B]1[/B]] = histo[[B]1[/B]] + 1;
    			}
    			else if (randarray[i] <= 30){
    				// An alternative way to increase a variable
    				histo[[B]2[/B]] += 1;
    			}
    			else if ( randarray[i] <= 40){
    				// Another alternative way to increase a variable
    				histo[[B]3[/B]]++;
    			}
    			...

    Your next challenge is to print X number of stars for the number of elements in each histo. Best of luck :-)

  6. #6
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey turtle many thanks for your reply sorry i havent replied sooner my internet was down.. i have ammended my prgram with the advice given but still i am getting errors and is isnt compliling correctly.. ive come to an conclusion that switch statements arent my strong area lol but i will not give up until i understand them.. =D

    Java Code:
    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();
        
    	 
    		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 (int j = 0; i<randarray.length; i++)
    		{
    			if (randarray[i] <= 10) {
    				// The value is less than 10...
    				// You want to increase the number of 0..10s
    				// Therefore, increase the first element of histo (not the ith element)
    				histo[0] = histo[0] + 1;
    				
    			}
    			else if ( randarray[i] <= 20){
    				// Increase the second...
    				histo[1] = histo[1] + 1;
    			}
    			else if (randarray[i] <= 30){
    				// An alternative way to increase a variable
    				histo[2] += 1;
    			}
    			else if ( randarray[i] <= 40){
    				// Another alternative way to increase a variable
    				histo[3]++;
    			}
    			else if ( randarray[i] <= 50){
    				// Another alternative way to increase a variable
    				histo[4]++;
    			}
    			else if ( randarray[i] <= 60){
    				// Another alternative way to increase a variable
    				histo[5]++;
    			}
    			else if ( randarray[i] <= 70){
    				// Another alternative way to increase a variable
    				histo[6]++;
    			}
    			else if ( randarray[i] <= 80){
    				// Another alternative way to increase a variable
    				histo[7]++;
    			}
    				else if ( randarray[i] <= 90){
    				// Another alternative way to increase a variable
    				histo[8]++;
    			}
    			else if ( randarray[i] <= 100){
    				// Another alternative way to increase a variable
    				histo[9]++;
    			}
    		}
    			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 | "); 
    			}
    			
    	}

  7. #7
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Your program is not compiling because you have do not have matching curly braces. That is, you are missing a '}' for the class '{'.

    (However, in future it would be helpful if you paste the error message. Experience with error messages allows the developer to quickly identify problems)

    (Also correctly indenting helps to identify this sort of problem)

    Your switch statement is syntactically valid, but will not perform the desired task.
    To better understand switch statements review this tutorial: [unable to post link - first entry when search google for "java switch statement"]


    I would suggest writing a description of what this part of the program must do. Identify what variables you have as input, what output you must produce. Write the steps one instruction at a time - in words.

    Then read the instructions, checking that they meet your goal.

    Then write the instructions in programming code, this time checking to see if it does not meet your goal (People see what they expect - so be critical and look for problems).

    Best of luck.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    This is the one Turtle was trying to link.
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Your switch statement isn't completely wrong, you're just switching the wrong expression (randarray[i])
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    hey turtle many thanks for the reply.. ok so my program has to step by step..
    1- generate random numbers 1-100
    2- to generate it 80 times
    3- have to create a histogram using a switch statement
    4- for every random number in each part of histogram one '*' to be added to certain part of histogram..

    many thanks for the links and replys phhein and turtle

Similar Threads

  1. Hints on how to make a Java Class
    By luron31 in forum New To Java
    Replies: 11
    Last Post: 07-09-2009, 06:31 AM
  2. Great Demand For IT Professionals.
    By avularamesh4u in forum Jobs Discussion
    Replies: 2
    Last Post: 12-30-2008, 06:05 PM
  3. Hi, just found this great forum from Google.
    By paulachrist in forum Introductions
    Replies: 7
    Last Post: 07-27-2008, 10:47 AM
  4. A great doubt in Java Applet,will u solve it!!
    By anithababu in forum Java Applets
    Replies: 6
    Last Post: 01-27-2008, 02:20 PM
  5. great book for teenager to learn Java?
    By 21rouge in forum New To Java
    Replies: 8
    Last Post: 12-03-2007, 06:23 AM

Posting Permissions

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