read all elements of martix diagonally from upper left to lower right - java

I am having a java code that reads diagonal values from upper right to lower left , But I want code to read values from upper left to lower right.
int [][]mat = { {1,2,3},
{4,5,6},
{7,8,9},
};
int N=3;
for (int s=0; s<N; s++) {
for (int i=s; i>-1; i--) {
System.out.print(mat[i][s-i] + " ");
}
System.out.println();
}
for (int s=1; s<N; s++) {
for (int i=N-1; i>=s; i--) {
System.out.print(mat[i][s+N-1-i] + " ");
}
System.out.println();
}
and output of above code is
1
4 2
7 5 3
8 6
9
I want sequence like
3 2 6 1 5 9 4 8 7

int [][]mat = { {1,2,3},
{4,5,6},
{7,8,9},
};
int N=3;
for (int s=N-1; s>-N; --s) {
int iMin = s>0 ? s : 0;
int iMax = s>0 ? N : N+s;
for (int i=iMin; i<iMax; ++i) {
System.out.print(mat[i-s][i] + " ");
}
}
System.out.println();
Output
3 2 6 1 5 9 4 8 7

I have modified your code to achieve the needed output.
for (int s = N - 1; s > -1; s--) {
for (int i = s; i < N; i++) {
System.out.print(mat[i - s][i] + " ");
}
System.out.println();
}
for (int s = 1; s < N; s++) {
for (int i = s; i < N; i++) {
System.out.print(mat[i][i - s] + " ");
}
System.out.println();
}

Related

Java Number Pyramid

I want to create a number pyramid like this,
7
5 6 5
3 4 5 4 3
1 2 3 4 3 2 1
but I couldn't create it. I pasted my code and its output is like this,
7
5 6 7 6 5
3 4 5 6 7 6 5 4 3
1 2 3 4 5 6 7 6 5 4 3 2 1
What should I do to fix this ? Is my code totally wrong or litte changes are enough ?
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
for (int i = width; i >= 1; i=i-2)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = i; j <= width; j++)
{
System.out.print(j+" ");
}
for (int j = width-1; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
You may compute the row index, then it's easy to get the number ranges
for (int i = width; i >= 1; i -= 2) {
int row_idx = (width - i) / 2;
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= i + row_idx; j++) {
System.out.print(j + " ");
}
for (int j = i - 1 + row_idx; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
You just need to count the level. You can do this by adding additional count variable. Lets take an example:
On level 0 you just print 7 and count is 0.
On level 1 count is 1. So you need to only print from i's current value + count amount on left and right. But since we counted 1 extra on left so a value will be less on right.
.........
And code will go on like this.
See code for better explanation:
public static void main( String[] args ) {
Scanner myScan = new Scanner(System.in);
System.out.print("Enter mountain width: ");
int width = myScan.nextInt();
System.out.println("Here is your mountain ");
myScan.close();
System.out.println();
int counter = 0;
for (int i = width; i >= 1; i = i - 2) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = i; j <= width - counter; j++) {
System.out.print(j + " ");
}
for (int j = width - 1 - counter; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
counter++;
}
}
I just changed a little bit on your code to make it work. Cheers mate !

How to print numbers which are below left diagonal in matrix?

for (i = 1; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
The above code is not able to print numbers which are below the left diagonal. For a 3x3 matrix my code is printing:
1 2 3
4 5 6
7 8 9
OUTPUT :
4 5
7 8
Desired output:
4
7 8
You could add an if statment like this :
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
if (i>j) {
System.out.print(a[i][j] + " ");
}
}
System.out.println();
}
Or better you could do :
for(int i=0;i<a.length;i++) {
for(int j=0;i>j;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
A simple way to achieve this depending on the matrix size is
final int matrixSize = 7; // your matrix size
for (int i = 0; i < matrixSize; ++ i) {
for (int j = 0; j < i; ++ j) System.out.print(a[i][j] + " ");
System.out.print("\n");
}

How to print the odd numbers in 2 2D arrays java from user input?

I am able to print the 2 2D arrays with the numbers from user input and I am also able to print the odd numbers, but I am having trouble combining the two forms of code so that the odd numbers stay consistent with their cell and not just in one print line. How would I print the odd numbers leaving the non odds as blanks in 2 3x3 arrays?
Heres the code for printing the array:
public static void display ( int[][] FirstArray, int[][] SecondArray)
{
//Print first array
System.out.print("Array1: \n");
for (int row = 0; row < FirstArray.length; row++)
{
for(int column = 0; column < FirstArray[row].length; column++)
{
System.out.print(FirstArray[row][column] + " ");
}
System.out.println();
}
//Print second array
System.out.print("Array2: \n");
for (int row = 0; row < SecondArray.length; row++)
{
for(int column = 0; column < SecondArray[row].length; column++)
{
System.out.print(SecondArray[row][column] + " ");
}
System.out.println();
}
}
ex output:
array 1: array2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
Here is the code for printing the odd numbers without in the 3x3 format like the code above:
public static void display(int[][] FirstArray, int[][] SecondArray)
{
int count=0;
for (int i = 0; i < FirstArray.length; i++) {
for (int j = 0; j < FirstArray.length; j++)
{
if(FirstArray[i][j]%2==1)
{
System.out.println(m1[i][j]);
}
}
}
for (int i = 0; i < SecondArray.length; i++) {
for (int j = 0; j < SecondArray.length; j++)
{
if(SecondArray[i][j]%2==1)
{
System.out.println(SecondArray[i][j]);
}
}
}
ex output:
3 3 3 3 3 3 3 3 3 (odd numbers displayed but in one line)
Ex output of what Im looking for(assuming i entered in even numbers too):
3 3 3
3 3 3
3 3
You can redress your print statements in both the loops as :
for (int i = 0; i < FirstArray.length; i++) {
for (int j = 0; j < FirstArray.length; j++) {
if(FirstArray[i][j]%2==1) {
System.out.print(m1[i][j] + " "); // odd number and space
} else {
System.out.print(" "); // blank space for even numbers
}
}
System.out.println(); // next line for next row
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers of rows : ");
int n = sc.nextInt();
System.out.println("Enter number of colmns : ");
int m = sc.nextInt();
int[][] array = new int[n][m];
System.out.println("Enter elements of array : ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(" X[ " + i + "," + j + "] = ");
array[i][j] = sc.nextInt();
}
}
System.out.print("Even numbers on position :");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] % 2 != 0) {
System.out.print(array[i][j] + " ");
}
}
}

How to display n by n square pattern from numbers 1 through nine then 0 in Java

I have already tried putting values in an array, using matrices and even using recursive arrays but those only return error to me.
Now the code I have made looks like this
import java.util.*;
public class SQf1t9t0 {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
f19t0[i][j] = i+1;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
}
The input for instance has to look like this if the entered value for n is 4:
1234
5678
9012
I don't really think you need array just to print values. You can simply have counter and rest it once it more that 9. Something like that:
int counter = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print(counter);
if(++counter > 9) {
counter = 0;
}
}
System.out.println();
}
You just need to use modulo to start back at 0 after 9.
import java.util.Scanner;
public class DisplayMatrix
{
public static void main(String[] args) {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
f19t0[i][j] = (i * n + j + 1) % 10;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
yar.close();
}
}
For example :
Enter size for n square:
6
1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 0 1 2 3 4
5 6 7 8 9 0
1 2 3 4 5 6
PS: Don't forget to close your scanner.
You don't have a method in your class. I don't know whether you didn't put it mistakenly or not! But the following worked.
import java.util.*;
public class test {
public static void main(String[] args) {
int n, i, j;
Scanner yar = new Scanner(System.in);
System.out.println("Enter size for n square: ");
n = yar.nextInt();
int[][] f19t0 = new int[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
f19t0[i][j] = (i*n+j+1) % 10;
System.out.print(f19t0[i][j] + " ");
}
System.out.println();
}
yar.close();
}
}
For input 5, it outputs:
Enter size for n square:
5
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5

Printing pyramid of numbers

I have homework to make a triangle that looks like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I have been able to create almost half the triangle with the following code:
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
I have been unable to figure out how to mirror the other half of the triangle with my code to look like the triangle above. The instructor hinted that using the for loop with the tab return \t is the way to do this.
Like #Maroun's answer but simpler
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
or
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i - size + 1; j <= i; j++)
System.out.print((j > 0 ? j : "") + " ");
System.out.println();
}
prints
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
You can try this one:
int x = 0;
for(int i = 1; i <= 5; i++) {
for(int k = x; k < 4; k++) {
System.out.print(" ");
}
x++; //less spaces will be printed in the next iteration
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
I wrote another loop that begins with 4 spaces and then the amount of spaces is reduced.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Tip: Always open { for your loops and conditions, even for one operation, this might prevent bugs.

Categories