I have an 10x10 array and I am trying to fill it with nested loop
int A[][] = new int [10][10];
So far I have created this -
C = 1;
for (i=0; i<=9; i++)
for (j=9-i; j>=7-i; j--)
if (j>=0) {
A[i][j] = C; C=C+1;
}
And here is the result so far.
But I am trying to create this with two for loops and cannot manage it.
Thank you for any help!
This should do the job:
iterate rows as usual from 0 to 10, set column index to 9 and decrease it
in the inner loop, set the range from i to the Math.max(i - beltWidth + 1, 0) inclusive (beltWidth is the maximal count of non-zero values in a column, in this case it's 3).
final int size = 10;
final int beltWidth = 3;
int A[][] = new int [size][size];
int c = 1;
for (int i = 0, j = size - 1; i < size; i++, j--) {
for (int k = i; k >= Math.max(i - beltWidth + 1, 0); k--) {
A[k][j] = c++;
}
}
for (int[] r : A) {
for (int x : r) {
System.out.printf("%2d ", x);
}
System.out.println();
}
Output
0 0 0 0 0 0 0 6 3 1
0 0 0 0 0 0 9 5 2 0
0 0 0 0 0 12 8 4 0 0
0 0 0 0 15 11 7 0 0 0
0 0 0 18 14 10 0 0 0 0
0 0 21 17 13 0 0 0 0 0
0 24 20 16 0 0 0 0 0 0
27 23 19 0 0 0 0 0 0 0
26 22 0 0 0 0 0 0 0 0
25 0 0 0 0 0 0 0 0 0
Here is my version. I borrowed the nice printf formatting from Alex's answer :)
I just wanted to mention 2 things:
I think it's a bit easier to read if i and j are called y and x
I seem to remember that if you need performance, you need to traverse the array along the x axis, as that is how CPU cache is fetched (at least in theory). That would make the algorithm a bit more complicated, but I guess in this case we are not really concerned with performance so having y in the inner loop is fine :)
int dimension = 10;
int[][] result = new int[dimension][dimension];
int C = 27;
int yOffset = 9;
for (int x = 0; x < dimension; x++) {
for (int y = Math.min(2, yOffset); y >= 0 && yOffset - y >= 0; y--) {
result[yOffset - y][x] = C--;
}
yOffset--;
}
for (int[] y : result) {
for (int x : y) {
System.out.printf("%2d ", x);
}
System.out.println();
}
Output
0 0 0 0 0 0 0 6 3 1
0 0 0 0 0 0 9 5 2 0
0 0 0 0 0 12 8 4 0 0
0 0 0 0 15 11 7 0 0 0
0 0 0 18 14 10 0 0 0 0
0 0 21 17 13 0 0 0 0 0
0 24 20 16 0 0 0 0 0 0
27 23 19 0 0 0 0 0 0 0
26 22 0 0 0 0 0 0 0 0
25 0 0 0 0 0 0 0 0 0
Related
I want to change the values of a 2d array from a given starting position (rowPos and colPos) for a certain amount of rows and columns.
So far I have the code below:
int[][] block = new int[10][10];
int rowPos = 3, colPos = 3;
int rows = 4, columns = 4;
for (int i = rowPos; i < rows; i++)
for (int j = colPos; j < columns; j++)
block[i][j] = 1;
for (int[] x : block) {
for (int y : x)
System.out.print(y + " ");
System.out.println();
}
However this gives me the following output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
It is only setting the value at rowPos and colPos. This is my expected output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I feel like I'm close but missing something small, please help me!
Change the loops to this:
for (int i = rowPos; i < rows + rowPos; i++)
for (int j = colPos; j < columns + colPos; j++)
block[i][j] = 1;
In your code, you let i and j start at the row and column you want to begin changing the values. That's fine, but it also means your condition doesn't work anymore. Let me show you what happens:
You declare i and set it's value to 3.
You check if i (3) is less than rows (4). Success!
You declare j and set it's value to 3.
You check if j (3) is less than columns (4). Success!
You set block[3][3] to 1.
j is incremented by 1 and is now 4.
You check if j (4) is less than columns (4). Fail!
i is incremented by 1 and is now 4.
You check if i (4) is less than rows (4). Fail!
You print the array's contents.
The problem is that because you start i and j at values greater than 0, you need to keep that in mind when performing the check.
I have trouble creating a matrice for a game map design.
void prepareMatrix(int width, int height)
{
room = new int[height][width];
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(i < height/4)
{
room[i][j] = 2;
}
else if(j == 0 || j == --width)
{
room[i][j] = 1;
}
else if(i == --height)
{
room[i][j] = 1;
}
else
{
room[i][j] = 0;
}
}
}
}
I want to create something like this: (1- Wall1, 2- wall2, 0-floor)
2 2 2 2 2 2
2 2 2 2 2 2
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
And I get this:
2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2
1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The matrice would be a blueprint for the map.
You are using --width and --height. It appears from the expected result that you want the 1's to go in the first and last columns and in the last row. As a commenter implied, --width does not just return the width minus one, it also reduces width by 1. You may want width - 1 and height - 1 instead.
Like M. Aroosi said, try to change the --width to width-1 and --height to height-1. You don't want to modify the value of a parameter. I think what is happening is that every time it goes through the loop, the values change for width and height.
I am trying to write a program to locate and count all connected regions in a grid. A connected region consists of a set of marked cells (value 1) such that each cell in the region can be reached by moving up, down, left or right from another marked cell in the region, cells on a diagonal line are not considered connected.
So, it would take an input of:
10 20
0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0
0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 1
0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1
1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0
1 1 0 1 0 0 0 1 1 1 0 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0 11 11 0 11 0 0
Here is my code:
package project2;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Project2 {
private static int height;
private static int length;
public static void main(String[] args) {
String inputFile;
Scanner input = new Scanner(System.in);
System.out.print("Enter input file name: ");
inputFile = "test_case_proj2.txt";
try {
Integer grid[][] = loadGrid(inputFile);
System.out.println("Before flood fill");
printGrid(grid);
findGroups(grid, 0, 0, 2, height, length);
System.out.println("After flood fill");
printGrid(grid);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static void findGroups(Integer[][] array, int column, int row,
int counter, int height, int length) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (row < 0 || row >= length || column < 0 || column >= height) {
} else {
if (array[column][j] == 1) {
array[column][j] = counter;
findGroups(array, column, row + 1, counter, height, length);
findGroups(array, column, row - 1, counter, height, length);
findGroups(array, column - row, j, counter, height, length);
findGroups(array, column + row, j, counter, height, length);
}
}
}
counter++;
column++;
row++;
}
}
public static Integer[][] loadGrid(String fileName) throws IOException {
FileInputStream fin;
fin = new FileInputStream(fileName);
Scanner input = new Scanner(fin);
height = input.nextInt();
length = input.nextInt();
Integer grid[][] = new Integer[height][length];
for (int r = 0; r < height; r++) {
for (int c = 0; c < length; c++) {
grid[r][c] = input.nextInt();
}
}
fin.close();
return (grid);
}
public static void printGrid(Integer[][] grid) {
for (Integer[] grid1 : grid) {
for (int c = 0; c < grid[0].length; c++) {
System.out.printf("%3d", grid1[c]);
}
System.out.println();
}
}
}
Does someone see what I am doing wrong?
You put too much responsibilities into one method. You combine the floodfill algorithm with your island numbering algorithm.
I've created this jdoodle.
First of all you better create a single fill method, that does nothing more than filling islands with the value of a counter (I've made it static so you don't need to pass it through the algorithm, although this is arbitrary):
public static void fill(Integer[][] array, int column, int row, int height, int length) {
if (row >= 0 && row < length && column >= 0 && column < height && array[column][row] == 1) {
array[column][row] = counter;
fill(array, column, row + 1, height, length);
fill(array, column, row - 1, height, length);
fill(array, column - 1, row, height, length);
fill(array, column + 1, row, height, length);
}
}
As you can see, it's a simple algorithm that uses recursion.
Secondly, you simply create a method that calls the fill algorithm on all the possible tiles. If you reach a point with a value of 1, you know that this island has not been claimed by another king yet :D. Thus you fill it and claim it to the king with the counter id. Normally one uses a special array of boolean to prevent the fill algorithm to go into an infinite loop. You solved this smartly by starting to assign number from index counter = 2, of course once all island are claimed, you need to decrement the value.
public static void findGroups(Integer[][] array, int column, int row, int height, int length) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (array[i][j] == 1) {
fill(array, i,j, height, length);
counter++;
}
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (array[i][j] > 1) {
array[i][j]--;
}
}
}
}
The rest of the algorithm remains the same (the algorithm now reads from stdin, but this is simply to make sure the jdoodle keeps working).
About your algorithm, it's quite hard to understand. For instance you use a fill and part of the calls use column and row, other parts use j. Next your counter is only updated for each row. This causes problems if two idlands start at the same row (as you can see with your output).
I am trying to locate all four-connected regions in the grid. A four-connected region consists of a set of marked cells (value 1) such that each cell in the region can be reached by moving up, down, left or right from another marked cell in the region. The assignment states that we should use recursion.
An example input would be:
10 20
0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0
0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 1
0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1
1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0
1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 0 0
1 1 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0 0
0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0
0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1 1 0
1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0
And the output should be:
0 1 1 0 0 0 2 0 3 3 0 0 0 0 4 0 0 0 5 0
0 1 1 1 0 2 2 0 0 3 0 0 4 4 4 0 0 0 5 5
0 0 1 0 0 0 2 2 0 3 0 0 0 4 0 0 0 5 5 5
6 0 0 0 0 0 0 0 3 3 0 0 7 0 0 0 5 5 5 0
6 6 0 6 0 0 0 3 3 3 0 0 0 8 8 0 5 5 0 0
6 6 6 6 0 0 0 0 0 0 9 0 8 8 8 0 0 0 0 0
0 6 6 6 0 0 0 9 9 9 9 0 0 8 8 0 8 0 0 0
0 6 6 6 6 6 0 0 9 9 9 0 0 0 8 8 8 8 8 0
0 0 0 6 6 0 0 0 0 9 0 0 0 8 8 0 0 8 8 0
10 0 6 6 6 6 6 0 0 0 0 0 0 0 8 8 0 8 0 0
Right now when I run the code I have, I get this output:
0 2 2 0 0 0 2 0 2 2 0 0 0 0 2 0 0 0 2 0
0 2 2 2 0 2 2 0 0 2 0 0 2 2 2 0 0 0 2 2
0 0 2 0 0 0 2 2 0 2 0 0 0 2 0 0 0 2 2 2
2 0 0 0 0 0 0 0 2 2 0 0 2 0 0 0 2 2 2 0
2 2 0 2 0 0 0 2 2 2 0 0 0 2 2 0 2 2 0 0
2 2 2 2 0 0 0 0 0 0 2 0 2 2 2 0 0 0 0 0
0 2 2 2 0 0 0 2 2 2 2 0 0 2 2 0 2 0 0 0
0 2 2 2 2 2 0 0 2 2 2 0 0 0 2 2 2 2 2 0
0 0 0 2 2 0 0 0 0 2 0 0 0 2 2 0 0 2 2 0
2 0 2 2 2 2 2 0 0 0 0 0 0 0 2 2 0 2 0 0
My code looks like:
package project2;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class project2 {
private static int height;
private static int length;
public static void main(String[] args) {
String inputFile;
Scanner input = new Scanner(System.in);
System.out.print("Enter input file name: ");
inputFile = input.nextLine();
try {
Integer grid[][] = loadGrid(inputFile);
System.out.println("Before flood fill");
printGrid(grid);
findGroups(grid, 0, 0, 2, height, length);
System.out.println("After flood fill");
printGrid(grid);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static void findGroups(Integer[][] array, int column, int row,
int counter, int height, int length) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (row < 0 || row >= length || column < 0 || column >= height) {
} else {
if (array[i][j] == 1) {
array[i][j] = counter;
findGroups(array, column, row + 1, counter, height, length);
findGroups(array, column, row - 1, counter, height, length);
findGroups(array, column - 1, row, counter, height, length);
findGroups(array, column + 1, row, counter, height, length);
counter++;
}
}
}
}
}
public static Integer[][] loadGrid(String fileName) throws IOException {
FileInputStream fin;
fin = new FileInputStream(fileName);
Scanner input = new Scanner(fin);
height = input.nextInt();
length = input.nextInt();
Integer grid[][] = new Integer[height][length];
for (int r = 0; r < height; r++) {
for (int c = 0; c < length; c++) {
grid[r][c] = input.nextInt();
}
}
fin.close();
return (grid);
}
public static void printGrid(Integer[][] grid) {
for (Integer[] grid1 : grid) {
for (int c = 0; c < grid[0].length; c++) {
System.out.printf("%3d", grid1[c]);
}
System.out.println();
}
}
}
I'm not sure what I am doing wrong, I believe I am moving the counter up after each time. Does anyone have a recommendation of what the issue might be?
Thanks in advance.
i want setting an array and below is my code
public static void setArray()
{
int i = 5;
int j = 5;
int testarray[][] = new int[i][j];
for(int x = 0;x<i;x++)
{
for(int y=0;y<j;y++)
{
System.out.print("0 ");
}
System.out.println("");
}
}
the result is something like this:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
if i want to put a number/alphabet beside to let the user know which column, how can i do that ?
Expected Result:
====================
1 2 3 4 5
A|0 0 0 0 0
B|0 0 0 0 0
C|0 0 0 0 0
D|0 0 0 0 0
E|0 0 0 0 0
You need another initial for-loop to print the numbers, then you need to add another print statement within your second for-loop to print the letter for each row:
System.out.print(" ");
for (int x = 0; x < i; x++) { // this prints the numbers on the first row
System.out.print(" " + x);
}
System.out.println();
for (int x = 0; x < i; x++) {
System.out.print((char) ('A' + x) + "|"); // this prints the letters
for (int y = 0; y < j; y++) {
System.out.print("0 ");
}
System.out.println("");
}
0 1 2 3 4
A|0 0 0 0 0
B|0 0 0 0 0
C|0 0 0 0 0
D|0 0 0 0 0
E|0 0 0 0 0
You need to print 1, 2, 3, 4, 5 .. column number of times and print A, B, C, D .. till you reach the number of rows. Try coding it yourself, it's not that difficult (I don't want to provide a ready-made code)