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();
}

}
}

How about something like this? You are not using x at all.

for (int i=0; i <= 8; i++) {
for (int x = 0; x < array1[i]; x++) {
System.out.print("*");
}
System.out.println();
}

That'll print out the stars according to the value, but how do we print this as a table as in the question?

oh, well, just print out i and array1[i] before the stars.

Like this??

for (int i=0; i <= 8; i++) {
System.out.print(i);
System.out.print(array1[i]);
r (int x = 0; x < array1[i]; x++) {
System.out.print("*");
}
System.out.println();
}

I tried it this way:

Code :

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
*****
***********
****
*
*******
**********

*********
************

How can I also get the stars to be printed beside its value?

come on -- you need to print the numbers inside the i loop.

for each index from 0 to 8, {print the numbers, then the string of ***}

To solve this problem, you can iterate over each element of the array and print a number of asterisks ("*") equal to the value of the element. Here is an updated version of your code that accomplishes this:

```java
class Arrays {

public static void main(String[] args) {

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

for (int i = 0; i < array1.length; i++) {
System.out.print(i + " " + array1[i] + " ");
for (int j = 0; j < array1[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
```

Explanation:
- First, create an array called `array1` with the given values.
- Then, use a for loop to iterate over each element of `array1`.
- Inside the loop, print the index of the element (`i`), followed by a space, the value of the element (`array1[i]`), and another space.
- Use another nested for loop to print a number of asterisks (`*`) equal to the value of the element.
- Finally, print a newline character (`System.out.println()`) to move to the next line for the next element.

This will produce the desired output where each value in the array is represented by a bar of asterisks.