# Array Pattern

### Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.

## ISC Computer Science Specimen Question Paper 2016 (Q3, Array Pattern)

Given a square matrix M [ ] [ ] of order ānā. The maximum value possible for ānā is 10.
Accept three different characters from the keyboard and fill the array according to the instruction given below.

Fill the upper and lower elements formed by the intersection of the diagonals by character 1.
Fill the left and right elements formed by the intersection of the diagonals by character 2.
Fill both the diagonals by character 3.
Output the result in format given below:

## Example 1

```ENTER SIZE       : 4
INPUT            : First Character  '*'
: Second Character  '?'
: Third  Character  '#'
OUTPUT           :
#**#
?##?
?##?
#**#```

## Example 2

```ENTER SIZE       : 5
INPUT            : First Character  '\$'
: Second Character  '!'
: Third  Character  '@'
OUTPUT           :
@\$\$\$@
!@\$@!
!!@!!
!@\$@!
@\$\$\$@
```

## Example 3

```ENTER SIZE       : 65
OUTPUT           : SIZE OUT OF RANGE
```

Notice that the input character is being entered with enclosing single quote.

The java program for the above is as follows:

```import java.util.*;
class Pattern{ //Array Pattern
public static void main( String args[] ){
Scanner sc = new Scanner( System.in );
String temp;
System.out.print( "ENTER SIZE       : " );
int n = sc.nextInt();
temp = sc.nextLine();
if(n>10){
System.out.println( "OUTPUT           : SIZE OUT OF RANGE" );
}else{
char M[][] = new char[n][n];

char c1, c2, c3;
System.out.print( "INPUT            : First Character  " );
temp = sc.nextLine();
c1 = temp.charAt(1);
System.out.print( "                 : Second Character  " );
temp = sc.nextLine();
c2 = temp.charAt(1);
System.out.print( "                 : Third  Character  " );
temp = sc.nextLine();
c3 = temp.charAt(1);
for( int row = 0; row < M.length; row++ ){
for( int col = 0; col < M[row].length; col++ ){
if( col > row && col < M.length - 1 - row) M[row][col] = c1;
else if( col < row && col > M.length - 1 - row) M[row][col] = c1;
else if( row == col || row == M.length - col - 1) M[row][col] = c3;
else M[row][col] = c2;
}
}
System.out.println("OUTPUT           :" );
for( int row = 0; row < M.length; row++ ){
System.out.print( "                  " );
for( int col = 0; col < M[row].length; col++ ){
System.out.print( M[row][col] );
}
System.out.println();
}
}

}
}```

The second set of nested loop is redundant and the program can also be written using only one set of nested loop as follows:

```import java.util.*;
class Pattern{
public static void main( String args[] ){
Scanner sc = new Scanner( System.in );
String temp;
System.out.print(      "ENTER SIZE       :");
int n = sc.nextInt();
temp = sc.nextLine();
if(n>10){
System.out.println( "OUTPUT          : SIZE OUT OF RANGE");
}else{
char M[][] = new char[n][n];

char c1, c2, c3;
System.out.print(  "INPUT            : First Character  ");
temp = sc.nextLine();
c1=temp.charAt(1);
System.out.print(  "                 : Second Character  ");
temp = sc.nextLine();
c2=temp.charAt(1);
System.out.print(  "                 : Third  Character  ");
temp = sc.nextLine();
c3=temp.charAt(1);
System.out.println("OUTPUT           :");
for( int row=0;row<M.length; row++){
System.out.print("                  ");
for( int col=0; col<M[row].length;col++){
M[row][col]=' ';
if( col>row && col<M.length-1-row) M[row][col]=c1;
else if( colM.length-1-row) M[row][col]=c1;
else if( row==col || row == M.length -col-1) M[row][col]=c3;
else M[row][col]=c2;
System.out.print( M[row][col] );
}
System.out.println();
}
}
}
}```

The ISC Computer Science Question Paper for 2016 is available here.