How to write an vertical Histogram - java

So, right now I managed to output the Histogram like this:
The code for this picture is as following:
int max = getBiggest(arr);
int min = getSmallest(arr);
int n = max / 50 + 1;
for (int i = 0; i < arr.length; i++) {
for (int j = min; j < arr[i]; j += n) {
System.out.print(i + 1);
}
System.out.println("(" + arr[i] + ")");
}
But my task is to output the Histogram like this:
Can someone explain me please how to write this code?

Try this.
public static void main(String[] args) {
int[] arr = {12, 28, 11, 16, 21, 12};
int length = arr.length;
int max = IntStream.of(arr).max().getAsInt();
int min = IntStream.of(arr).min().getAsInt();
System.out.println("Histgramm:");
for (int h = min; h <= max; ++h) {
for (int i = 0; i < length; i++)
System.out.print(arr[i] >= h ? (i + 1) + " " : " ");
System.out.println("(" + h + ")");
}
}
output:
Histgramm:
1 2 3 4 5 6 (11)
1 2 4 5 6 (12)
2 4 5 (13)
2 4 5 (14)
2 4 5 (15)
2 4 5 (16)
2 5 (17)
2 5 (18)
2 5 (19)
2 5 (20)
2 5 (21)
2 (22)
2 (23)
2 (24)
2 (25)
2 (26)
2 (27)
2 (28)

Related

Create a 2D array with first number being the number of rows

1.Create a square 2D array of a size that is decided by user input - the first number being the number of rows.
2.Allow the user to fill the array with integers of their choice.
3. Print to the screen the sum of the diagonals. For example see below
Example 2D Array:
| 1 2 3 4 |
| 5 4 6 9 |
| 11 13 16 6 |
| 2 4 18 20 |
Diagonal 1 = 1, 4, 16, 20. Diagonal 2 = 4, 6, 13, 2. The sum of Diagonal 1 is 41. (1 + 4 + 16 + 20) The sum of Diagonal 2 is 25. (4 + 6 + 13 + 2) The sum of both diagonals is 66. You would return 66, the sum of both diagonals only.
The code I have written is as follows but it is missing the following :
I know I need to fill the array somewhere - but not sure where ???
when I run the sum diagonals it doesn't let me fill array properly ??
import java.util.Scanner;
public class SumOfDiagonals{
public static void main(String [] args){
Scanner input = new Scanner (System.in);
int [][] matrix;
System.out.print ("Enter row number:");
int row = input.nextInt();
int col = row;
matrix = new int [row][col];
for (int i=0; i<matrix.length; i++){
for (int j = 0; i<matrix[0].length; j++){
System.out.print ("Enter a number for ( "+i+", "+j+")");
matrix[i][j]=input.nextInt();
}
}
int sum1=0;
int sum2=0;
for (int i=0;i<matrix.length;i++){
sum1 += matrix[i][i];
sum2 += matrix[i][matrix.length-1-i];
}
System.out.println ("Diagonal 1: "+sum1);
System.out.println ("Diagonal 2: "+sum2);
System.out.println ("Sum of two diagonals: " + (sum2+sum1));
}
}
My error when the test code is as follows:
Your program tested with:
5 5 4 3 2 1 1 2 3 7 5 1 1 1 1 1 11 12 13 14 15 15 14 13 12 11
Your answer:
Enter row number:Enter a number for ( 0, 0)
Enter a number for ( 0, 1)
Enter a number for ( 0, 2)
Enter a number for ( 0, 3)
Enter a number for ( 0, 4)
Enter a number for ( 0, 5)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at SumOfDiagonals.main(SumOfDiagonals.java:13)
Expected answer:
Sum of diagonals = 69
You got a grade of 52
You have a typo in your second for-loop.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; *HERE* i < matrix[0].length; j++) {
System.out.print("Enter a number for ( " + i + ", " + j + ")");
matrix[i][j] = input.nextInt();
}
}
should be
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print("Enter a number for ( " + i + ", " + j + ")");
matrix[i][j] = input.nextInt();
}
}

Can someone explain how this left rotating array code works?

I am trying to learn more about arrays and how to rotate them. I stumbled upon a code and I'm trying to figure out how it prints out the rotation correctly.
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int d = sc.nextInt();
int arr [] = new int [n];
for (int i = 0; i < n; i++)
{
arr [i] = sc.nextInt();
}
for (int i = 0; i < n; i++)
{
System.out.print (arr [(i + d) % n] + " ");
}
}
I have tried doing the math myself to see how it works, but it doesn't add up and I'm not sure what I'm doing wrong. The n is for the number for integers in the array and d is the number of rotations.
The equation is: (i + d) % n with n = 5, d = 4, and the array being {1,2,3,4,5}
(1 + 4) % 5 = 5 % 5 = 0
(2 + 4) % 5 = 6 % 5 = 1
(3 + 4) % 5 = 7 % 5 = 2
(4 + 4) % 5 = 8 % 5 = 3
(5 + 4) % 5 = 9 % 5 = 4
The answer that it prints out is 5 1 2 3 4 which is correct, but from what I calculated it should print out as 1 2 3 4 5. How I got this is that from the following calculations it says that 1 should go to index 0, 2 should go to index 1, etc... If you guys can help explain how the code works, that would much appreciated.
Here,
for (int i = 0; i < n; i++)
{
System.out.print (arr [(i + d) % n] + " ");
}
The array will start from 0 since in the for loop you gave i as 0. so it will start from 0 to 4.
(0 + 4) % 5 = 4 % 5 = 5
(1 + 4) % 5 = 5 % 5 = 1
(2 + 4) % 5 = 6 % 5 = 2
(3 + 4) % 5 = 7 % 5 = 3
(4 + 4) % 5 = 8 % 5 = 4
First, you collect arrays with the contents {1,2,3,4,5}, this means you have 5 indexes with details :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Then you want each index content to be added to the value d then modulus % with the value n.
What's wrong is in your second iteration ?
for (int i = 0; i < n; i++){
System.out.print (arr [(i + d) % n] + " ");
}
If you print this (i + d)% n, it will produce this :
(0 + 4) % 5 = 4
(1 + 4) % 5 = 0
(2 + 4) % 5 = 1
(3 + 4) % 5 = 2
(4 + 4) % 5 = 3
So with this command :
System.out.print (arr [(i + d) % n] + " ");
Actually you are printing :
arr[4]
arr[0]
arr[1]
arr[2]
arr[3]
So if you want the results that you want, change the second iteration by holding the value of each index of your array with the following code:
for (int i = 0; i < n; i++){
arr[i] = (arr[i] + d) % n;
System.out.print(arr[i] + " ");
}
This will print 0 1 2 3 4
Hope this helps

Cannot print pyramid of numbers

for (int i = 1; i <= 4; i++)
{
int n = 4;
for (int j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--)
{
int n = 3;
for (int j = 0; j <= n - i; j++)
{
System.out.print(" ");
}
for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
My output:
Enter height:
12
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
109876543212345678910
1110987654321234567891011
12111098765432123456789101112
It prints correctly, but with no spacing...
Would I need another for loop with a println that just prints a space under the first for loop?
Also, if I did that would it still work for double digit heights such as 12?
I would greatly appreciate help, thank you.
You just need to play around with spaces.
for (int i = 1; i <= height; i++) {
int n = height;
int n2 = height + 1;
for (int j = 1; j <= n - i; j++) {
System.out.print("" + String.format("%3s", " ") + " ");
}
for (int k = i; k >= 1; k--) {
System.out.print("" + String.format("%3s", k) + " ");
}
for (int l = 2; l <= i; l++) {
System.out.print("" + String.format("%3s", l) + " ");
}
System.out.println();
System.out.println();
}
OUTPUT: For height 10
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10

numeric pyramid in java.

i need to print out a numeric pyramid using java. that counts in multiples of 2 along with the spaces like how it is below. but the code i have it only bring up multiples of 1 of no spaces.
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
this is my code.
import java.util.Scanner;
public class NumericPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Taking noOfRows value from the user
System.out.println("How Many Rows You Want In Your Pyramid?");
int noOfRows = sc.nextInt();
//Initializing rowCount with 1
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = noOfRows; i > 0; i--) {
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++) {
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++) {
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--) {
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Output:
How Many Rows You Want In Your Pyramid?
7
Here Is Your Pyramid
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
For each j from 1 to n you need write 2^j. Currently you are writting only j.
So write function which for given k returns 2^k.
EDIT: For bigger n you need to use BigInteger:
import java.math.BigInteger;
public class NumericPyramid {
private static BigInteger pow(int exponent) {
BigInteger result = new BigInteger("1");
BigInteger two = new BigInteger("2");
for (int i = 0; i < exponent; i++) {
result = result.multiply(two);
}
return result;
}
and use it in both for loops. Replace
System.out.print(j+" ");
with
System.out.print(pow(j)+" ");
the pyramid goes 1 2 4 8 16, that's a sequence of 1 * 2 = 2, 2 * 2 = 4, 4 * 2 = 8 and so on, and then goes inverse, 8 / 2 = 4, 4 / 2 = 2, 2 / 2 = 1, you only need to multiply by 2 and divide by 2 when the center
for (int i = 0; i < noOfRows; i++) {
int cont = 1;
for (int j = 0; j <= i; j++) {
System.out.print(cont + " ");
cont = cont * 2;
}
cont = cont / 2;
for (int j = 0; j < i; j++) {
cont = cont / 2;
System.out.print(cont + " ");
}
System.out.println();
}

Can someone explain how Java nested loops work with number patterns?

I am having trouble understanding how nested loops and numbers patterns work. I have completed all of the patterns except 3. Can someone please help me with this code and explain how this works?
public class Patterns7{
public Patterns7() {
}
public void displayPatternI(int lines)
{
System.out.println("\n\tPattern I\n");
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= i; j++)
System.out.print (j + " " );
System.out.println();
}
}
public void displayPatternII (int lines)
{
System.out.println("\n\tPattern II to be implemented\n");
for (int i = 1;i <= lines; i++)
{
for(int j = i;j >= 1; j--)
System.out.print(j);
System.out.println();
}
System.out.println();
}
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = 1; i <= lines; i++)
{
for (int space = 1; space <= lines-i; space++)
System.out.print (" ");
for (int j = 1; j <= i; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
}
Pattern III is supposed to look like this:
6
56
456
3456
23456
123456
But all I could get it to do was this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
I am unsure of how to get the out put to start with 6 and decrease then increase.
Pattern V is supposed to look like this:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
But it comes out like this:
1
1 2 3
1 2 3 4 5
Code:
public void displayPatternVI (int lines)
{
System.out.println("\n\tMy Own Pattern to be implemented\n");
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= (lines/2)-i; space++)
System.out.print (" ");
for (int j = 1; j <= (i*2)-1; j++)
System.out.print (j + " ");
System.out.println();
}
for (int i = 1; i <= lines/2; i++)
{
for (int space = 1; space <= i-1; space++)
System.out.print (" ");
for (int j = 1; j <= lines-(i*2)+1; j++)
System.out.print (j + " ");
System.out.println();
}
System.out.println();
}
Pattern VI is supposed to look like this:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
6 5 4 3 2 1 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
but it looks like this:
1
1 2 3
1 2 3 4 5
1 2 3 4 5
1 2 3
1
Can someone please help and explain to me how to this?
You're really only supposed to ask one question at a time, but here's a solution to your first issue. For SIZE equals 6:
6
56
456
3456
23456
123456
you would want something like:
String temp;
for(int i = 0; i < SIZE; i++)
{
temp = "";
for(int j = SIZE - i; j <= SIZE ; j++)
{
temp += j;
}
System.out.printf("%" + SIZE + "s\n", temp);
}
Try the second one on your own.
public void displayPatternIII (int lines)
{
System.out.println("\n\tPattern III to be implemented\n");
for (int i = lines; i >= 1; i--)
{
int space = 1;
for (; space <= i-1; space++)
System.out.print (" ");
for (int j = space; j <= lines; j++)
System.out.print (j);
System.out.println();
}
}
Output:
Pattern III to be implemented
6
56
456
3456
23456
123456

Categories

Resources