public void fill(ArrayList<String> a1) {
int i = 0;
while (i < a1.size()) {
if (i == 0) {
for (int j = 0; j < a1.get(i).length(); j++)
crossword[ROWS / 2][(COLUMNS / 4) + j] = a1.get(i)
.charAt(j);
i++;
}
if (i == 1) {
outerloop: for (int t = 0; t < ROWS; t++)
for (int s = 0; s < COLUMNS; s++)
for (int j = 0; j < a1.get(i).length(); j++)
if (crossword[t][s] == a1.get(i).charAt(j)) {
for (int z = 0; z < j; z++)
crossword[t - z - 1][s] = a1.get(i).charAt(
z);
for (int h = j + 1; h < a1.get(i).length(); h++)
crossword[t + h - j][s] = a1.get(i).charAt(
h);
crossword[t][s] = a1.get(i).charAt(j);
break outerloop;
}
i++;
}
}
}
The above is my method to make the first two words of a list of words intersect each other on a crossword puzzle board. My question is for the part:
for (int z = 0; z < j; z++)
crossword[t - z - 1][s] = a1.get(i).charAt(z);
It takes the letters in front of the intersection point and prints them backwards above the intersection row. My brain is overloaded with different things right now and I can't seem to understand how to make the letters go in the right order. I can't attach an image to display my problem but for example the vertical word "throwing" which intersects with horizontal word "clowning" at the letter "o" prints out "rht" before the o (when it should be printing out "thr"). Could someone help? Would be much appreciated!
This will do the trick:
for (int z = 0; z < j; z++)
crossword[t - j + z][s] = a1.get(i).charAt(z);
P.S. This question hasn't been closed yet (even though you did respond to your question). You should flag this answer (or, if you don't feel like giving me points, an answer that you put in) as correct, so that the question doesn't remain in the unanswered section!
Related
Im trying to run a two for loops that iterate over something like colored large square (for example 300x300) and replacing it with the colors(or pixels) of a smaller square (say 100x100) which is what k and l represent.
Essentially, I was wondering how to keep k and l running between 0-100 and restarting once it hits 100, but also keep i and j running the whole time. I tried used 4 nested for loops but it did not work properly, although I'm starting to think that may be a solution, but that would be very inefficient.
Please let me know if this sounds a little confusing I will try my best to explain more clearly, thanks.
excuse the psuedocode
for (i=0; i < width; i++)
for (j=0; i< height; j++)
set(i, j , pixel(k,l));
The problem seems to be to tile an image onto another image. You have width and height for the target image's dimensions, so let's say sourceWidth and sourceHeight are the source image's dimensions (100 by 100, in your example).
To tile an image, you want k == i % sourceWidth and l == j % sourceHeight, so that the pixel you read from the source image "wraps around" the boundaries. These should be invariant conditions. The simplest way to satisfy them is to declare them that way directly:
for(int i = 0; i < width; ++i) {
int k = i % sourceWidth;
for(int j = 0; j < height; ++j) {
int l = j % sourceHeight;
set(i, j, pixel(k, l));
}
}
This does a lot of % operations, which could be quite inefficient, but it gives us a correct piece of code to transform. If we are incrementing k and l, then there are two properties needed to maintain the invariants:
k and i are incremented in unison; and l and j are incremented in unison.
When k reaches sourceWidth it must wrap back to 0, likewise for l and sourceHeight.
for(int i = 0, k = 0; i < width; ++i, ++k) {
if(k == sourceWidth) { k = 0; }
for(int j = 0, l = 0; j < height; ++j, ++l) {
if(l == sourceHeight) { l = 0; }
set(i, j, pixel(k, l));
}
}
Note that the for loop initialiser and stepper now declare and update two variables, for both loops. This is allowed in Java.
This is likely to be faster than doing a lot of % operations, but note that branching with if statements can also slow down an algorithm, so you may want to benchmark it to be sure.
In the special case where source image's dimensions are powers of two, we can achieve the same result with a fast bitwise operation: i % sourceWidth will be equivalent to i & (sourceWidth - 1):
// special case: sourceWidth and sourceHeight are powers of 2
int wMask = sourceWidth - 1, hMask = sourceHeight - 1;
for(int i = 0, k = 0; i < width; ++i, ++k) {
for(int j = 0, l = 0; j < height; ++j, ++l) {
set(i, j, pixel(i & wMask, j & hMask));
}
}
It could be done by something like this:
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
if(k > 100) {
k = 0;
}
if(l > 100) {
l = 0;
}
k++;
l++;
set(i, j , pixel(k,l));
}
}
Iam doing homework and i have problem with two dimensional array. To be specific i have to find highest sum of numbers and write left hand corner and right corner. But how to write condintion when the number is in the corner ?
There is an image https://imgur.com/cHCPDuN
I already tried to start FOR from 1 not from 0 and it works but i want more effective way
int[][] pole = new int[6][6];
Random sc1 = new Random();
int a = 0;
for (int j = 1; j < pole.length - 1; j++) {
for (int i = 1; i < pole.length - 1; i++) {
a = sc1.nextInt(9) + 1;
pole[i][j] = a;
}
if (j == 4) {
for (int j1 = 0; j1 < pole.length; j1++) {
for (int i1 = 0; i1 < pole.length; i1++) {
System.out.print(pole[i1][j1] + " ");
}
System.out.println();
}
}
}
This one works but i had to used this wrong way...
I wrote the following code to print a pyramid, but only one side is being printed.
int k = 7;
int m = 13;
int x = 6;
int y = x;
for (int i = 0; i < k; i++) {
for (int j = 0; j < m; j++) {
if (j < x) System.out.print(" ");
if ((j >= x) && (j <= y)) System.out.print("*");
}
System.out.println();
x++;
y++;
}
This code should print a pyramid starting with 1 asterisk at the top (first row containing the most spaces) and then increment by 2 each time until a pyramid with 7 rows is formed.
In your code x++; should be x--;
I'll summarize it for you:
int k = 7;
int m = 13;
int x = 6;
int y = x;
for (int i = 0; i < k; i++) {
for (int j = 0; j < m; j++) {
if (j<x)
System.out.print(" ");
if (j>=x && j<=y)
System.out.print("*");
}
System.out.println();
x--;
y++;
}
x needs to reduced each loop until the base of the triangle to maintain
the spaces amount.
your x and y are always the same, so this
if ((j >= x) && (j <= y))
will only be true once, when j is exactly = x = y.
I'm missing by just a little bit. What I want:
*******
*****
***
*
***
*****
*******
What I'm getting
*******
*****
***
*
*
***
*****
*******
The code
public class HD404 {
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 1;
for (int i = 0; i < N; i++) {
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
}
Right now I'm mostly just guessing and I just can't pin point my error. What am I missing here?
The problems lays with the second part of your code where you ask to draw one star and you start at zero where you should start at one.
Solution
x = 1;
for (int i = 0; i < N; i++)
should be replaced with
x = 3;
for (int i = 1; i < N; i++)
The problem is that you are starting to draw the bottom of the hourglass with 1 asterisk (x = 1) instead of 3.
The second issue is that the bottom of the hourglass only has N-2 lines, not N-1 so the loop should start at 1 instead of 0. This is because the line with a single asterisk was already drawn in the upper-half.
Corrected code:
public static void main(String[] args) {
int N = StdIn.readInt();
int x = N*2-1;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x-=2;
StdOut.println();
}
x = 3; // <-- not 1 here, the first line has 3 asterisks
for (int i = 1; i < N; i++) { // <-- i starts at 1 because the first line was already drawn in the upper half
for (int j = i; j < N-1; j++) {
StdOut.print(" ");
}
for (int k = 0; k < x; k++) {
StdOut.print("*");
}
x += 2;
StdOut.println();
}
}
As a side-note, you could rewrite this code a lot shorter by making the following observations:
There are x lines to draw so we can loop from 0 to x included (to respect the symmetry) and skip the middle line so as not to draw it twice
For every line, there are x columns to draw and it is either a space or a *.
For every line, * is drawn only if the current column is between min(i, x-i) and max(i, x-i) (if we're in the upper-part, i < x-i and if we're in the bottom-part, i > x-i).
Code:
public static void main(String[] args) {
int N = 4;
int x = 2 * N - 1;
for (int i = 0; i <= x; i++) {
if (i == N) continue; // skip the middle-line for it not to be drawn twice
for (int j = 0; j < x; j++) {
System.out.print(j >= Math.min(i, x-i) && j < Math.max(i, x-i) ? "*" : " ");
}
System.out.println();
}
}
Sample output:
*******
*****
***
*
***
*****
*******
The easiest way I can think up is probably to prevent the last iteration of your first outer loop, that way you'll prevent the first single star line to be shown.
I would probably do it this way:
for(int i = 0; i < N && x > 1; i++)
{
/*Code of the first inner loop*/
}
For those whose still looking for a simpler and lesser code regarding hourglass challenge. This contains 2 for loops only.
You may use this as reference.
public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;
for(int i=0; i<dimension; i++) {
int actual = space;
for (int j=dimension; j > 0; j--) {
if(actual > 0) {
System.out.print(" ");
actual--;
}
else {
System.out.print("*");
if(stars==printed) {
actual = space;
printed = 0;
} else {
actual = 1;
printed++;
}
}
}
if(i <= size-2) { // will pattern spaces and stars from top to middle
space++;
stars--;
}
else { // will pattern spaces and stars from middle to top
space--;
stars++;
}
System.out.println();
}
}
It's a simple matter of flipping an image horizontally and/or vertically. The premise is that given a 2D integer array that was created from importing a picture, I must create a method with a int[][] param and horizontally flip it before returning void.
The syntax is below:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++)
{
for (int j = 0; j < imgArray[i].length / 2; j++)
{
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 - i][j] = temp;
}
}
}
I use imgArray as the array param and use temp as a placeholder while the loop swaps pixels, or rather, that was the intention. Currently the window does nothing after prompting the flip. Can somebody help me find the problem with the logic or syntax?
Thanks in advance, please specify any details I should provide
P.S. I can confirm the unreferenced supplied code is functional and tested.
It is happening because you are using i instead of j. But i will not stop after halfway, but it is continued and re-swap the array.
Here is a correct code :
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length / 2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray.length - 1 - j];
imgArray[i][imgArray.length - 1 -j] = temp;
}
}
Or if you want to swap columns, not rows :
for (int i = 0; i < imgArray.length / 2; i++) {
for (int j = 0; j < imgArray[i].length; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 -i][j] = temp;
}
}
This will correctly flip the image horizontally:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length/2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray[i].length - 1 - j];
imgArray[i][imgArray[i].length - 1 - j] = temp;
}
}
}
Please see my solution below,
for(int i=0; i<matrix.length / 2; i++)
{
int[] row = matrix[i];
int[] temp = row;
matrix[i] = matrix[matrix.length - 1];
matrix[matrix.length - 1] = row;
}