Description: This program prints the following pattern(Vertical Histogram):
if the array is of elements -|3|7|2|5|
*
*
* *
* *
** *
if the array is of elements -|3|7|2|5|
*
*
* *
* *
** *
****
****
according to the values entered in the array
****
according to the values entered in the array
Primary Inputs: Read 10 digits from the user
Primary Output: The pattern specified above
Platform Used: JDK 1.6 with JCreator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** *Primary Input: n numbers as elements of an array. n is also user specified *Primary Output: The vertical histogram. Defined as: * *Let four values are entered in the array as: 3 7 2 5 *the corresponding vertical histogram would be: * * * * * * * * * * * ** * * **** * **** * *i.e. number of stars in each column=value in the array at corresponding column */ import java.io.*; //for Console class. class VerticalHistogram { public static void main(String[] args) { int Element_Count = 0; // To hold the count of number of values in the // array. To be entered by the user int i; // As loop count int max = 0; // To hold the maximum amongst the values entered in the // array Console con = System.console(); // For reading input from the console. // Will work in JDK 1.6 only // prompt for entering the number of values in the array System.out.print("Enter the number of values to be entered:"); Element_Count = Integer.parseInt(con.readLine()); // read the line from // the console and // convert to // integer int arr[] = new int[Element_Count]; // array to hold the numbers. // Dimension is as entered by the // user // print the message for prompting user to enter input System.out.println("Enter the " + Element_Count + " values for the array"); // loop for getting the input and finding the maximum value out of them for (i = 0; i < Element_Count; i++) { arr[i] = Integer.parseInt(con.readLine()); if (arr[i] > max) max = arr[i]; } // print the array back System.out.println("\nThe array you entered is:"); for (i = 0; i < Element_Count; i++) System.out.print(arr[i]); System.out.println(); // actual logic for printing the vertical histogram starts from here for (i = 0; i < max; i++) { for (int j = 0; j < Element_Count; j++) { if (max - arr[j] <= i) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } // end of main } |