Formatting This Magical Square - java

I am looking to format the output of this code so that the totals of the rows and columns show up next to/below said row/column. I am getting the numbers correct, but spaces and char counts seem to be too unpredictable to format it correctly for any size square. Here is the code i currently have.
public void printSquare()
{
for (int x = 0; x < square.length; x ++)
{
System.out.println();
for (int j = 0; j < square.length; j++)
{
System.out.print(" " + square[x][j]);
}
System.out.println(" | " + sumRow(x));
}
System.out.print(sumOtherDiag());
for (int x = 0; x < square.length; x ++)
{
System.out.print(" ^");
}
System.out.println(sumMainDiag());
for (int x = 0; x < square.length; x ++)
{
System.out.print(" " + sumCol(x));
}
}
And the output for this is..
8 1 6 | 15
3 5 7 | 15
4 9 2 | 15
15 ^ ^ ^15
15 15 15
Are there any strategies in order to more reliably format something like this?

Instead of
System.out.printf(" " + square[x][j]);
use
System.out.printf("%4d", square[x][j]); // 4 positions for right-aligned decimal
and so on for other similar statements.

Related

how to print pattern where start and end element is not same

How to print a pattern where i and j variable are not supposed to be printed for pattern rows? I try many times I change values without results.
int k = 1, p = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
p += k;
System.out.print(p + " ");
}
k++;
System.out.println();
}
The above code is the generated output:
1
3 5
8 11 14
18 22 26 30
35 40 45 50 55
The desired output:
1
3 5
5 8 11
7 11 15 19
9 14 19 24 29
p must be updated after each i loop, using the current value of k. The following output can be generated using this code:
int k = 0;
for (int i = 1; i <= 5; i++) {
int p = k++;
for (int j = 1; j <= i; j++) {
p += k;
System.out.print(p + " ");
}
System.out.println();
}
For better formatting, you can use
System.out.printf("%-4d", p);
I'd write it this way:
int numRows = 5;
for (int row = 1; row <= numRows; row++) {
for (int col = 1, p = (row*2)-1; col <= row; col++, p+=row) {
System.out.print(p + " ");
}
System.out.println();
}
The pattern I see is as follows:
The first Row is designated as Row #1 (not zero)
The starting number in each row is (Row * 2) - 1
Each Row has the same number of Columns as the current Row number
In each Row, the numbers are incremented by the current Row number

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

Mirrored Triangle

I am required to display a mirrored triangle like so:
0 1 2
0 1
0
But I am only able to get
0 1 2 3
1 2 3
2 3
3
I am unsure of what I am doing wrong and everything I've looked at only shows the star pattern, no number patterns. here is my code.
for (int i = 0; i <= size; i ++) {
for(int j = i; j <= size; j++) {
System.out.print(j + " ");
}
System.out.println(" ");
}
For each line, you have to print the leading spaces before printing the numbers. So you need two inner for loops, one for the spaces and one for the numbers:
for(int i = 0; i <= size; i++) {
// first print out the leading spaces
for(int j = 0; j < i; j++) {
System.out.print(" ");
}
// then print out the numbers
for(int j = 0; j <= size - i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}

How do I put spaces in between individual characters when creating the triangle using nested for loops?

My apologies if this individual question is a duplicate, but I have not seen any specific answers to this particular problem.
I am trying to put spaces in between each individual character in a triangle I created using nested for loops in the code below.
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (5 - i + 1); j++) {
System.out.print(j);
}
System.out.println();
}
This outputs:
1
21
321
4321
54321
12345
1234
123
12
1
However, it is required to be:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
I've tried putting " " on either as well as both sides of the print statement, as I did with the reverse of these two triangles, to no avail. Am I missing something?
You can also try something like this.
int spaces=10;
int z;
for (int i = 1; i <=5 ; i++) {
spaces=spaces-2;
for (int j = 0; j<=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else {
z=i;
while (z >= 1) {
System.out.print(z + " ");
z--;
}
}
}
System.out.println("\n");
}
System.out.println("\n");
spaces=-2;
int m;
for (int i = 1; i <=5 ; i++) {
spaces=spaces+2;
for (int j = 0; j <=spaces ; j++) {
if(j!=spaces)
System.out.print(" ");
else{
z=i;
m=1;
while (m<=6-z) {
System.out.print(m + " ");
m++;
}
}
}
System.out.println("\n");
}
Just change System.out.print(j); to System.out.print(j + " ");

Trouble with a java histogram issue

This is the question:
Design and implement an application that creates a histogram that allows you to
visually inspect the frequency distribution of a set of values. The program should read in
an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a
chart similar to the one below that indicates how many input values fell in the range 1
to 10, 11 to 20, and so on. Print one asterisk for each value entered.
1 - 10 | *****
11 - 20 | **
21 - 30 | *******************
31 - 40 |
41 - 50 | ***
51 - 60 | ********
61 - 70 | **
71 - 80 | *****
81 - 90 | *******
91 - 100 | *********
Source Code:
import java.io.*;
import java.util.Scanner;
public class histogram
{
public static void main(java.lang.String[] args)
throws IOException
{
Scanner scan = new Scanner (System.in);
final int min = 1;
final int max = 10;
final int limit = 10;
int[] a = new int[max];
for (int b = 0; b < a.length; b++)
{
a[b] = 0;
}
System.out.println("Enter Number between 1 and 100: \n (Or press 0 to stop)");
int number = scan.nextInt();
while (number >= min && number <= (limit*max) && number != 0)
{
a[(number-1)/limit] = a[(number - 1 ) / limit] + 1;
System.out.print("Please enter a value:");
number = scan.nextInt();
}
System.out.println("\n__Histogram__");
for (int y = 0; y < a.length; y++)
{
System.out.print(" " + (y * limit + 1) + " - " + (y + 1) * limit + "\t");
return;
}
for (int z = 0; z < a[b]; z++)
{
System.out.print("*");
}
System.out.println(0);
}
}
In the last for statement, it says that b cannot be resolved to a variable. When I use java for help it sets b to 0 and the program doesn't run correctly. Any ideas?
Since b was an array index in the earlier loop, I think you will need to put the last loop inside the loop to increment through the array indexes as you did in the beginning.
Changed b to y and moved z for loop inside y for loop.
for (int y = 0; y < a.length; y++)
{
System.out.print(" " + (y * limit + 1) + " - " + (y + 1) * limit + "\t");
for (int z = 0; z < a[y]; z++)
{
System.out.print("*");
}
System.out.println();
}

Categories

Resources