Results 1 to 3 of 3
Thread: simple histogram
- 03-06-2010, 03:10 PM #1
[Solved] simple histogram
Hi , I would like to construct the simple histogram with symbols (e.g #or % or *).. based on the user integer input.. (input: 4 2 3), and i would like to display in this way.
+
+ +
+ + +
+ + + <---- histogram displayed with symbol
4 2 3 <---inputs (the number of input is fixed in this case)
Can you help me in how to display those symbols side by side.(in a histogram format), I am stuck when i tried to combine all the symbols together. My codes just display them in 1 vertical line instead of displaying on multiple vertical lines. below is my code. Thanks in advance.
Java Code:import java.util.Scanner; public class histogram { public static void main(String[] args) { int num1,num2,num3,num4; Scanner sc = new Scanner (System.in); System.out.print("Enter thhe numbers: "); num1 = sc.nextInt(); String ss1 = genPattern(num1); num2 = sc.nextInt(); String ss2 = genPattern(num2); num3 = sc.nextInt(); String ss3 = genPattern(num3); num4 = sc.nextInt(); String ss4 = genPattern(num4); System.out.println(ss1 + ss2 + ss3 + ss4); } public static String genPattern(int number){ String msg= ""; String as = "+"; for (int i= 1; i<=number; i++){ msg=msg+ as + "\n" ; } return msg; } }Last edited by heartysnowy; 03-08-2010 at 09:29 AM.
- 03-06-2010, 06:01 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I am sure there are more elegant solutions to this but here is my solution. By the way I really enjoyed figuring it out :D
hope this is helpfulJava Code:import java.util.Scanner; import java.lang.Math; public class Histogram { public static void main(String[] args) { int num1,num2,num3,num4; //rows will be determined by the highest number entered int rows; Scanner sc = new Scanner (System.in); System.out.print("Enter thhe numbers: "); num1 = sc.nextInt(); rows=num1; String ss1 = genPattern(num1); num2 = sc.nextInt(); rows=Math.max(num1, num2); String ss2 = genPattern(num2); num3 = sc.nextInt(); if(rows<num3) { rows=num3; } String ss3 = genPattern(num3); num4 = sc.nextInt(); if(rows<num4) { rows=num4; } String ss4 = genPattern(num4); System.out.println("Rows: "+rows); //use 2d character array to store the pattern Character display[][]=new Character [rows][4]; int ss1Index=0,ss2Index=0,ss3Index=0,ss4Index=0; //get ss1 pattern and let column 0 of display hold its contents for(int i=rows-1;i>=0;i--) { if(ss1Index<ss1.length()){ display[i][0]=ss1.charAt(ss1Index); ss1Index++; } else{ display[i][0]=' '; } } //get ss2 pattern and let column 1 of diplay hold its contents for(int i=rows-1;i>=0;i--) { if(ss2Index<ss2.length()){ display[i][1]=ss2.charAt(ss2Index); ss2Index++; } else{ display[i][1]=' '; } } //get ss3 pattern and let column 2 of diplay hold its contents for(int i=rows-1;i>=0;i--) { if(ss3Index<ss3.length()){ display[i][2]=ss3.charAt(ss3Index); ss3Index++; } else{ display[i][2]=' '; } } //get ss4 pattern and let column 3 of diplay hold its contents for(int i=rows-1;i>=0;i--) { if(ss4Index<ss4.length()){ display[i][3]=ss4.charAt(ss4Index); ss4Index++; } else{ display[i][3]=' '; } } //display the histogram for(int i=0;i<rows;i++) { for(int j=0;j<4;j++) { if(display[i][j]==null) { System.out.print(" "); }else{ System.out.print(display[i][j]); } } System.out.println(); } } public static String genPattern(int number){ String msg= ""; String as = "+"; for (int i= 1; i<=number; i++){ msg=msg+ as ; } return msg; } }
- 03-07-2010, 07:34 AM #3
Similar Threads
-
histogram equalization
By syarizma in forum New To JavaReplies: 2Last Post: 08-14-2009, 03:03 AM -
Regarding the display of histogram
By Mazharul in forum Java 2DReplies: 2Last Post: 08-27-2008, 03:09 AM -
Arrays and Histogram Help Needed
By sebbybey in forum New To JavaReplies: 4Last Post: 08-15-2008, 09:01 PM -
Arrays and Histogram Help Needed
By sebbybey in forum New To JavaReplies: 3Last Post: 08-15-2008, 09:00 PM -
How to create a Histogram
By Devilsfutbol17 in forum New To JavaReplies: 4Last Post: 06-04-2008, 09:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks