Page 1 of 1

## 5 Replies - 3023 Views - Last Post: 07 February 2010 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=119527&amp;s=dd3a12b3b8a69a56d4414c628e2b0af3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lucka

Reputation: 0
• Posts: 9
• Joined: 24-March 09

Posted 10 August 2009 - 08:43 PM

why isn't the desired out put coming?

the program details are as follows

Write a program to input the order of square matrix and fill the matrix in the spiral way

example

order=5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

the code i have written is as follows

```//Fills the matrix in the spiral order
import java.io.*;
public class SpiralMatrix
{
//instance variables
int m[][];
int order;

public SpiralMatrix(int n)
{
order=n;
m=new int[order][order];

}

public void spiralFill()
{
int leftcol,rightcol,toprow,botrow;
leftcol=toprow=0;
rightcol=botrow=order-1;
int n=1;
do
{
//filling toprow from left to right.
for(int j=leftcol;j<=rightcol;j++)
{
m[toprow][j]=n++;
}
toprow++;
//filling right side from top downwards
for(int i=toprow;i<=botrow;i++)
{
m[i][rightcol]=n++;
}
rightcol--;
//filling botrow backward from right to left
for(int j=rightcol;j>=leftcol;j--)
{
m[botrow][j]=n++;
}
//filling leftcol upwards
for(int i=botrow;i>=toprow;i--)
{
m[i][leftcol]=n++;
}
leftcol++;
}
while(n<=order*order);
}
public void matrixPrint()
{
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[]args)throws IOException
{
int n=0;

System.out.println("Enter an integer as order :");

SpiralMatrix obj=new SpiralMatrix(n);
obj.spiralFill();
obj.matrixPrint();
}
}//end of the class
```

don't mind the spacing
regards

Is This A Good Question/Topic? 0

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

Posted 10 August 2009 - 09:12 PM

the problem is with the fill or with the output??..if with the output then how do want the output to look like?

### #3 baavgai

• Dreaming Coder

Reputation: 6379
• Posts: 13,629
• Joined: 16-October 07

Posted 11 August 2009 - 06:02 AM

You are SO close. Just put "botrow--;" in the appropriate place and you'll be fine.

### #4 lucka

Reputation: 0
• Posts: 9
• Joined: 24-March 09

Posted 11 August 2009 - 09:33 AM

baavgai, on 11 Aug, 2009 - 05:02 AM, said:

You are SO close. Just put "botrow--;" in the appropriate place and you'll be fine.

where?
well i am giving it a shot
thank you

### #5 Guest_nami*

Reputation:

Posted 07 February 2010 - 06:17 PM

lucka, on 10 August 2009 - 07:43 PM, said:

why isn't the desired out put coming?

the program details are as follows

:rolleyes:

Write a program to input the order of square matrix and fill the matrix in the spiral way

example

order=5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

the code i have written is as follows

```//Fills the matrix in the spiral order
import java.io.*;
public class SpiralMatrix
{
//instance variables
int m[][];
int order;

public SpiralMatrix(int n)
{
order=n;
m=new int[order][order];

}

public void spiralFill()
{
int leftcol,rightcol,toprow,botrow;
leftcol=toprow=0;
rightcol=botrow=order-1;
int n=1;
do
{
//filling toprow from left to right.
for(int j=leftcol;j<=rightcol;j++)
{
m[toprow][j]=n++;
}
toprow++;
//filling right side from top downwards
for(int i=toprow;i<=botrow;i++)
{
m[i][rightcol]=n++;
}
rightcol--;
//filling botrow backward from right to left
for(int j=rightcol;j>=leftcol;j--)
{
m[botrow][j]=n++;
}
//filling leftcol upwards
for(int i=botrow;i>=toprow;i--)
{
m[i][leftcol]=n++;
}
leftcol++;
}
while(n<=order*order);
}
public void matrixPrint()
{
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[]args)throws IOException
{
int n=0;

System.out.println("Enter an integer as order :");

SpiralMatrix obj=new SpiralMatrix(n);
obj.spiralFill();
obj.matrixPrint();
}
}//end of the class
```

don't mind the spacing
regards

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,160
• Joined: 27-December 08