Results 1 to 3 of 3
  1. #1
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default [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.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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

    Java 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;
    }
    }
    hope this is helpful

  3. #3
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    thanks marshy for ur work ..glad that u enjoy solving my prob :D..

    Could there be other way to solve it w/o using an array? I am doing the school work here and we aren't allowed to use array for this case ;(... i know beggar can't b chooser but I really need other way around...

Similar Threads

  1. histogram equalization
    By syarizma in forum New To Java
    Replies: 2
    Last Post: 08-14-2009, 03:03 AM
  2. Regarding the display of histogram
    By Mazharul in forum Java 2D
    Replies: 2
    Last Post: 08-27-2008, 03:09 AM
  3. Arrays and Histogram Help Needed
    By sebbybey in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 09:01 PM
  4. Arrays and Histogram Help Needed
    By sebbybey in forum New To Java
    Replies: 3
    Last Post: 08-15-2008, 09:00 PM
  5. How to create a Histogram
    By Devilsfutbol17 in forum New To Java
    Replies: 4
    Last Post: 06-04-2008, 09:22 PM

Posting Permissions

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