import java.util.Scanner; public class ltnguyen_Magic { public static void main
ID: 3568100 • Letter: I
Question
import java.util.Scanner;
public class ltnguyen_Magic
{
public static void main ( String args[] )
{
Scanner in = new Scanner (System.in);
int[][] square = new int [4][4];
int sqlength = square.length;
int magicValue = sqlength * (sqlength * sqlength + 1) / 2;
boolean magical;
System.out.printf ("The magic value for your square is %d, which means that every row, column and diagonal of your square must add up to that number. ", magicValue);
for (int r = 0; r < square.length; r ++)
{
System.out.printf ("Please enter the 4 values for row %d, separated by spaces: ", r);
square [r][0] = in.nextInt();
square [r][1] = in.nextInt();
square [r][2] = in.nextInt();
square [r][3] = in.nextInt();
}
if ( checkDiagonals(square) && checkRows(square) && checkColumns(square) && checkRange(square) )
{
System.out.println ("MAGIC: YES");
}
else
{
System.out.println ("MAGIC: NO");
}
}
public static boolean checkRows ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum;
boolean valid = true;
System.out.print ("ROWS: ");
for ( int r = 0; r < theSquare.length; r ++ )
{
sum = 0;
for ( int c = 0; c < theSquare[r].length; c++ )
{
sum += theSquare[r][c];
}
if ( sum != magic )
{
System.out.printf ("%d ", r);
valid = false;
}
if ( r == theSquare.length - 1 && valid == true )
{
System.out.println ("VALID");
}
else if ( r == theSquare.length - 1 && valid == false )
{
System.out.println ("");
}
}
return valid;
}
public static boolean checkColumns ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum;
boolean valid = true;
System.out.print ("COLS: ");
for ( int c = 0; c < theSquare.length; c ++ )
{
sum = 0;
for ( int r = 0; r < theSquare[c].length; r++ )
{
sum += theSquare[r][c];
}
if ( sum != magic )
{
System.out.printf ("%d ", c);
valid = false;
}
if ( c == theSquare.length - 1 && valid == true )
{
System.out.println ("VALID");
}
else if ( c == theSquare.length - 1 && valid == false )
{
System.out.println ("");
}
}
return valid;
}
public static boolean checkDiagonals ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum = 0, c = 0, sum1 = 0;
boolean valid = true;
System.out.print ("DIAG: ");
for ( int r = 0; r < theSquare.length; r ++ )
{
sum += theSquare[r][c];
c++;
}
c--;
for ( int r = 3; r >= 0; r -- )
{
sum1 += theSquare[r][c];
c--;
}
if ( sum != magic || sum1 != magic)
{
if ( sum != magic )
{
System.out.print ("0 ");
valid = false;
}
if ( sum1 != magic )
{
System.out.print ("1");
valid = false;
}
}
else
{
System.out.print ("VALID");
}
System.out.println ("");
return valid;
}
public static boolean checkRange ( int theSquare[][] )
{
int[] range = new int [theSquare.length * theSquare.length];
int arrayFiller = 0;
boolean valid = true;
for ( int r = 0; r < theSquare.length; r ++ )
{
for ( int c = 0; c < theSquare[r].length; c++ )
{
range[arrayFiller] = theSquare[r][c];
arrayFiller++;
}
}
System.out.print ("RANG: ");
for ( int i = 0; i < range.length; i++ )
{
for ( int r = i+1; r <range.length; r++)
{
if ( range[i] == range[r] || (range[i] < 1 || range[i] > theSquare.length * theSquare.length))
{
System.out.printf ("%d ", range[r]);
valid = false;
}
}
}
if ( valid == true )
{
System.out.print ("VALID");
}
System.out.println ("");
return valid;
}
}
This is what the output suppose to look like
The magic value for your square is 34, which means that every row, column
and diagonal of your square must add up to that number.
Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9
Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: YES
but this is what the program show
The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.
Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 1 6 9
DIAG: VALID
ROWS: 3
MAGIC: NO
i need help fixing this program thanks
Explanation / Answer
import java.util.Scanner;
public class ltnguyen_Magic
{
public static void main ( String args[] )
{
Scanner in = new Scanner (System.in);
int[][] square = new int [4][4];
int sqlength = square.length;
int magicValue = sqlength * (sqlength * sqlength + 1) / 2;
boolean magical;
System.out.printf ("The magic value for your square is %d, which means that every row, column and diagonal of your square must add up to that number. ", magicValue);
for (int r = 0; r < square.length; r ++)
{
System.out.printf ("Please enter the 4 values for row %d, separated by spaces: ", r);
square [r][0] = in.nextInt();
square [r][1] = in.nextInt();
square [r][2] = in.nextInt();
square [r][3] = in.nextInt();
}
if ( checkDiagonals(square) && checkRows(square) && checkColumns(square) && checkRange(square) )
{
System.out.println ("MAGIC: YES");
}
else
{
System.out.println ("MAGIC: NO");
}
}
public static boolean checkRows ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum;
boolean valid = true;
System.out.print ("ROWS: ");
for ( int r = 0; r < theSquare.length; r ++ )
{
sum = 0;
for ( int c = 0; c < theSquare[r].length; c++ )
{
sum += theSquare[r][c];
}
if ( sum != magic )
{
System.out.printf ("%d ", r);
valid = false;
}
if ( r == theSquare.length && valid == true )
{
System.out.println ("VALID");
}
else if ( r == theSquare.length && valid == false )
{
System.out.println ("");
}
}
return valid;
}
public static boolean checkColumns ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum;
boolean valid = true;
System.out.print ("COLS: ");
for ( int c = 0; c < theSquare.length; c ++ )
{
sum = 0;
for ( int r = 0; r < theSquare[c].length; r++ )
{
sum += theSquare[r][c];
}
if ( sum != magic )
{
System.out.printf ("%d ", c);
valid = false;
}
if ( c == theSquare.length - 1 && valid == true )
{
System.out.println ("VALID");
}
else if ( c == theSquare.length - 1 && valid == false )
{
System.out.println ("");
}
}
return valid;
}
public static boolean checkDiagonals ( int theSquare[][] )
{
int length = theSquare.length;
int magic = length * (length * length + 1) / 2;
int sum = 0, c = 0, sum1 = 0;
boolean valid = true;
System.out.print ("DIAG: ");
for ( int r = 0; r < theSquare.length; r ++ )
{
sum += theSquare[r][c];
c++;
}
c--;
for ( int r = 3; r >= 0; r -- )
{
sum1 += theSquare[r][c];
c--;
}
if ( sum != magic || sum1 != magic)
{
if ( sum != magic )
{
System.out.print ("0 ");
valid = false;
}
if ( sum1 != magic )
{
System.out.print ("1");
valid = false;
}
}
else
{
System.out.print ("VALID");
}
System.out.println ("");
return valid;
}
public static boolean checkRange ( int theSquare[][] )
{
int[] range = new int [theSquare.length * theSquare.length];
int arrayFiller = 0;
boolean valid = true;
for ( int r = 0; r < theSquare.length; r ++ )
{
for ( int c = 0; c < theSquare[r].length; c++ )
{
range[arrayFiller] = theSquare[r][c];
arrayFiller++;
}
}
System.out.print ("RANG: ");
for ( int i = 0; i < range.length; i++ )
{
for ( int r = i+1; r <range.length; r++)
{
if ( range[i] == range[r] || (range[i] < 1 || range[i] > theSquare.length * theSquare.length))
{
System.out.printf ("%d ", range[r]);
valid = false;
}
}
}
if ( valid == true )
{
System.out.print ("VALID");
}
System.out.println ("");
return valid;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.