Java creating a 2d int array which getting bigger inside - java

I want to create a 2d int array thats getting bigger from the outside to the inside, the first layer should start with 1 and the next layer should be 1 higher.
With n = 3 it should look like this :
1 1 1 1 1 1
1 2 2 2 2 1
1 2 3 3 2 1
1 2 3 3 2 1
1 2 2 2 2 1
1 1 1 1 1 1
thats what i already have
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 1 ;
for (int i = 0; i < 2*n; i++) {
for (int j = 0; j < 2*n; j++) {
feld[i][j] = c-i+j;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //this to printing the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}

This should work.
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 0;
while(c<n) {
for(int i = c; i < 2*n-c; i++) {
feld[c][i] = c+1;
feld[i][c] = c+1;
feld[2*n-c-1][i] = c+1;
feld[i][2*n-c-1] = c+1;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //print the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}
You can change n to whatever value and it should still work

Related

Printing the even numbers in the columns of 2D array

hello guys I am struggling to find the right way to print only the even numbers of the array.
I made a 1 dimensionnal array to save the element of the columns and then make sure if the index of the col%2==0 put that number in the output.
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//array with 3 row in 5 col
int[][] matrix = new int[3][5];
//int []y = new int[5];
// to impalement th array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Enter matrix[" + i + "][" + j + "]: ");
matrix[i][j] = input.nextInt();
}
System.out.print("\n");
}
System.out.print("matrix values \n");
// to show up the originally array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.print("\n");
}
//////the new array to display only the even numbers in the col
System.out.print("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}
}
}
}
the output doesn't print the new array
matrix values
1 2 3 4 5
6 7 8 9 10
3 2 4 5 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Matrix.main(Matrix.java:43)
Replace i by k here :
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}

Transposing a 2D Array Only Works For Rectangular Arrays - Java

I have a class with a method that transposes an array given the array, rows, and columns
public class Transpose {
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayRows][arrayColumns];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
}
The output I get looks like this:
Original:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
Transposed:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
I realized that the method only works when the values of arrayRows and arrayColumns are the same value, for example: 6, 6 or 5, 5. I tried to place the values opposite of each other, the rows in columns and vice versa, however it did not work. How can I get the method to work for non-rectangular/square arrays?
See line comments for explanation. Those are the only lines I modified.
public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
System.out.println("Transposed:");
for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You need to swap the places of arrayRows and arrayColumns in the transposed matrix, because the new matrix is supposed to be a [5][6] instead of a [6][5].
So your line of
int[][] transposedArray = new int[arrayRows][arrayColumns];
becomes
int[][] transposedArray = new int[arrayColumns][arrayRows];
We also need to swap i and j in the following statement, because the loops are following the indices of the original matrix:
transposedArray[i][j] = array[j][i];
to
transposedArray[j][i] = array[i][j];
And lastly, you can't print the transposed matrix while you're creating it as you're doing now, because you're just re-printing the original matrix that way. I suggest printing the matrix after you're done creating it.
With these changes, your code ends up like this:
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayColumns][arrayRows];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[j][i] = array[i][j];
}
}
for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
for(int j = 0; j < transposedArray[i].length; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You can see a working example here.
for(int i = 0; i < array.length; i++)
// should count the number of rows. = 6
iterating from 0 ... 5
for(int j = 0; j < array[i].length; j++) // should count the number of columns = 5
iterate from 0..4
transposedArray[i][j] = array[j][i];
to access 6 ... you need j=5 ; i = 0...4
your loops are not able to access the values

Creating a number gird in Java

I'm trying to create a number grid in Java that would look like this:
789
456
132
public class Test {
public static void main(String[] args) {
//create grid
int [] [] grid = new int [3][3];
grid [0][0] = 7;
grid [1][0] = 8;
grid [2][0] = 9;
grid [0][1] = 4;
grid [1][1] = 5;
grid [2][1] = 6;
grid [0][2] = 1;
grid [1][2] = 2;
grid [2][2] = 3;
int rows = 3;
int columns = 3;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println(grid[i][j] + " ");
}
System.out.println("");
}
}
}
the result that I'm getting is:
7
4
1
8
5
2
9
6
3
Any suggestions on to where I am going wrong? Many thanks in advance
System.out.println() prints a newline every time. Instead use System.out.print() in the inner loop. Also you need to switch i and j when printing:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}
use System.out.print(grid[j][i] + " "); you need to swap the position ofi and j because your numbers entered are in columns whereas you want them to be printed in rows.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}
output
7 8 9
4 5 6
1 2 3

When sorting row, how to swap columns along in multidimensional-array?

I have an array[x][y]
4 2 0 3 1
t o b e o
r n o t t
o b e x x
How can i sort only the for row but move the column with the row.
The result have to be
0 1 2 3 4
b o o e t
o t n t r
e x b x o
i tried with
for (char[] innerArray : grid) {
Arrays.sort(innerArray);
}
but it sorts all rows.
Here is the solution for Your problem. this Algorithme can do exactly what you mention above.
Integer[][] arr = new Integer[4][4];
arr[0][0] = 4;
arr[0][1] = 3;
arr[0][2] = 1;
arr[0][3] = 2;
arr[1][0] = 6;
arr[1][1] = 4;
arr[1][2] = 9;
arr[1][3] = 2;
arr[2][0] = 9;
arr[2][1] = 7;
arr[2][2] = 5;
arr[2][3] = 4;
arr[3][0] = 11;
arr[3][1] = 33;
arr[3][2] = 6;
arr[3][3] = 8;
for (int i = 0; i < arr[0].length; i++) {
for (int j = i+1; j < arr[0].length; j++) {
if (arr[0][i] > arr[0][j]) {
for (int k = 0; k < arr.length; k++) {
int temp = arr[k][i];
arr[k][i] = arr[k][j];
arr[k][j] = temp;
}
}
}
}
To print out the Array for testing purpose .
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if (arr[i][j] > 9) {
System.out.print(arr[i][j]+" ");
}else{
System.out.print(arr[i][j]+" ");
}
}
System.out.println();
}
This is an exemple of the above.
before Sorting
4 3 1 2
6 4 9 2
9 7 5 4
11 33 6 8
After Sorting
1 2 3 4
9 2 4 6
5 4 7 9
6 8 33 11
I hope that this solution meet your requirements.

can't find how to program this number pattern

Number Pattern
I am asked to enter a number rc, and based on rc construct this pattern. I am able to initialize the table but without the highlighted numbers:
int [][] num2 = new int [rc][rc];
counter = 1;
for(int i = 0; i < rc; i++){
if(i!=0)
counter--;
for(int j =0; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
}
Any hints or ideas?
You got it partially right. The numbers printed on each row are the same but the start point is incremented by 1 each time. Thus, you can use variable i again to shift it.
int [][] num2 = new int [rc][rc];
int counter = 1;
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
num2[i][(j + i) % rc] = counter++;
}
}
The following code is working fine for your problem.
int rc=5;
int [][] num2 = new int [rc][rc];
int counter = 1;
for(int i = 0; i < rc; i++){
for(int j =i; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
for(int k =0; k < rc; k++){
if(num2[i][k]==0){
num2 [i] [k] = counter;
counter++;
}
System.out.print(num2[i][k]+"\t");
}
System.out.println();
}
The logic behind my solution is:
First fill an array from 1 - N, where N is the user input (or
rc in this case):
Then, we check if it's not the first line, if it is, we simply print
the numbers in order.
Now, we have to know which numbers go first:
In the line 1 (remember it starts from 0), it must print the number at [1][4] in [1][0], so our loop substracts rc - i + j, this gives: 5 - 1 + 0, which in fact is index [4].
We know that after we've printed the last numbers first, we must continue the sequence, so we print index: [1][0] at [1][1] (Why 1, 2? Because otherwise we would get something like the example below, that's why we need to substract 1 to it
1 2 3 4 5
10 7 8 9 10
And that's it:
public class StrangePattern {
public static void main(String[] args) {
int rc = 5;
int number = 1;
int spaces = 0;
int[][] numbers = new int[rc][rc];
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
numbers[i][j] = number;
number++;
}
}
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
if (i != 0) {
if (j < i) {
System.out.print(numbers[i][rc - i + j] + "\t");
} else {
System.out.print(numbers[i][j - spaces] + "\t");
}
} else {
System.out.print(numbers[i][j] + "\t");
}
}
spaces++;
System.out.println();
}
}
}
Which provides this output:
1 2 3 4 5
10 6 7 8 9
14 15 11 12 13
18 19 20 16 17
22 23 24 25 21
And this one for rc = 3:
1 2 3
6 4 5
8 9 7

Categories

Resources