2.5. Rigourous code.   

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();
:

Notice now though that the program is not rigorous in that a person can erroneously enter a value of 20 for the size of the array. This would cause a fatal run time error. Since we do not want fatal errors in large programs (or small ones) we will make our algorithm stricter.

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();
:

What form does the error routine take? It could be an ASSERT routine but this may produce a fatal error message and terminate the program; something we are trying to avoid. A better form of control is via a loop mechanism. We keep asking-testing the user until the response is correct.

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");
:

Notice that this is about the best form of loop error recovery and is applicable to most forms of requesting/processing a response. Sometimes it may be convenient to just issue a message and exit the program but it is often nicer to give the user a second chance in case the error in entering the response was just a slight mental aberration.

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

Our output would now be zero, one or more occurrences of the above message. If we had no such statement, then we would know that our array was symmetric. This, though, is hardly the nice result we are after. It solves the problem but not nicely.

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 row  1 to size-1 do

for
column row+1 to size do
/* set symmetric */

For a 5x5 array we now require 10 comparisons instead of 25.


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.

We now need to use our knowledge of how a for loop can be changed into a while loop. How would you rewrite the above in pseudo code using a while loop and including the above termination conditions.

Try to write the while loop equivalent before looking at the solution here.

Don't Cheat.