Results 1 to 9 of 9
- 11-18-2009, 11:57 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
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 | "); } } } }
- 11-18-2009, 12:58 PM #2
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
A couple of things...
Firstly you are confusing OR with AND
The AND symbol is &&
The OR symbol is ||
Example AND usage...
Example OR usage...Java Code:if (hungry && haveMoney) { buyFood() }
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 12:59 PM. Reason: "AND with OR" changed to "OR with AND". :-P
- 11-18-2009, 01:02 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
many thanks turtle will try and do it now will keep u posted =)
- 11-18-2009, 08:48 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
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]++; } } } }
- 11-18-2009, 11:03 PM #5
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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 :-)
- 11-24-2009, 12:59 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
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 | "); } }
- 11-24-2009, 01:23 PM #7
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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.
- 11-24-2009, 01:54 PM #8
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
- 11-24-2009, 09:03 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
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
-
Hints on how to make a Java Class
By luron31 in forum New To JavaReplies: 11Last Post: 07-09-2009, 05:31 AM -
Great Demand For IT Professionals.
By avularamesh4u in forum Jobs DiscussionReplies: 2Last Post: 12-30-2008, 05:05 PM -
Hi, just found this great forum from Google.
By paulachrist in forum IntroductionsReplies: 7Last Post: 07-27-2008, 09:47 AM -
A great doubt in Java Applet,will u solve it!!
By anithababu in forum Java AppletsReplies: 6Last Post: 01-27-2008, 01:20 PM -
great book for teenager to learn Java?
By 21rouge in forum New To JavaReplies: 8Last Post: 12-03-2007, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks