Making a border of numbers in descending order in matrix - java

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

Related

How to understand what is going on in line line 23 of the for loop initilization

I need help in understanding why we initialize the for loop at line 23 to (size / 2) - 1. I understand this entire for loop is controlling the printing for the 2nd half of the hour glass but why is that specific initialization necessary. I thought if in order to print the 2nd half we would have to pick up where we left off at size / 2. The int i would be 1 in the code for its first iteration and that doesn't make any sense to me, shouldn't it be 2 if the first half starts at 0?
Actual problem being worked:
Define a method called drawHourClass that draws an hour glass with a specific number of lines. For example, calling drawHourClass(4) will draw the following diagram:
****
**
**
****
public class Hour_Glass {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hour_Glass hourGlass = new Hour_Glass();
hourGlass.drawHourGlass(4);
}
public void drawHourGlass(int size) {
for (int i = 0; i < size / 2; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < size - (i * 2); k++) {
System.out.print("*");
}
for (int l = 0; l < i; l++) {
System.out.print(" ");
}
System.out.println();
}
for (int i = ((size / 2) - 1); i >= 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < size - (i * 2); k++) {
System.out.print("*");
}
for (int l = 0; l < i; l++) {
System.out.print(" ");
}
System.out.println();
}
}
}
The second for i loop moves back through the same numbers that the first one moved through but in the opposite direction. The last time i processed anything it was at (size / 2) - 1 and it jumped out of the loop at (size / 2). So the second loop starts at (size / 2) - 1 and jumps out when it gets to -1.
For size of 4, the top loop is going to produce two lines of output:
i == 0
i == 1
The second loop is also going to produce two lines of output:
i == 1
i == 0
And the code inside each is duplicated, so you get your four lines as in your example.
I'm not sure the confusion. In the top loop, you're going to loop with i starting at zero and going to (size/2) - 1 (because of the < comparison in the for statement). The second one is going to go from (size/2)-1 to 0. So the same thing but in the opposite direction.

to output all elements of square matrix in a spiral

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

JAVA Writing the diagonal of an 2-dimensional array into an 1-dimensional array

Want to write the diagonal of an 2-dimensional array (n*n Matrix) into an one-dimensional array.
1 2 3
4 5 6 => 1 5 9
7 8 9
public int[] getDiagonalFromArray(int[][] two_d_array){
int[] diagonal_array = new int[two_d_array[0].length];
int k=0;
for (int i = 0; i < two_d_array[0].length; i++) {
for (int j = 0; j < two_d_array[1].length; j++) {
for (int l = 0; l < two_d_array[0].length; l++) {
diagonal_array[k]=two_d_array[i][j];} //HERE SHOULD BE THE ERROR... HOW DO I CYCLE THROUGH THE 1dim "diagonal_array"?
}
}
return diagonal_array;
}
This method delivers wrong values.
This method of mine works, but just Prints the diagonale, instead of putting it into an 1dim array.
public void getDiagonal(int[][] two_d_array){
//int[] diagonal_array = new int[two_d_array[0].length];
for (int i = 0; i < two_d_array[0].length; i++) {
for (int j = 0; j < two_d_array[1].length; j++) {
if (i==j) System.out.print(two_d_array[i][j]+" ");
}
}
}
Where is the logical difference? I tried the if-clause on the first method, but it raises the "outofbound"-Exception.
Thanks in advance.
Why do you need more than one loop?
for (int i = 0; i < two_d_array[0].length; i++) {
diagonal_array[i]=two_d_array[i][i];
}
Seems to be enough to me.
If your matrix has the same width and height, this is a solution:
public int[] getDiagonal(int[][] two_d_array){
int[] diagonal_array = new int[two_d_array.length];
for (int i = 0; i < two_d_array.length; i++) {
diagonal_array[i] = two_d_array[i][i];
}
return diagonal_array;
Here, I consider principal diagonal elements to be the set of elements , where n & m are the number of rows and the number of columns (per row?) respectively.
Thus, the number of diagonal elements is never greater than min(numOfRows, numOfColumns).
And so, you can always try:
public int[] getDiagonalFromArray(int[][] 2DArray){
int[] diagonalArray = new int[Math.min(2DArray.length, 2DArray[0].length]);
int k=0;
for (int i = 0; i < 2DArray.length && k < diagonalArray.l length; ++i) {
for (int j = 0; j < 2DArray[i].length && k < diagonalArray.l length; ++j) {
if (i == j) {
diagonalArray[k++]=2DArray[i][j];
}
}
}
return diagonalArray;
}
Threw in some bounds checks for good measure.
Your input matrix must at be at least rectangular (square makes most sense), otherwise, the code will behave unreliably.
This is the same as #Andreas' answer, but I sacrifice performance and brevity here for the sake of understanding.

Mirrored Triangle generation in java

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

Filling in the main diagonals in a 2D array

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.

Categories