I am trying to write a code that fills in the 2 main diagonals in an NxN matrix, for example:
if N=5 (which is entered through command line), we would have a 5x5 matrix filled with zeros and the diagonals would have 2s filled in, like:
2 0 0 0 2
0 2 0 2 0
0 0 2 0 0
0 2 0 2 0
2 0 0 0 2
I wrote a code for an all-zero table, but i can't figure our how to fill in the diagonals.
Looking at the case of 5x5 i would have to fill in the matrix at the following indices:
#1 (0,0) (0,n-1)
#2 (1,1) (1,n-2)
#3 (2,2) (2,n-3)
#4 (3,1) (3, n-2)
#5 (4,0) (4,n-1)
However, since N can be any number, i assume that first i have to find the middle row, after which i have to decrement the indices in the reverse order.
I am learning Java for 2 weeks only and this one is pretty hard.
My code for a zero-filled table is this:
public static void main (String[] args){
int n = Integer.parseInt(args[0]);
System.out.println(n);
int[][] table = new int[n][];
for (int i = 0; i < n; i++) {
table[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
table[i][j] = (0);
}
} System.out.print(Arrays.deepToString(table));
}
Obviously, this is very far from what i need to achieve, and i am not sure if it's entirely right. I would really appreciate some help.
Try this,
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args){
int n = 5;
System.out.println(n);
int[][] table = new int[n][];
for (int i = 0; i < n; i++) {
table[i] = new int[n];
for (int j = 0; j < n; j++) {
if(i==j || n-i == j+1){
table[i][j] = table[i][n-i-1] = 2;
}
else{
table[i][j] = 0;
}
System.out.print(table[i][j]);
}
System.out.println();
}
}
}
To fill one diagonal, we just have to count from i==0 to i==size-1 and fill in (i,i) each time.
for(int i=0; i<size; i++) {
table[i][i] = 2;
}
The other diagonal is only slightly harder:
for(int i=0; i<size; i++) {
table[i][some calculation involving i and size] = 2;
}
You should be able to work out what the calculation is.
You can do it in two loops - but you can also combine filling both diagonals into one loop.
You could also amend your nested loops that create the array, to handle the cells on the diagonals as you encounter them there.
#inside the loop
if( a condition indicating that the cell is on a diagonal) {
table[i][j] = 2;
} else {
table[i][j] = 0;
}
Write down the coordinates of the cells on the diagonals, and you should quickly see what the condition is.
Related
A brief explanation: With the code below it will make a randomly generated Square and some code below would make sure that it was a Magic Square, in which the sum of the elements in each row, column, and the two diagonals are the same value.
My teacher said at maximum it should take three minutes to generate a magic square. So all I ask is there anything that can be done to improve or fix this code, please?
import java.util.ArrayList;
import java.util.Random;
class Main {
public static void main(String[] args) {
int size = 9;
int N = 3;
boolean result = true;
ArrayList<Integer> list = new ArrayList<Integer>(size);
int[][] mat = new int[N][N];
while (result) {
for (int i = 1; i <= size; i++) {
list.add(i);
}
Random rand = new Random();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int index = rand.nextInt(list.size());
System.out.print(list.remove(index)+" ");
}
System.out.println();
}
System.out.println();
// Checking process
// sumd1 and sumd2 are the sum of the two diagonals
int sumd1 = 0, sumd2 = 0;
for (int i = 0; i < N; i++) {
// (i, i) is the diagonal from top-left -> bottom-right
// (i, N - i - 1) is the diagonal from top-right -> bottom-left
sumd1 += mat[i][i];
sumd2 += mat[i][N - 1 - i];
}
// if the two diagonal sums are unequal then it is not a magic square
if (sumd1 != sumd2)
result = false;
// calculating sums of Rows and columns and checking if they are equal to each other,as well as equal to diagonal sum or not
for (int i = 0; i < N; i++) {
int rowSum = 0, colSum = 0;
for (int j = 0; j < N; j++) {
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
result=false;
}
result = true;
}
}
}
Trying with random numbers to coincidentally find a solution is deadly slow.
If you have the numbers 1 to 9, its entire sum 1+2+3+...+8+9 is 9*(1+9)/2 = 45.
As you have 3 rows and 3 colums, a row and column must sum upto 45/3 = 15.
Now that should restrict the number of possibilities.
So you must build in some intelligence in the code. Avoid random numbers as they do not even guarantee you'll find a solution in hundred years.
If you already treated recursion, that would be the easiest way to try all possibly valid combinations.
If you already treated Set, a BitSet maybe might be useful for a row, column or diagonal.
If you find it hard to code walking through all possibilities, you might hold the 2 dimensional matrix in a 1 dimensional array int[N*N], and have N (rows) + N (columns) + 2 (diagonals) arrays of N indices.
And of course I will not spoil your fun and satisfaction finding a smart solution.
Work it out on paper first.
The task is following:
a square matrix A of order M is given. Starting with the element A0,0 and moving clockwise, you should output all its elements in a spiral: the first row, the last column, the last row in reverse order, the first column in reverse order, the remaining elements of the second row and so on.
public class Pres10Task8 {
public static void main(String[] args) {
int m =4;
int [][] a=new int [m][m];
Random rand = new Random();
for(int i =0;i<a.length;i++){
for(int j =0;j<a[i].length;j++){
a[i][j]=rand.nextInt(100);
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(int k=0;k<m/2+1;k++){
for(int j = k; j<m+1-k;j++){
System.out.println(a[k][j]);
}
int j =m+1-k;
for(int i=k+1;i<m+1-k;i++){
System.out.println(a[i][j]);
}
for(int j=m-k;j>k;j--){
j =k ;
System.out.println(a[i][j]);
}
for(int i =m-k;i>k+1;i-- ){
i =m+1-k;
System.out.println(a[i][j]);
}
}
}
}
Would you be so kind to look through my code and say what is wrong with it? How should I rewrite my code in order to get the right output?
You couldn't use a variable in your loop already exist before your method :
int j = m + 1 - k;
// ^--------------------------------------Already exist
for (int i = k + 1; i < m + 1 - k; i++) {
System.out.println(a[i][j]);
}
for (int j = m - k; j > k; j--) {
// ^---------You can't declare a variable already exist,
//you can just use it or initialize it
So instead use this without int j:
for (j = m - k; j > k; j--) {
Second
System.out.println(a[i][j]);
// ^-----The i is not exist so you have to
//create it and inisialize it so you can use it
You have made many mistakes as YCF_L mentioned in the answer and also After solving few of them I was still getting other errors. You can solve this problem by a simple logic that Divide Square Matrix into smaller squares and then follow the same pattern to print each of the squares.
For Example,
Suppose I have a matrix of order 5 then we can have 3 squares here for distinct dimensions.
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
Here each number represents that to which square the element belongs.So all outer elements belongs to first square then the next layer's elements belongs to second square and so on.
Now you have made your problem easier by dividing it into smaller sub-problems which can be solved in same way. Now you need to make logic of print the elements of each of the squares.
So for this, Consider the most outer square.
1 1 1 1 1 1 2 3 4 5
1 1 Thier Printing Order 16 6
1 1 ================> 15 7
1 1 14 8
1 1 1 1 1 13 12 11 10 9
So notice that we start from the first element and move into the same row into the right direction.
Then move in same column into down direction.
Then again in the same row in left direction.
At last in the same column but in the up direction.
After printing the outer square move to the inner square and do the same for each of the squares.
Code for the same:
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int m =4;
int [][] a=new int [m][m];
Random rand = new Random();
for(int i =0;i<a.length;i++){
for(int j =0;j<a[i].length;j++){
a[i][j]=rand.nextInt(100);
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int squares=m/2; //Calculating total number of squares
for(int i=0;i<squares;i++)
{
int low=i; //Set the dimension of the square
int high=m-i-1;
for(int j=low;j<=high;j++) //First Row --> (Right Direction)
System.out.println(a[low][j]);
for(int j=low+1;j<=high;j++) //Last Column --> (Down Direction)
System.out.println(a[j][high]);
for(int j=high-1;j>=low;j--) //Last Row --> (Left Direction)
System.out.println(a[high][j]);
for(int j=high-1;j>low;j--) //First Column --> (Up Direction)
System.out.println(a[j][low]);
}
if(m%2==1) //If Matrix is of odd order then print the middle element.
System.out.println(a[mid][mid]);
}
}
Why you use jagged array [][] instead of 2-dimensional [,] ???
May give a simple sample:
public void CountDiag(int size)
{
// initialize straight order
int[,] ar2 = new int[size, size];
int count = 0;
// initialize spiral way
int y = 0;
int x = 0;
int top = 0;
int bot = ar2.GetLength(0)-1;
int left = 0;
int right = ar2.GetLength(1)-1;
do
{
//topleft to right
for (; y < right; y++)
{
ar2[x, y] = count + 1;
count++;
}
ar2[x, y] = count + 1;
right--;
//topright to bottom
for (; x < bot; x++)
{
ar2[x, y] = count + 1;
count++;
}
ar2[x, y] = count + 1;
top++;
//botright to left
for (; y > left; y--)
{
ar2[x, y] = count + 1;
count++;
}
ar2[x, y] = count + 1;
left++;
//botleft to top
for (; x > top; x--)
{
ar2[x, y] = count + 1;
count++;
}
ar2[x, y] = count + 1;
bot--;
} while (count < ar2.Length-1);
}
AND you print it out like this:
public void PrintArray(int[,] array)
{
int n = (array.GetLength(0) * array.GetLength(1) - 1).ToString().Length + 1; // for padding
for (int i = 0; i < array.GetLength(0); i++) // 0 - length rows
{
for (int j = 0; j < array.GetLength(1); j++) // 1 length columns
{
Console.Write(array[i, j].ToString().PadLeft(n, ' '));
}
Console.WriteLine();
}
Console.ReadLine();
}
I am trying to make a border around an empty matrix that is ascending and descending. I already have the top and left side which are ascending from 0 to 4, but I cannot get the right side and bottom side to go from 4 to 0. Here is the code of my for loops. I have tried about every variation to get it to work, but it either gives me an index out of bounds exception or just shows up as zeroes instead. I would greatly appreciate any feedback.
private static void fillBorder( int[][] matrix )
{
for (int r=0; r<matrix.length; ++r) // left side
{
matrix[r][0] = r;
}
for (int j=0 ; j<matrix.length; ++j) // top
{
matrix[0][j] = j;
}
for (int m=matrix.length; m>0; --m) // bottom
{
matrix[4][m] = m;
}
for (int s=matrix.length; s>0; --s) // right side
{
matrix[s][4] = s;
}
}
Here is also a picture of what the output is supposed to look like to give you a better idea of what it's supposed to be. picture of output
I think you can do this with a single for loop:
for (int i=0; i < matrix.length; ++i) {
matrix[0][i] = i;
matrix[i][0] = i;
matrix[matrix.length-1][matrix.length-1-i] = i;
matrix[matrix.length-1-i][matrix.length-1] = i;
}
This assumes that the matrix in fact is square (i.e. has the same number of rows and columns). If not, then my code would have to change, but then again so would your problem statement.
for (int r=0; r < matrix.length; ++r) {
for (int c=0; c < matrix.length; ++c) {
System.out.print(matrix[r][c] + " ");
}
System.out.println();
}
Output:
0 1 2 3 4
1 0 0 0 3
2 0 0 0 2
3 0 0 0 1
4 3 2 1 0
For your rows, you should be using matrix[0].length. The other thing is you should use the matrix dimension rather than hardcoding 4 into the code
private static void fillBorder( int[][] matrix )
{
for (int r=0; r<matrix.length; ++r) // left side
{
matrix[r][0] = r;
}
for (int j=0 ; j<matrix[0].length; ++j) // top
{
matrix[0][j] = j;
}
for (int m=matrix[0].length; m>0; --m) // bottom
{
matrix[matrix.length - 1][m] = m;
}
for (int s=matrix.length; s>0; --s) // right side
{
matrix[s][matrix[0].length - 1] = s;
}
}
This question relates to my 2D array display, which currently looks something like this.
A B C D
1: 0 0 0 0
2: 0 0 0 0
3: 0 0 0 0
I am trying to get location (0,0) to change to the number 1 as this will be the start of my count.
However it won't change and remains as a zero, here is my code.
int[][] chessBoard = new int[3][4];
int rowhead = 1;
TextIO.put(" ");
for (int col = 0; col < chessBoard[0].length; col++)
TextIO.putf("%4s",((char) ('A' + col)));
TextIO.putln();
for (int [] row:chessBoard){
TextIO.put(rowhead++ + ":");
for (int griddisplay:row)
TextIO.putf("%4d", griddisplay);
TextIO.putln();
chessBoard [0][0] = 1;
Now this keeps my coordinates (o, o) displaying a zero, however if I change this
chessBoard [0][0] = 1;
to this
chessBoard [1][0] = 1;
Then the grid does change accordingly to
A B C D
1: 0 0 0 0
2: 1 0 0 0
3: 0 0 0 0
Where am I going wrong?
Your code works fine, I just added a few methods and altered output
public class Chessboard
{
public static void main(String[] args)
{
int[][] chessBoard = new int[3][4];
print(chessBoard);
chessBoard [0][0] = 1;
print(chessBoard);
chessBoard [1][0] = 1;
print(chessBoard);
clear(chessBoard);
print(chessBoard);
}
public static void print(int[][] chessBoard)
{
int rowhead = 1;
System.out.print("\n ");
for (int col = 0; col < chessBoard[0].length; col++)
System.out.printf("%4s",((char) ('A' + col)));
System.out.println();
for (int[] row : chessBoard)
{
System.out.print(rowhead++ + ":");
for (int griddisplay : row)
System.out.printf("%4d", griddisplay);
System.out.println();
}
System.out.println();
}
public static void clear(int[][] chessBoard)
{
for (int row = 0; row < chessBoard.length; row++)
for(int col = 0; col < chessBoard[row].length; col++)
chessBoard[row][col] = 0;
}
}
Move your chessBoard [0][0] = 1; right below the TextIO.put(rowhead++ + ":");:
int[][] chessBoard = new int[3][4];
int rowhead = 1;
TextIO.put(" ");
for (int col = 0; col < chessBoard[0].length; col++)
TextIO.putf("%4s",((char) ('A' + col)));
TextIO.putln();
for (int [] row:chessBoard) {
TextIO.put(rowhead++ + ":");
chessBoard[2][3] = 1;
for (int griddisplay : row){
TextIO.putf("%4d", griddisplay);
}
TextIO.putln();
}
and it will work propertly =).
You appear to have some formatting issues but from the gist of it:
You are constructing your chess board as an array [3][4] and by the looks of it that is 3 'rows' and 4 'columns' so your first index is your row number and second is column number.
You loop through the first 'row' to get the length (4) so are outputting 4 columns - correctly I am assuming.
You then move to printing the chessboard (though I don't see the matching curly brace }). First you loop across rows and then across columns.
For example:
for (int [] row:chessBoard) {
TextIO.put(rowhead++ + ":");
for (int griddisplay:row)
TextIO.putf("%4d", griddisplay);
TextIO.putln();
chessBoard [0][0] = 1;
}
If this is your code, the first loop starts processing the first row. It loops through the columns for the first row, THEN sets [0][0] to be 1... but you've already printed that so it won't be shown. If you replace it with [1][0], it actually sets that BEFORE printing the second row so correctly shows the value.
As a final tip, the curly braces specify the scope of the for loop. If you omit the braces, the loop only runs the statement immediately after it. A habit a lot of programmers get in to is to always explicitly use braces to avoid easy mistakes.
I happened to appear for a test and got the following as question. I am unable to figure out how to proceed. The scenario is to write a java program that prints the following with respective N. If suppose N=3, it must have 2*N rows and output must be,
1
2*3
4*5*6
4*5*6
2*3
1
Output must consist only numbers and asterisk. N varies between 0 to 100. Also, given
public static void main(String[] args){
int rows=2;
mirrorTriangle(rows);
}
public void mirrorTriangle(int n){
//Logic
}
I don't understand why is that rows declared as 2 if rows are supposed to be varying with N. Please explain the logic.
Please find the solution to your problem, with explanation comments.
public static void main(String[] args) throws Exception
{
// initialize n
int n = 4;
// initialize x to 1 from where our printing will start.
int x = 1;
/* We will store our generated numbers in an array.
* For example, the array after we generate
* the numbers would look like:
* [1,0,0,
2,3,0,
4,5,6,
4,5,6,
2,3,0,
1,0,0]
*
* When n = 3, there are going to be 3*2 i.e, n*2 rows.
* in our case 6 rows.
* visualize with the above values.
* The first n/2 rows will be the numbers we print,
* the next n/2 will be the mirror image of the first n/2 rows.
* no. of columns in each row will be equal to n, in our example:3
*/
int arr[][] = new int[n*2][n];
/*
* Start populating the matrix
* Each row will contain number of elements eaual to the row number,
* so 1st row -> 1 element, 2nd - > 2,.. and so on.
*/
for(int row=0;row<n;row++)
{
int col = 0;
while(col < row+1)
{
arr[row][col] = arr[n*2-row-1][col] = x++;
col++;
}
}
/*
* Now our task is just to read out the array.
* The tricky part is adding the astricks.
* We notice that row1 will have 1-1 asticks, row2 -> 2-1 astricks ,.. and so on.
* So in between the numbers while reading out,
* for each row we maintain the number of astricks.
*/
for(int i=0;i<arr.length;i++)
{
StringBuilder build = new StringBuilder();
for(int j=0;j<arr[i].length;j++)
{
if(arr[i][j] > 0)
{
build.append((arr[i][j])).append("*");
}
}
System.out.print(build.delete(build.length()-1,build.length()).toString());
System.out.println();
}
}
o:p for n=4:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
def N = 3
def i = 0
def j = 0
int[][] numbers = new int[N][]
// Generate, print, and store numbers
while( i < numbers.length ){
numbers[i] = new int[i+1]
j = 0
while( j < numbers[i].length ){
numbers[i][j] = j+1
++j
print j
}
println ""
i++
}
// Print them again, in reverse order
i = numbers.length - 1
while( i >= 0 ){
j = 0
while( j < numbers[i].length ){
print numbers[i][j]
j++
}
println ""
i--
}
Output:
1
12
123
123
12
1
The code is pretty self-explanatory. You need just N rows but print 2N because, wait for it ... symmetry. If you have 6 rows, first 3 are new while the other 3 are just mirrored images so why waste the memory space when you can just print them again?
Is there an explicit requirement for recursion? It is implied by the structure of the problem not mentioned anywhere.
int rows=2 is an example probably, for the purposes of the problem you can't do anything 'smart' like using pointers ...
I will also assume that you are not permitted to use values '> 100' so that you can overload the meaning of the n value - same goes for 2's complement.
If you allow for looping, as a substitute for recursion you can generate the triangle without having to save state outside of the stack:
public static void main(String[] args){
int rows=3;
mirrorTriangle(rows);
}
public static void mirrorTriangle(int n){
for (int i = 0 ; i < n + 1 ; i++) {
renderLine(i);
}
for (int i = n ; i > 0 ; i--) {
renderLine(i);
}
}
private static void renderLine(int n) {
int j = n * (n - 1) / 2 + 1;
int k = j + n;
while (j < k) {
System.out.print(j);
j++;
if (j < k) System.out.print('*');
}
System.out.println();
}
Try this fresh code:
public class String4 {
public static void main(String[] args) {
int rows = 3;
mirrorTriangle(rows);
}
private static void mirrorTriangle(int rows) {
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i);
if(j>0&&j<i)
System.out.print("*");
}
System.out.println();
}
for(int k=rows;k>0;k--)
{
for(int l=1;l<=k;l++)
{
System.out.print(k);
if(l>0&&l<k)
System.out.print("*");
}
System.out.println();
}
}
}
Output:
1
2*2
3*3*3
3*3*3
2*2
1
I think this is a better and simple solution than the chosen one.
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter limit");
int limit = s.nextInt();
int start[] = new int[limit];
int v = 1;
for (int i=1; i<=limit; i++) {
start[i-1] = v;
for (int j=1; j<=i; j++) {
System.out.print(v++);
if(j==i)
continue;
System.out.print("*");
}
System.out.print("\n");
}
for (int i=limit-1; i>=0; i--) {
v=start[i];
for (int j=i; j>=0; j--) {
System.out.print(v++);
if(j==0)
continue;
System.out.print("*");
}
System.out.print("\n");
}
}