I am working on a sudoku game and I am using the method of creating a solved puzzle and then "digging holes" to get the puzzle that is displayed to the user. For some reason, the testingPuzzle array is being reset to the numbers array every time it goes through the loop. It is true that the testingPuzzle array should be set to the numbers array originally, but it should be edited one spot at a time every time it goes through the loop and have a bunch of zeroes in it after a few iterations. Here is the loop itself:
do{
x = Math.abs(rand.nextInt() % 9);
y = Math.abs(rand.nextInt() % 9);
takeaway_num = testingPuzzle[x][y];
testingPuzzle[x][y] = 0;
} while (arraysEqual(solvePuzzle(testingPuzzle), numbers));
SolvePuzzle is a method that solves the given puzzle as parameters and returns that solved puzzle. So basically arraysEqual(solvePuzzle(testingPuzzle), numbers)checks to see if testingPuzzle is solvable
I am setting the testingPuzzle array equal to the numbers array before the do while loop. It looks like this:
int[][] testingPuzzle = new int[9][9];
for(int y = 0; y < 9; y++){
for(int x = 0; x < 9; x++){
testingPuzzle[x][y] = numbers[x][y];
}
}
Just so you know numbers is the sodoku answer that was generated in a previous method.
I am using my own method for testing if the arrays are equal, called "arraysEqual because I thought .equals() was the problem originally. Here is the method I am using for this:
private static boolean arraysEqual(int[][] a, int[][] b){
for(int y = 0; y < 9; y++){
for(int x = 0; x < 9; x++){
if(a[x][y] != b[x][y]){
return false;
}
}
}
return true;
}
I'm not really sure why the testingPuzzle Array is being set to the same as the numbers array at the end of each loop. I think it could have something to do with passing the actual array vs. a copy of it to a method that takes in an array but I'm not sure how that works in java.
Related
I need to arrange numbers of a given size (provided by user at runtime), to 3 dimensional array to represent these numbers in real 3D space.
For example if user enters 7 then I need to create an array of size 2,2,2 and arrange the first 7 numbers given by user into the array starting from position 0,0,0 .
The cube should always be smallest possible for example cube of size 2 can contain 2*2*2 = 8 values. And need a function that can take input numbers and return 3D integer array with values inserted from input array (input[0] will become result[0][0][0] and so on).
int input[7] ;
int[][][] result = bestFunction(int[] input) {...}
I have implemented with 3 nested for loops by checking each value at a time.
Is there a better or faster approach to implement it?
Do a Math.floor(Math.cbrt(val)) to get the dimension size.
I think we can reduce it to level 1 nesting, or using two loops, using a string input for the z-axis values.
for(int i = 0; i<size; i++)
for(int j = 0;j<size;j++)
arr[i][j]= Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
where Scanner sc = new Scanner(System.in); if you're taking in user input.
Three for loops would be O(n) as long as you break right after the last element of the input is put in.
int[][][] func(int[] input) {
int size = (int) Math.ceil(Math.cbrt(input.length));
int[][][] result = new int[size][size][size];
int x = 0;
loop: for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
result[i][j][k] = input[x++];
if (x == input.length)
break loop; //Finish processing
}
}
}
return result;
}
This question already has an answer here:
Java - Two-Dimensional Arrays - Plotting Points
(1 answer)
Closed 6 years ago.
I have an assignment for a JAVA class I am taking. We are discussing two-dimensional arrays, however on this particular assignment, I can not figure out how to return back specific points and set those points with a specific value. Here is the assignment:
Write a method called create2DArray that will fill, create, and return a 10 x 10 2d array with random numbers in the range of 1 to 100. Write a method called print2DArray that will print a 10 x 10 2D array in row column fashion. Write a method called createCoords that will search the 2D array looking for any value that is evenly divisible by 3. Once you have found a number you should log the row, column location. This means when your method finishes it should produce a list of coordinates that I can use to plot my graph. This method must also return the number of coordinates that are divisible by 3 so that I know how many points there are to plot. I am not particular as to how the coordinates are returned back as long as I get a list of the row, column locations. So, I will leave it to you to work out a mechanism for returning the values. To test that you have logged the coordinates create another function called fillLocations that will fill the locations in the array you have logged with -1. So, your program should flow in this order 1.create2DArray 2.print2DArray 3.createCoords 4.fillLocations 5.print2DArray
I understand and have completed create2DArray and print2DArray, but I can not figure out createCoords and fillLocations. Here is what I have so far, but it does not work and there are errors present:
public int createCoords(int row1, int col1){
int[][] coords = new int[row1][col1];
int[][] count = new int[0][0];
int co = 0;
for(int row = 0; row < 10; row++)
{
for(int col = 0; col < 10; col++)
{
if(coords[row][col] % 3 == 0)
co++;
return count[row][col];
}
}
return co;}
public int fillLocations(int[][] count){
int x = 0;
int y = 0;
for(int row = 0; row < 10; row++)
{
for(int col = 0; col < 10; col++)
{
if(count[row][col] % 3 == 0)
x = row;
y = col;
break;
}
}
return (x, y);}
I'm not going to write the code, but pretty much how I would go about doing this is:
I would have a separate 2d boolean array the same size as the 2d int array, auto filled with false. This would go in createCoords.
use nested for loop to cycle through all addresses of the 2d int array, and when i find a number that's divisible by three, i mark the corresponding point on the boolean array as 'true'. This would also go in createCoords.
after the for loops finish, I then look at the original 2d int array and boolean array. i would use the nested for loops again, and when i find a true value on the boolean array, i would mark the corresponding location on the int array as -1. This would go in fillLocations. You should also pass the 2d boolean array to fillLocation along with the 2d int array.
Good luck!
EDIT: this is assuming that the FillLocation function REPLACES values in the 10x10 int array with -1. That's what I interpreted your question to be. Please correct me if i'm wrong. I also added a short description at the end of each paragraph where each segment of code would go.
There are couple of problems with your code. Firstly, for the fillLocations method, you are expected to return the NUMBER of cells ([row][column]) where the value is divisible by 3. This means that for each value that passes the test:
if(coords[row][col] %3 === 0)
Then you should increment the counter by 1.
So for this part, your code would look like:
int [][] your2DArray = new int[10][10];
...
private int getNumberOfValuesDivisibleByThree(){
int numberOfValuesDivisibleByThree = 0;
for (int row=0; row < your2DArray.length; row++){
for(int col = 0; col < your2DArray[0].length; col++){
if(your2DArray[row][col] % 3 === 0){
//value at this coordinate or in this cell is divisible by 3
//here you can 'log' the coordinates as required -
System.out.println(row+","+col);
//increment the count
numberOfValuesDivisibleByThree +=1;
}
}
}
//return the final count
return numberOfValuesDivisibleByThree;
}
Please see how to create and manipulate a 2D array in Java. Syntax for creating a two-dimensional array
I have an assignment for a JAVA class I am taking. We are discussing two-dimensional arrays, however on this particular assignment, I can not figure out how to return back specific points and set those points with a specific value. Here is the assignment:
Write a method called create2DArray that will fill, create, and return
a 10 x 10 2d array with random numbers in the range of 1 to 100. Write
a method called print2DArray that will print a 10 x 10 2D array in row
column fashion. Write a method called createCoords that will search
the 2D array looking for any value that is evenly divisible by 3.
Once you have found a number you should log the row, column location.
This means when your method finishes it should produce a list of
coordinates that I can use to plot my graph. This method must also
return the number of coordinates that are divisible by 3 so that I
know how many points there are to plot. I am not particular as to how
the coordinates are returned back as long as I get a list of the row,
column locations. So, I will leave it to you to work out a mechanism
for returning the values. To test that you have logged the
coordinates create another function called fillLocations that will
fill the locations in the array you have logged with
-1. So, your program should flow in this order
create2DArray
print2DArray
createCoords
fillLocations
print2DArray
I understand and have completed create2DArray and print2DArray, but I can not figure out createCoords and fillLocations. Here is what I have so far, but it does not work and there are errors present:
public int createCoords(int row1, int col1){
int[][] coords = new int[row1][col1];
int[][] count = new int[0][0];
int co = 0;
for(int row = 0; row < 10; row++)
{
for(int col = 0; col < 10; col++)
{
if(coords[row][col] % 3 == 0)
co++;
return count[row][col];
}
}
return co;}
public int fillLocations(int[][] count){
int x = 0;
int y = 0;
for(int row = 0; row < 10; row++)
{
for(int col = 0; col < 10; col++)
{
if(count[row][col] % 3 == 0)
x = row;
y = col;
break;
}
}
return (x, y);}
As a programmer you'll nearly always need to research for doing different things. This research will be easier when you divide your problem to smaller problems.
For example you need to generate random numbers? So search on google that and you'll find this: How do I generate random integers within a specific range in Java?.
You need to create and return a 2D array? Google and see Syntax for creating a two-dimensional array
And with your art, put the pieces of the puzzle together in a way that gives your desired result.
public int[][] create2DArray() {
int[][] newArray = new int[10][10];
Random random = new Random();
int range = 100;
for(int i = 0; i < 10; i++)
{
for(int j = 0;j<arr[0].length;j++)
{
newArray[i][j]= (random.nextInt(range) + 1);
}
}
return newArray;
}
This method, creates and returns a 10*10 2D array filled with random generated numbers between 1-100. I'm sure you can write the rest of your program and enjoy from it by yourself.
This is the last problem I could not solve:
You are given an array {1,2,3,4,5,6,7,8,9}
Create a function that creates a random permutation without using a temporary list, in Θ(n) time.
Couldn't I use Collections.shuffle function to do this once I get the int[] array? I'm not sure what exactly the problem is asking for. I could do a simple loop where I could randomize it using an iterative method and do a simple check but shuffle would be easier, no?
In pseudo code:
index1 = 0
index2 = 0
loop: when index1 < array.length
index2=random[index1,array.length]
print[array[index2]
swap[index1,index2]
index1++
In Java:
int x = 0;
int y = 0;
Random r = new Random();
while(x < array.length){
y = x + r.nextInt(array.length-x);
System.out.println(array[y]);
int temp = array[x];
array[x] = array[y];
array[y] = temp;
x++;
}
I am trying to remove duplicates from an array. What I have works on an array size of 10([11]). Then i had to scale it up to 5000([5001]). I thought this would be very simple. It compiles but when I run it it runs an infinite loop. I'm not sure if it's just taking a long time or something doesn't work.
The sort.sorting works.
public class work_on_it5
{
public static void main(String [] args)
{
int array [] = new int [5001];
int LB = 1;//declare the lower bound
int UB = 5000;//declare the upper bound
for(int x = 0; x < 4999; x++)
{
if(array[x]==array[x+1])
{
array[x+1] = (int)(Math.random()*50) + 1;
sort.sorting(array);
x=0;
}
}
sort.sorting(array);
for(int x = 0; x < 4999; x++)
{
System.out.println(array[x]);
}
//median(LB, UB, array);
//mean(array);
}
The reason for infinite loop is because you are setting x=0;
for(int x = 0; x < 4999; x++)
{
if(array[x]==array[x+1])
{
array[x+1] = (int)(Math.random()*50) + 1;
sort.sorting(array);
x=0; //Here you are setting the value of x which is never changed resulting in infinite loop
}
}
in your for loop
So every time when it enters in the for loop the value of the x is equal to 0.
Also the declaration
int array [] = new int [5001];
so all the elements of the array will have the default value as 0 so the condition if(array[x]==array[x+1]) will always be true and then the above scenario that x is always 0 will cause the problem. Change the logic!
On a side note:-
It is better to use array.length instead of hard coding the length of array in for loop.
Why infinite loop happens:
1 You declare an int array as follows:
int array [] = new int [5001];
each element has a defalut value of 0.
2 in the for-loop, if(array[x]==array[x+1]) will always TRUE. and then x = 0
for(int x = 0; x < 4999; x++)
{
if(array[x]==array[x+1])
{
array[x+1] = (int)(Math.random()*50) + 1;
Arrays.sort(array);
x=0;
}
}
As a result, the program always compare the first 2 elements only.
Compare array[0] and array[1], they are equal.
Reset x = 0
Compare array[0] and array[1], they are equal.
Reset x = 0
Compare array[0] and array[1], they are equal.
Reset x = 0
... ...
This causes infinite loop. Make some change and go ahead. :)