Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print out an array in a special format?

Status
Not open for further replies.

wangdong

Programmer
Oct 28, 2004
202
CN
I have a two dimension integer array:

int number[][] = {{1,2,3},{4,5,6},{7,8,9}};

Now, I want to print out in this format:

1 2 3
8 9 4
7 6 5

Has anyone got any idea on how to do it?

Chinese Java Faq Forum
 
I can't see the rule which leads
from
1 2 3
4 5 6
7 8 9
to
1 2 3
8 9 4
7 6 5
Is there any?

For an individual mapping of fixed positions you could use:
Code:
System.out.println (
number [0][0] + ... + number [0][2] + "\n"  
+ number [2][1] + ... + number [1][0] + "\n" 
+ number [2][0] + ... + number [1][2]);

seeking a job as java-programmer in Berlin:
 
As stefan said, I think we need to know the logic behind the new positions to give you a solution.

Personally, I don't understand how comes the second matrix, but now that you say it's dynamical, I'm completeley lost.

Cheers,
Dian
 
But we don't understand the rules which transform the array's data into the ordering you are after. You need to give us these rules. If the rules are fluid, what constraints exist in how they can change.

Maybe if you told us why the data needs to be in the format you're wanting? What is it for?

Help us to help you.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
the array will be displayed in a clockwise screwy format. the number of rows and colums are always the same (3x3, 4x4....). The following example is a 6x6 array displayed in the format.

1 2 3 4 5 6
20 21 22 23 24 7
19 32 33 34 25 8
18 31 36 35 26 9
17 30 29 28 27 10
16 15 14 13 12 11


Chinese Java Faq Forum
 
Ahhhh. So the numbers are layed-out in clockwise 'corkscrew' fashion within the new grid.

Out of interest, what application does this kind of transformation have?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
.. or maybe the term 'spiral' is a better one. Anyway, I've never come across an algorithm which does this. I'll try to ponder on this over the weekend.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
One thing though. Is the the positions in the source matrix which get transformed to the new locations, or is it the data values?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
okay.. assuming you're tranforming the array positions, I think this can be solved using recursion. Don't have the time right now to try and implement this though.

If n is the grid size, then the heart of the recursion implements the cases for n=1 and n=2, and then odd n's recurse down in descending odd sequence to the n=1 case, and even n's recurse down in descending even sequence to the n=2 case. The outer-most part of the spiral is done first and is trivial.

Can't be sure if this nails it without trying it though.



Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Are you talking to yourself tim?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I was feeling lonely [smile].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
LOL. Thanks, sedj. I'm feeling better now. [bigsmile]

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Well - I needed a similar algorithm about 10 years ago, but in the scope of graphics - evaluating the color of pixels, and starting from the center.

To get the spin, you increase x (stepping right), increase y (stepping down), decrease x (stepping left), decrease y (stepping up) and increase x again and so on.

The width you step is (for 6x6) 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1 - an easy, regular pattern.

Combine both, and you solved your problem.

seeking a job as java-programmer in Berlin:
 
Code:
asux ->~/proj/mini/forum > java Spirale 20
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 21
 75 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 95 22
 74 143 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 161 96 23
 73 142 203 256 257 258 259 260 261 262 263 264 265 266 267 268 219 162 97 24
 72 141 202 255 300 301 302 303 304 305 306 307 308 309 310 269 220 163 98 25
 71 140 201 254 299 336 337 338 339 340 341 342 343 344 311 270 221 164 99 26
 70 139 200 253 298 335 364 365 366 367 368 369 370 345 312 271 222 165 100 27
 69 138 199 252 297 334 363 384 385 386 387 388 371 346 313 272 223 166 101 28
 68 137 198 251 296 333 362 383 396 397 398 389 372 347 314 273 224 167 102 29
 67 136 197 250 295 332 361 382 395 400 399 390 373 348 315 274 225 168 103 30
 66 135 196 249 294 331 360 381 394 393 392 391 374 349 316 275 226 169 104 31
 65 134 195 248 293 330 359 380 379 378 377 376 375 350 317 276 227 170 105 32
 64 133 194 247 292 329 358 357 356 355 354 353 352 351 318 277 228 171 106 33
 63 132 193 246 291 328 327 326 325 324 323 322 321 320 319 278 229 172 107 34
 62 131 192 245 290 289 288 287 286 285 284 283 282 281 280 279 230 173 108 35
 61 130 191 244 243 242 241 240 239 238 237 236 235 234 233 232 231 174 109 36
 60 129 190 189 188 187 186 185 184 183 182 181 180 179 178 177 176 175 110 37
 59 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 38
 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39
asux ->~/proj/mini/forum > java Spirale 3
 1 2 3
 8 9 4
 7 6 5
looks nice, doesn't it?

seeking a job as java-programmer in Berlin:
 
I solved it using recursion:-

Code:
public class Spiral {

	public static void main(String[] args) {
		Spiral spiral = new Spiral();
		for(int n = 1; n < 11; n++){
			spiral.print( spiral.transform(spiral.prepareInput(n)));
		}
	}
	
	public int[][] transform( int[][] matrix ){
		//unpack the 2-dimensional array into 1 dimension for easy traversal
		int size = matrix.length;
		int elements = size * size;
		int[] input = new int[elements];
		for (int i=0; i<elements; i++){
			input[i] = matrix[(i/size)%size][i%size];
		}
		int[][] output = new int[size][size];
		transform(input, output, 0, 0, 0, size);
		return output;
	}
	
	private void transform( int[] input, int[][] output, int inputIndex, int ox, int oy, int size ){
		if ( size == 0 ) return; //done
		if ( size == 1 ){
			output[ox][oy] = input[inputIndex];
			return;
		}
		//apply outside spiral
		for (int i=0; i<size; i++){
			output[oy][ox+i]=input[inputIndex+i];
			output[oy+size-1][ox+size-1-i]=input[inputIndex+size+size-2+i];
		}
		for (int i=0; i<size-2; i++){
			output[oy+i+1][ox+size-1] = input[inputIndex+size+i];
			output[oy+size-2-i][ox] = input[inputIndex+size+size+size-2+i];
		}
		//Recurse to fill in inner spiral
		transform( input, output, inputIndex+(size*4)-4, ox+1, oy+1, size-2);
	}

	private void print(int[][] data){
		System.out.println("n=" + data.length);
		for( int i=0; i<data.length; i++ ){
			for ( int j=0; j<data.length; j++ ){
				System.out.print( data[i][j] + " ");
			}
			System.out.println("");
		}
		System.out.println("");
	}

	private int[][] prepareInput(int size){
		int elements = size * size;
		int[][] data = new int[size][size];
		for (int i=0; i<elements; i++){
			data[(i/size)%size][i%size] = i+1;
		}
		return data;
	}
}

This can probably be made more efficient. A task left to you, I think.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I remapped the data onto a new array and then printed the new array out.
It will also work with matrices that are rectangular rather than just square ones

Code:
class spiral
{  
    public static void main(String args[])
    {
        int rowLimit = 3;
        int colLimit = 3;

        int source[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

        /*int rowLimit = 4;
        int source[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};*/

        int[][] remap = new int[rowLimit+1][colLimit+1];

        for (int i = 0; i <= rowLimit; i++)
        {
            for (int j = 0; j <= colLimit; j++)
            {
                remap[i][j] = 99;
            }
        }

        int urow = rowLimit;
        int ucol = colLimit;
        int lrow = 1;
        int lcol = 0;
        int colIndex = -1;
        int rowIndex = 0;

        int a = 0; //across
        int b = 1; //back
        int u = 2; //up
        int d = 3; //down

        int udba = a;

        for (int i = 0; i <= rowLimit;i++)
        {
            for (int j = 0; j <= colLimit;j++)
            {
                if (udba == a)
                {
                    if (colIndex == ucol-1)
                    {
                        udba = d;
                        ucol--;
                    }
                    colIndex++;
                }
                else if (udba == d)
                {
                    if (rowIndex == urow-1)
                    {
                        udba = b;
                        urow--;
                    }
                    rowIndex++;
                }
                else if (udba == b)
                {
                    if (colIndex == lcol+1)
                    {
                        udba = u;
                        lcol++;
                    }
                    colIndex--;
                }
                else if (udba == u)
                {
                    if (rowIndex == lrow+1)
                    {
                        udba = a;
                        lrow++;
                    }
                    rowIndex--;
                }
                //System.out.println("row = "+i+"|col = "+j+"|rowIndex = "+rowIndex+"|colIndex = "+colIndex+"|/n");
                if (rowIndex >= 0 && rowIndex <= rowLimit && colIndex >= 0 && colIndex <= colLimit)
                    remap[rowIndex][colIndex] = source[i][j];
            }    
        }

        for (int i = 0; i<= rowLimit;i++)
        {
            String output = "";
    
            for (int j = 0; j<= colLimit;j++)
            {
                if (j == 0)
                    output = output = output + remap[i][j];
                else 
                    output = output = output + "," + remap[i][j];
            }
            System.out.println(output);
        }
    }
}


"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Ah yes - the code...
Code:
public class Spirale
{
	public static final int EAST = 0;
	public static final int SOUTH = 1;
	public static final int WEST = 2;
	public static final int NORTH = 3;

	int target [][] ;
	/** */
	public Spirale (String param)
	{
		int len = Integer.parseInt (param);
		spiralfill (len);
		printArray (len);
	}

	public void spiralfill (int len)
	{
		int dir = EAST;
		int step = 1;
		int width = len;
		int x = -1, y = 0;
		target = new int [len][len];
		int count = 0;
		do
		{
			switch (dir)
			{
				case EAST:	++x; break;
				case SOUTH:	++y; break;
				case WEST:	--x; break;
				case NORTH:	--y; break;
			}
			++count;
			target [y][x] = count;
			// System.out.println ("x = " + x + "\ty = " + y + "\tc = " + count);
			--width;
			if (width == 0)
			{
				++dir;
				if (dir == 4) dir = EAST;
				++step;
				if (step == 2)
				{
					--len;
					step = 0;
				}
				width = len;
			}
		} while (len > 0);
	}

	public void printArray (int len)
	{
		for (int i = 0; i < len; ++i)
		{
			for (int j = 0; j < len; ++j)
			{
				System.out.print (" " + target [i][j]);
			}
			System.out.println ();
		}
	}

	/** */
	public static void main (String args[])
	{
		String param = "6";
		if (args.length == 1)
		{
			param = args[0];
		}
		new Spirale (param);
	}
}

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top