Just wanted other opinions on my code and a way to figure out whether or not each number placed into the 2D array is unique. My mind is blanking out a bit, I placed something in the method before but erased it. Debating on whether the parameters should simply be the 2D array by itself, or the array and the temporary number.
AbstractEden 0 Newbie Poster
/****************************************
*File Name: Assignment 4 Part 1
*Purpose: An n x n matrix that is filled with the numbers 1, 2, 3, . . . n^2 is a magic square if the sum of
*the elements in each row, in each column, and in the two diagonals is the same value.
* 16 3 2 13
* 5 10 11 8
* 9 6 7 12
* 4 15 14 1
*Write a program that generates 16 values between 1 and 16 (no duplicates allowed) and tests whether they form
*a magic square when put into a 4 X 4 array. You need to test two features:
* Does each of the numbers 1, 2, . . ., 16 occur in the user input?
* When the numbers are input into a square, are the sums of the rows, columns, and diagonals equals to each other?
*Programmer: Shanel Fowler
*Last Updated Date: 3/16/14
****************************************/
import java.util.*;
public class Assignment_4_01
{
boolean Duplicate = false; //Creates a boolean variable that stores.
static final int ROW = 4; //Creates an integer for the number of rows.
static final int COL = 4; //Creates an integer for the number of columns.
/**
*Method will determine whether or not there are any duplicates within the
*matrix. If the number is randomly generated again, another will take its
*place within the designated range.
*/
public static boolean isDuplicate(int[][] value)
{
for(int a = 0; a < value.length; a++)
{
for(int b = 0; b < value.length; b++)
{
boolean repeat = false;
int num;
while(repeat)
{
}
}
}
}
public static void main(String[] args)
{
int [][] squareArray = new int[ROW][COL]; //Creates a two dimensional array to store values.
int minNum = 1; //Creates an integer set to the minimum value.
int maxNum = 16; //Creates an integer set to the maximum value.
Random randNum = new Random(); //Creates random variable.
//Allocates a random value into each column and row in the 4 X 4 matrix.
for(int r = 0; r < ROW; r++)
{
for(int c = 0; c < COL; c++)
{
int tempVal = randNum.nextInt(maxNum - minNum + 1) + minNum;
squareArray[r][c] = tempVal;
}
}
for(int i = 0; i < squareArray.length; i++)
{
for(int x = 0; x < squareArray.length; x++)
{
System.out.print(squareArray[i][x]);
System.out.print(" ");
}
System.out.println();
}
}
}
cgeier 187 Junior Poster
Place numbers 1-16 in an ArrayList:
//holds numbers 1-16
ArrayList uniqueNums = new ArrayList();
//add numbers 1-16
for (int i=0; i < 16; i++)
{
uniqueNums.add(i+1);
}//for
Use the following random number generator method. It was taken from here.
randInt:
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
"randInt" is used below.
Generate unique random numbers like this:
//choose random int
for (int j = 15; j >= 0; j--)
{
int selectedNum = randInt(0,j);
//i print out the selected number.
//you will add it to your matrix here
System.out.println("selected: " + uniqueNums.get(selectedNum));
//after a number is added to the
//matrix, remove it from
//the arraylist
uniqueNums.remove(selectedNum);
System.out.println();
}//for
You can add your numbers to the matrix where I have the "System.out.println...." statement.
Edited by cgeier
AbstractEden 0 Newbie Poster
Thanks a lot, I'll definitely make note of this whenever I run into the problem again.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.