Java Matrix problem please help

I can't get the desired output

Page 1 of 1

5 Replies - 2799 Views - Last Post: 07 February 2010 - 06:33 PM Rate Topic: -----

#1 lucka  Icon User is offline

  • New D.I.C Head
  • member icon

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

Java Matrix problem please help

Posted 10 August 2009 - 08:43 PM

why isn't the desired out put coming?
please help

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;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	   
			
				System.out.println("Enter an integer as order :");
				n=Integer.parseInt(br.readLine());
			
		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
  • +

Replies To: Java Matrix problem please help

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java Matrix problem please help

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?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Java Matrix problem please help

Posted 11 August 2009 - 06:02 AM

You are SO close. Just put "botrow--;" in the appropriate place and you'll be fine.
Was This Post Helpful? 0
  • +
  • -

#4 lucka  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: Java Matrix problem please help

Posted 11 August 2009 - 09:33 AM

View Postbaavgai, 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
:D
Was This Post Helpful? 0
  • +
  • -

#5 Guest_nami*


Reputation:

Re: Java Matrix problem please help

Posted 07 February 2010 - 06:17 PM

View Postlucka, on 10 August 2009 - 07:43 PM, said:

why isn't the desired out put coming?
please help

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;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	   
			
				System.out.println("Enter an integer as order :");
				n=Integer.parseInt(br.readLine());
			
		SpiralMatrix obj=new SpiralMatrix(n);
		obj.spiralFill();
		obj.matrixPrint();
	}
}//end of the class   


don't mind the spacing
regards

Was This Post Helpful? 0

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Java Matrix problem please help

Posted 07 February 2010 - 06:33 PM

Wow. Necroposting just to quote the OP. Fail. :spam:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1