Ashley P.

asked • 10/12/19

Java - arrays & loops

Question : Write a Java program to read integer data set(size of n<10) into an array and display its data graphically by plotting each numeric value as a bar of asterisks as shown in the following table.


Element Value Histogram

0 5 *****

1 11 ***********

2 4 ****

3 1 *

4 7 *******

5 10 **********

6 0

7 9 *********

8 12 ************



My work :


My first thought was to do this using a for loop : accessing one element of the array at a time and then printing stars according to the value;


Below is the code, but I don't know how to get the no. of stars printed according the value . Any help on this is highly appreciated. Thanks!


class arrays{


public static void main(String[] args) {

int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12};

int i = 0;

for (int x = array1[i]; i <= 8; i++) {

System.out.print("*");

System.out.println();

}

}

}

1

1 Expert Answer

By:

Suzanne O. answered • 10/12/19

International Experience and Multiple State Certifications

Ashley P.

Thanks a lot for the response. But still I don't get how to get the stars printed according to the value. Below is my next attempt. Would you mind clarifying this please? class arrays { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; int i = 0; for (int k = 0; k <= 9; k++) { for (int x = array1[i]; i <= 9; i++) { System.out.println(array1[i]); // for (int j = array1[i]; i <= 9; i++) { System.out.print("*"); } System.out.println(); } } } }
Report

10/14/19

Ashley P.

class arrays { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; int i = 0; for (int k = 0; k <= 9; k++) { for (int x = array1[i]; i <= 9; i++) { System.out.println(array1[i]); // for (int j = array1[i]; i <= 9; i++) { System.out.print("*"); } System.out.println(); } } } }
Report

10/14/19

Ashley P.

Unfortunately, the code doesn't follow the formatting here. Sorry for the inconveniences caused!
Report

10/14/19

Ashley P.

I was able to print the first two columns as it is in the question, but not the stars. class Arrays2 { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; for (int j = 0; j <= 8; j++) { System.out.println(j+" "+array1[j]); } for (int i = 0; i <= 8; i++) { for (int x = 0; x < array1[i]; x++) { System.out.print("*"); } System.out.println(); } } } Output : 0 5 1 11 2 4 3 1 4 7 5 10 6 0 7 9 8 12 ***** *********** **** * ******* ********** ********* ************ Could you think of a way to print the stars next to each value? Thanks!
Report

10/14/19

Suzanne O.

tutor
I see a few things. Y ou are using system.out.println(j+" "+array1[j]);, but it should be a .print (stays on the same line), not a .println (has a line return in it). That fix alone should clear things up. but I would suggest for a bit more efficiency in the program that you move the star-printing for loop inside you first loop, so that you have a double loop and not a single then a double loop. Code would look similar to this: class Arrays2 { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; for (int j = 0; j <= 8; j++) { system.out.print(j+" "+array1[j])+" "; //.print, not .println for (int x = 1; x <= array1[j] x++) { system.out.print("*"); } system.out.println(); } }
Report

10/14/19

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.