Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 27th, 2004, 11:33 AM   #1
D4Z
Newbie
 
Join Date: Aug 2004
Posts: 1
Rep Power: 0 D4Z is on a distinguished road
first of all, this is a school problem. It's not mandatory to solve, but it's driving me crazy. We're supposed to take an integer input from a file and display it in a matrix, with a 1 in the center, for example if the input was 3 we should have a matrix like this

9 8 7
2 1 6
3 4 5


I'm having trouble doing this. Here's the code I have.

import java.io.*;
import java.util.*;

public class Shootout
{
	public static void main(String[]args) throws Exception
	{
 Scanner keyboard = new Scanner(new File("pr92.dat"));
 int side = keyboard.nextInt();
 int matrix[][] = new int [side][side];
 int maxLength = side;
 int maxWidth = side;
 int direction = 0;
 int max = side^2;
 int current=0;
 
 
 for(int i=((side+1)-maxLength); i<maxLength; i++)
 {
 	
 	for(int j=((side+1)-maxWidth); j<maxWidth; j++)
 	{
  if(direction==0)
  {
  	for(int k = 0; k<side; k++)
  	{
   matrix[i][j] = max-j;
   current = max-k;
  	}
  	
  	direction = 2;
  	maxWidth--;
  }
  
  if(direction==1)
  {
  	for(int k = 0; k<side; k++)
  	{
   matrix[i][j] = current;
   current--;
  	}
  	
  	direction = 2;
  	maxWidth--;
  }
  
  if(direction==2)
  {
  	for(int k = 0; k<side; k++)
  	{
   matrix[j][i]=current;
   current--;
  	}
  }
  
  direction=1;
  maxWidth--;
  
 	//	maxWidth--;
 	}
 	
 	maxLength--;
 }
 
 for(int i = 0; i<side; i++)
 {
 	for(int j=0; j<side; j++)
 	{
  System.out.print(matrix[i][j] + "\t");
 	}
 	
 	System.out.println();
 }
	
	}

}


Please go easy on me.
D4Z is offline   Reply With Quote
Old Aug 27th, 2004, 4:53 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
This is called a Ulam Spiral... if you want more graphical representation or a few docs to read, check it out via google.

Here are two Java classes to handle this... Enjoy.


import java.awt.*;

public final class SpiralGenerator extends Object {
	static final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
	int count, remain, distance, direction;

	SpiralGenerator() {
 count = 1;
 remain = 2;
 distance = 1;
 direction = RIGHT;
 }

	public void generate(int number, Point point) {
 int dx = 0, dy = 0;

 for (; count <= number; count++) {
 	if (--remain == 0) {
  switch (direction) {
  	case UP: distance++; direction = LEFT; break;
  	case DOWN: distance++;
  	default: direction--; break;
  	}
  remain = distance;
  }
 	switch (direction) {
  case LEFT: --dx; break;
  case RIGHT: ++dx; break;
  case UP: --dy; break;
  case DOWN: ++dy; break;
  }
 	}

 point.translate(dx, dy);
 }
	}

public final class PrimeGenerator extends Object {
	int number, index, primes[], squares[];

	PrimeGenerator() {
 index = 0;
 number = 1;
 primes = new int[1];
 squares = new int[1];
 }

	public int generate() {
 if (number > 2) {
loop:
 	while(true) {
  number += 2;
  for (int i = 1; i < index && number >= squares[i]; i++) {
  	if ((number % primes[i]) == 0) continue loop;
  	}
  break loop;
  }
 	}
 else number++;

 squares[index] = (primes[index] = number) * number;

 if (++index == primes.length) reallocate();

 return number;
 }

	void reallocate() {
 int array[];

 System.arraycopy(primes, 0, (array = new int[primes.length * 2]), 0, primes.length);
 primes = array;

 System.arraycopy(squares, 0, (array = new int[squares.length * 2]), 0, squares.length);
 squares = array;
 }
	}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC