0

I'm trying to output a square of X's using an array. The diagonals of the square will be filled with 'X' and the empties will be filled with spaces '_'.

Here's the code I got:

public static char[][] square(int z) {
    int size=5;
    char[][] myArr = new char[size][size];
    for (int c=0;c<size;c++)
        myArr[c][c]='X';
    for (int r=0;r<size;r++)
    {
        for (int col=size-1;col>=0;col--)//put X
        {
            myArr[r][col]='X';
        }
    }
    for(int count=0;count<size;count++){
        if (myArr[count][count]!='X')
            myArr[count][count]=' ';
    }

    return myArr;

}

This should be working-I ran it manually on paper and everything should have been fine. What can the problem be?

5
  • 1
    if everything should have been fine, what wasn't fine? as in, what error message did you get? Commented Sep 26, 2010 at 19:58
  • looking at the code it could be a few things :) Commented Sep 26, 2010 at 20:02
  • Not really related to the problem you ask (as I think eumiro posted the correct solution), but I'm just curious, are you actually using the parameter "int z" in the function at all? Commented Sep 26, 2010 at 20:08
  • Mind telling me what isn't working or better what exactly you're expecting? Commented Sep 26, 2010 at 20:12
  • Expected: X---X (where - is a space) Actual: XXXXX Commented Sep 26, 2010 at 20:21

4 Answers 4

4

The problem is probably, that here:

for (int r=0;r<size;r++)
{
    for (int col=size-1;col>=0;col--)//put X
    {
        myArr[r][col]='X';
    }
}

you are itering over the whole square (size * size) and not just drawing the Northeast - Southwest diagonal.

Try to replace it with this:

for (int r=0;r<size;r++)
{
    myArr[r][size - r] = 'X'
}

EDIT: To make your code little bit compacter:

public static char[][] square(int size) {
    char[][] myArr = new char[size][size];
    for (int c = 0; c < size; c++) {
        for (int r = 0; r < size; r++) {
            if ((c == r) || ( c == size - r)) {
                myArr[r][c] = 'X';
            } else { 
                myArr[r][c] = ' ';
            }
        }
    }
    return myArr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This still only covers one diagonal- the easy one where the rows and columns numbers are equal. What about the right to left diagonal where the row and column numbers are not the same?
Isn't ((c == r) || ( c == size - r)) covering both diagonals?
1

When you Iterate your loop, you want the diagonals to be 'x'. You can save a lot of work and code by minimizing how much you iterate.

public static char[][] square(int z) {
int size = z;
char[][] myArr = new char[size][size];

for(int i = 0;i < size;i++)
{
    for(int j = 0;j < size;j++)
    {
        if(i == j)
        {
             myArr[i][j] = 'X';
        }
        else if(i + j == size - 1)
        {
             myArr[i][j] = 'X';
        else
        {
             myArr[i][j] = " ";
        }
     }
}

return myArr;

}

3 Comments

This is only the right to left diagonal, what about the left to right diagonal, where i!=j?
Also, your not even using the parameter 'z'. We could eliminate that as well. I'm not quite sure what your challenging us on, but I think that function just creates the array you described, if we want to be creative we could just use x as the dimensions of the square and have an x,x sized square with 'X' in the middle! =)
I had it, but you still gota subtract i + j = size - 1;
0

You code seems a little bit overcomplicated with all those ifs and clever loops.
Just fill array with 'background chars' and then draw diagonal.

    char[][] myArr = new char[size][size];
    for (int i = 0; i < size; ++i) {
        Arrays.fill(myArr[i], ' ');
    }
    // now we have square filled with spaces

    // draw diagonal, like you did it
    for (int c = 0; c < size; c++) {
        myArr[c][c] = 'X';
        myArr[c][size - c - 1] = 'X';
    }

edit
Updated to draw two diagonals.

10 Comments

I dont see what you did there..myArr[i]=new char[size]..what was that for? Also when you use Arrays.fill(myArr[i], ' '), how come you only specify one dimension and not two?
@Nikita Rybak: The code you posted still only draws one diagonal (from NW corner to SE corner). You still need to draw a diagonal from NE to SW corners. Also, why initialize the array values with "background chars"? Besides adding overhead, I can't think of anything else it would do.
@SauceMaster The code you posted still only draws one diagonal (from NW corner to SE corner) I mentioned it and said why in my answer.
I thought I was drawing two? Theres the first loop where the dimensions are the same [c][c], which is the left to right loop. Then there is the other nested loop which draws the right to left loop, no?
@SauceMaster Besides adding overhead Are you saying it's a waste of resources to make N^2 + 2*N assignments instead of N^2? :) I just did what produces simpler code.
|
0

First problem is that when you do new char[size][size]; you still should new each char array before you use it.

This can be merged with the first for loop easily:

 for (int c=0;c<size;c++) {
    myArr[c] = new char[size]; // allocate the array
    myArr[c][c]='X';
 }

Next the algorithm doesn't work, though this one would:

public static char[][] square (int z) {
    int size = z, cap=((size+1) /2);
    char[][] myArr = new char[size][size];
    for (int c = 0; c < cap; c++) { // iterate only half the array doing 4 positions per iteration
        myArr[c] = new char[size];
        myArr[size-c-1] = new char[size];
        Arrays.fill(myArr[size-c-1],' '); // make the new line blank
        Arrays.fill(myArr[c], ' '); // make the new line blank
        myArr[c][c] = 'X';  // top left to center
        myArr[size - c - 1][size-c-1] = 'X'; // bottom right to center
        myArr[size - c - 1][c] = 'X'; // bottom left to center
        myArr[c][size - c - 1] = 'X'; // top right to center
    }
    return myArr;
}

/** test square algorithm by printing it to System.out */
private static void testSquare () {
    char[][] sq = square(5);
    for (char[] l : sq) {
        System.out.println(new String(l));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.