In the above we have specified the size of the array by
CONSTants i.e n=4. Notice that we should use this constant
wherever necessary and not use the value 4. Notice also that we
used it in our for loops for loading up the array. If we change
the value of the constant n to say 12, we could now process a
12x12 array. Rememeber as well that nothing stops us from
declaring a 12x12 array and only using a part of it.
static int size,row,column;
static boolean symmetric;
static final int n = 12;
static int a[][] = new int[n][n];
public static void main(String argv[]) throws IOException
{
Format.print(output,"Enter size of array:");
size = input.readInt();
Format.print(output,"Enter array row by row\n");
for(row=0;row<size;row++)
for(column=0;column<size;column++)
a[row][column] = input.readInt();
:
Therefore
(i) Indicate to user the maximum size allowed
(ii) Check to make certain that it is within bounds.
static int size,row,column;
static boolean symmetric;
static final int n = 12;
static int a[][] = new int[n][n];
public static void main(String argv[]) throws IOException
{
Format.print(output,"Enter size of array(<%2d):",n);
size = input.readInt();
if ( (size <=0) || (size >n) ) error ;
else
{
Format.print(output,"Enter array row by row\n");
for(row=0;row<size;row++)
for(column=0;column<size;column++)
a[row][column] = input.readInt();
:
public static void main(String argv[]) throws IOException
{
do
{
Format.print(output,"Enter size of array(<%2d):",n);
size = input.readInt();
if ( (size<=0) || (size>n) )
Format.print("Incorrect size. Please try again.\n");
} while ( (size <=0) || (size >n) );
Format.print(output,"Enter array row by row\n");
:
Another pitfall for beginners is where to put the output
statement. We could put it in the inner loop to avoid using the
variable symmetric: i.e
if a[row,column] != a[column,row] then
output "Not symmetric"
endif
The astute (awake) observer will have seen that we are
performing many redundant calculations since a[i,i] always equals
itself and there is no need to check a[i,j] against a[j,i] and
then later to check a[j,i] against a[i,j].
It is sufficient to simply check each of the elements in the
upper diagonal matrix, comparing each to its mirror element
in the lower diagonal matrix.
To do this we must examine rows 1 to n-1. In a particular row
we must examine each column to the right of the diagonal element
and continue until the end. The diagonal element in row i is
in column i so we start checking at column i+1 and continue
until end. Therefore the heart of our algorithm is now:
for row1 to size-1 do
for columnrow+1 to size do
/* set symmetric */
Our penultimate refinement involves the use of a while loop
instead of the for loop. In our code so far even though symmetric
may be set to false the for loop continues. In this example that
may not be too harsh but when processing large arrays it is more
efficient to terminate abruptly. The while loop allows us to do
that by carefully choosing the termination conditions. Our
termination conditions are
(i) continue while there are more to check
(ii) continue while array still appears symmetric.
Try to write the while loop equivalent before looking at
the solution here.