Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
Question : Write a Java program to read integer data set(size of n<10) into an array and display its data graphically by plotti...Asked by Arya
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();
}
}
}
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();
}
}
}
Answers
Answered by
oobleck
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();
}
for (int i=0; i <= 8; i++) {
for (int x = 0; x < array1[i]; x++) {
System.out.print("*");
}
System.out.println();
}
Answered by
Arya
That'll print out the stars according to the value, but how do we print this as a table as in the question?
Answered by
oobleck
oh, well, just print out i and array1[i] before the stars.
Answered by
Aryaa
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();
}
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();
}
Answered by
Arya
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?
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?
Answered by
oobleck
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 ***}
for each index from 0 to 8, {print the numbers, then the string of ***}
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.