My program uses StdDraw to create an N-by-N grid. I'm supposed to accept N and T in the command line (N is the number of lines in the grid, T is the number of times I can try to escape the grid in a random walk). I keep getting an error that says:
Exception in thread "main" java.lang.NegativeArraySizeException
at RandomWalk.main(RandomWalk.java:28)
My program looks like this:
import java.util.Random;
public class RandomWalk {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
int tempN = N;
int DEcount = 0; //Dead End Count
int x0 = N/2;
int y0 = N/2;
int x1 = x0;
int y1 = y0;
StdDraw.setXscale(0.0, N);
StdDraw.setYscale(0.0, N);
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.setPenRadius(0.002);
while (N >= 0) {
StdDraw.line(tempN, N, 0, N);
N--;
}
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.setPenRadius(0.002);
N = tempN;
while (N >= 0) {
StdDraw.line(N, tempN, N, 0);
N--;
}
for (int i = 0; i < T; i++) {
boolean[][] check = new boolean[N][N];
while (x1 > 0 && x1 < N-1 && y1 > 0 && y1 < N-1) {
//check for dead ends and make a random move
check[x1][y1] = true;
if (check[x1-1][y1] && check[x1+1][y1] && check[x1][y1-1] && check[x1][y1+1]) {
DEcount++;
break;
}
double rand = Math.random();
if (rand < 0.25) { if (!check[x1+1][y1]) x1++;}
else if (rand < 0.50) { if (!check[x1-1][y1]) x1--;}
else if (rand < 0.75) { if (!check[x1][y1+1]) y1++;}
else if (rand < 1.00) { if (!check[x1][y1-1]) y1--;}
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(0.01);
StdDraw.line(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
}
}
}
}
Additionally, what I'm supposed to be printing on the grid (the red lines that represent the random walk) aren't printing. The grid itself does print, though.
Can anyone help me figure out what I'm doing wrong?
Help is appreciated.
Consider this code fragment:
while (N >= 0) {
StdDraw.line(N, tempN, N, 0);
N--;
}
for (int i = 0; i < T; i++) {
boolean[][] check = new boolean[N][N];
At the end of the while loop, N is -1, and then you use it as an array size.
You may have meant to use tempN which seems to preserve N. I suggest more descriptive names to avoid this sort of problem.
issue 1: Inside your while loop you decrement variable N until its -1. its next use is as an array size specifier when allocating check. I guess you forgot another assignment N = tempN immediately before the for loop.
Related
I built 2 methods 1 get a number's digits, the 2nd sums all the digits of the number. It works , but for some reason when i type the number 11111 or higher it returns a wrong result.
For ex. the number 11111 returns 3 instead of 5
public class test_2 {
public static int getDigits(int x) {
int counter = 0;
while (x > 0) {
x /= 10;
counter++;
}
return counter;
}
public static int getNumber(int y) {
int New = 0;
for (int i = 0; i <= test_2.getDigits(y); i++) {
New += (y % 10);
y /= 10;
if (y < 10 && y > 0)
New += (y % 10);
}
return New;
}
}
There are multiple issues with your code, Follow below
test_2.getDigits(y) changes in each iteration of the loop, since y keeps changing
if (y < 10 && y > 0) condition is not necessary
The following will work:
public static int getNumber(int y) {
int New = 0;
for (int i = 0, len = getDigits(y); i < len; i++) {
New += (y % 10);
y /= 10;
}
return New;
}
Or simply:
public static int getNumber(int y) {
int New = 0;
while (y > 0) {
New += (y % 10);
y /= 10;
}
return New;
}
I am trying to compute a summation of float numbers. All small numbers output properly, when I use very large numbers as inputs, the output is always off by a few integers. For example, H = 5764801 W = 1679616, on paper, works out as 335923 30275911. In my program though, 335923 30275908 is printed instead. Here is the code:
public void printOutput(int H, int W) // The inputs
{
if(H == 1 && W == 1)
{
System.out.println(0 + " " + 1);
return;
}
List<Integer> pfw = primeFactors(W);
int y = 1;
while(H != (int) (Math.pow(Math.pow(W, 1f/y) + 1f, y))) y++;
final float N = findWholeNumber(pfw);
float height = 0;
for(int x = 1; x <= y + 1; x++)
{
height += (float) (W * Math.pow((N + 1f) / N, x-1f) + 1e-8); //Here is the summation
}
float cats = 1;
for(int x = 2; x <= y + 1; x++)
cats += (float) (Math.pow(N, x-1));
int notWorking = (int) (cats - W);
System.out.println(notWorking + " " + (int)height); //Outputs printing
}
private int findWholeNumber(List<Integer> factors)
{
List<Integer> common = new ArrayList<Integer>();
for(int i = 0; i < factors.size(); i++)
{
if(common.contains(factors.get(i))) continue;
common.add(factors.get(i));
}
int num = common.get(0);
for(int i = 1; i < common.size(); i++)
num *= common.get(i);
return num;
}
private List<Integer> primeFactors(int num)
{
List<Integer> pf = new ArrayList<Integer>();
if(num == 1)
{
pf.add(1);
return pf;
}
for(int j = 2; j <= num; j++)
while(num % j == 0) // is prime
{
pf.add(j);
num /= j;
}
return pf;
}
}
Floating point numbers have a limited precision as mantissa has a limited width.
You could try double for your case which precision is more (as its mantissa is wider), but it is also limited.
More information: https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008 and What is the maximum number in the mantissa part of a Java float?
If you need to have an unlimited precision, try BigDecimal. The count of significant digits there is only limited by the amount of your memory.
If you only need integer values, BigInteger is an option.
Study What Every Computer Scientist Should Know About Floating-Point Arithmetic, David Goldberg, 1991.
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I'm working on a couple of Project Euler problems and want to test my solution. My recursive function never ends even with reachable base cases.
in a 20x20 grid I am using x and y coordinates to navigate up and left to find the number of paths from (19,19) to (0,0). My base case is to return 1 when we reach (0,0). Otherwise I add the current count to the recursive call.
Function:
private static int numPaths(int x, int y, int pathsFound)
{
if(x == 0 && y == 0)
return 1;
else
{
if(x > 0)
{
pathsFound += numPaths(x - 1, y, pathsFound);
}
if(y > 0)
{
pathsFound += numPaths(x, y - 1, pathsFound);
}
}
return pathsFound;
}
Main:
int x = 19;
int y = 19;
System.out.println("Answer: " + numPaths(x, y, 0));
Is there a flaw in my recursive logic, or is just taking a very long time to compute? If you know the solution to this Euler problem, please do not post it.
https://projecteuler.net/problem=15
So if anyone is interested, I looked into memoization and came up with an elegant solution without recursion.
Function:
private static BigInteger numberPaths(ArrayList<ArrayList<BigInteger>> grid)
{
for(int i = 0; i <= 20; ++i)
{
for(int j = 0; j <= 20; ++j)
{
int x = j;
int y = i;
if(x - 1 < 0 || y - 1 < 0)
{
grid.get(x).set(y, BigInteger.ONE);
}
else
{
BigInteger topVal = grid.get(x - 1).get(y);
BigInteger leftVal = grid.get(x).get(y - 1);
grid.get(x).set(y, topVal.add(leftVal));
}
}
}
return grid.get(20).get(20); //the solution
}
Main:
ArrayList<ArrayList<BigInteger>> grid = new ArrayList<>();
for(int i = 0; i <= 20; ++i)
{
ArrayList<BigInteger> column = new ArrayList<>();
for(int j = 0; j <= 20; ++j)
{
column.add(BigInteger.valueOf(0));
}
grid.add(column);
}
System.out.println("Answer: " + numberPaths(grid));
You are given a 2D array as a string and a word via keyboard. The word
can be in any way (all 8 neighbors to be considered) but you can’t use
same character twice while matching. Return word's first and last
character's index as (x,y). If match is not found return -1.
That's the question. I'm having trouble with searching. I tried that:
int x=0,y=0;
for(int f=0; f<WordinArray.length; f++){
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[0].length; j++){
if(matrix[i][j].equals(WordinArray[f])){
x=i; y=j;
System.out.print("("+x+","+y+")");
}
}
}
}
But, That code is not working as it is supposed to. How else I can write this searching code?
Referring to Sixie's code
Assuming this is a valid input/output to your program?
Size:
4x4
Matrix:
a b c d
e f g h
i j k l
m n o p
Word: afkp
(0,0)(3,3)
I edited your code, so that it should work for input on this form (it is case sensitive at the moment, but can easily be changed by setting .toLowerCase()
Scanner k = new Scanner(System.in);
System.out.println("Size: ");
String s = k.nextLine();
s.toUpperCase();
int Xindex = s.indexOf('x');
int x = Integer.parseInt(s.substring(0, Xindex));
int y = Integer.parseInt(s.substring(Xindex + 1));
System.out.println("Matrix:");
char[][] matrix = new char[x][y];
for (int i = 0; i < x; i++) {
for (int p = 0; p < y; p++) {
matrix[i][p] = k.next().charAt(0);
}
}
System.out.print("Word: ");
String word = k.next();
int xStart = -1, yStart = -1;
int xEnd = -1, yEnd = -1;
// looping through the matrix
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
// when a match is found at the first character of the word
if (matrix[i][j] == word.charAt(0)) {
int tempxStart = i;
int tempyStart = j;
// calculating all the 8 normals in the x and y direction
// (the 8 different directions from each cell)
for (int normalX = -1; normalX <= 1; normalX++) {
for (int normalY = -1; normalY <= 1; normalY++) {
// go in the given direction for the whole length of
// the word
for (int wordPosition = 0; wordPosition < word
.length(); wordPosition++) {
// calculate the new (x,y)-position in the
// matrix
int xPosition = i + normalX * wordPosition;
int yPosition = j + normalY * wordPosition;
// if the (x,y)-pos is inside the matrix and the
// (x,y)-vector normal is not (0,0) since we
// dont want to check the same cell over again
if (xPosition >= 0 && xPosition < x
&& yPosition >= 0 && yPosition < y
&& (normalX != 0 || normalY != 0)) {
// if the character in the word is not equal
// to the (x,y)-cell break out of the loop
if (matrix[xPosition][yPosition] != word
.charAt(wordPosition))
break;
// if the last character in the word is
// equivalent to the (x,y)-cell we have
// found a full word-match.
else if (matrix[xPosition][yPosition] == word
.charAt(wordPosition)
&& wordPosition == word.length() - 1) {
xStart = tempxStart;
yStart = tempyStart;
xEnd = xPosition;
yEnd = yPosition;
}
} else
break;
}
}
}
}
}
}
System.out.println("(" + xStart + "," + yStart + ")(" + xEnd + ","
+ yEnd + ")");
k.close();
I think you need to plan your algorithm a bit more carefully before you start writing code. If I were doing it, my algorithm might look something like this.
(1) Iterate through the array, looking for the first character of the word.
(2) Each time I find the first character, check out all 8 neighbours, to see if any is the second character.
(3) Each time I find the second character as a neighbour of the first, iterate along the characters in the array, moving in the correct direction, and checking each character against the word.
(4) If I have matched the entire word, then print out the place where I found the match and stop.
(5) If I have reached the edge of the grid, or found a character that doesn't match, then continue with the next iteration of loop (2).
Once you have your algorithm nailed down, think about how to convert each step to code.
If I understood your question right. This is a quick answer I made now.
int H = matrix.length;
int W = matrix[0].length;
int xStart = -1, yStart = -1;
int xEnd = -1, yEnd = -1;
String word = "WordLookingFor".toLowerCase();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (matrix[i][j] == word.charAt(0)) {
int tempxStart = i;
int tempyStart = j;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int k = 0; k < word.length(); k++) {
int xx = i+x*k;
int yy = j+y*k;
if(xx >= 0 && xx < H && yy >= 0 && yy < W && (x != 0 || y != 0)) {
if(matrix[xx][yy] != word.charAt(k))
break;
else if (matrix[xx][yy] == word.charAt(k) && k == word.length()-1) {
xStart = tempxStart;
yStart = tempyStart;
xEnd = xx;
yEnd = yy;
}
} else
break;
}
}
}
}
}
}
A little trick I used for checking all the 8 neighbors is to use two for-loops to create all the directions to go in:
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if(x !=0 || y != 0)
System.out.println(x + ", " + y);
}
}
This creates
-1, -1
-1, 0
-1, 1
0, -1
0, 1
1, -1
1, 0
1, 1
Notice: All but 0,0 (you don't want to revisit the same cell).
The rest of the code is simply traversing though the matrix of characters, and though the whole length of the word you are looking for until you find (or maybe you don't find) a full match.
This time the problem is that how could I print word's first and last
letter's indexes. I tried various ways like printing after each word
was searched. But, all of them didn't work. I am about to blow up.
int[] values = new int[2];
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[0].length; j++){
if(Character.toString(word.charAt(0)).equals(matrix[i][j]) == true || Character.toString(ReversedWord.charAt(0)).equals(matrix[i][j]) == true ){
System.out.print("("+ i + "," +j+")");
//First letter is found.Continue.
for(int p=1; p<word.length(); p++){
try{
for (int S = -1; S <= 1; S++) {
for (int SS = -1; SS <= 1; SS++) {
if(S !=0 || SS != 0)
if(matrix[i+S][j+SS].equals(Character.toString(word.charAt(p))) && blocksAvailable[i+S][j+SS] == true ||
matrix[i+S][j+SS].equals(Character.toString(ReversedWord.charAt(p))) && blocksAvailable[i+S][j+SS] == true) {
values[0] = i+S;
values[1] = j+SS;
blocksAvailable[i+S][j+SS] = false;
}
}
}
}catch (ArrayIndexOutOfBoundsException e) {}
I wrote this method for my java minesweeper game, it is supposed to check spots surrounding a set of coordinates and then calculate how many bombs are near by.
public void numberMines(){
int count = 0;
int x = 0;
int y = 0;
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
//first 2 loops go through every spot on the board
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = xMin; i <=xMax; i++){
for (int j = yMin; j <=yMax; j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
count = 0;
}
}
}
}
Every time I run this method it returns 3,2,1, or empty so it does count how many bombs are around, but for some reason it is over looping and returning the same thing for every spot that is not a bomb after the first one. I really cant see where I messed up, please help!
Move this block of code:
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
Inside of your for loops:
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//Insert code here <---
Because at the moment, you're doing these calculations once, for x=0, y=0.
The code would probably also look cleaner if you moved the setting of count to 0 just before the i,j, loops, and not having it done once before all loops start, and again inside the conditional that displays the result.
Based on your comment - I think your valid indexes range from 0..(rows-1) and 0..(columns-1) - so you have a fencepost error also. Modify these lines:
if (x == rows-1){
xMax = rows-1;
}
if (y == columns-1){
yMax = columns-1;
}
But still have this entire block inside of your x/y loops. You don't get the out of bounds error when they're outside because you never calculate xMax and yMax when x and y are at their maximum values.
Avoid to declare all variables at the beginning of a method, better declare them close to when they are used. To fix your problem, you need to compute count, xMin, xMax, yMin and yMax in the loops like this:
public void numberMines(){
//first 2 loops go through every spot on the board
for (int x=0; x<rows; x++){
for (int y=0; y<columns; y++){
int count = 0;
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = (x == 0 ? 0 : x-1); i <= (x == rows ? rows : x+1); i++){
for (int j = (y == 0 ? 0 : y-1); j <= (y == rows ? rows : y+1); j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
}
}
}
}
I have inlined the boundary checks, which is not necessary, but makes the code shorter to present here.