Guide For School logo Guide For SchoolICSE and ISC Resources

Java program to fill a Matrix with 3 characters in the given sequence (ISC Specimen 2016 Question 3)

30 September 2015

Java program to fill a square Matrix with 3 characters in the given sequence. Solution to Question 3 of ISC Computer Science Practical Specimen 2016.


Question:

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 :
Plain
# * * #
? # # ?
? # # ?
# * * #
Example 2 ENTER SIZE : 5 INPUT : FIRST CHARACTER : ‘$’ SECOND CHARACTER : ‘!’ THIRD CHARACTER : ‘@’ OUTPUT :
Plain
@ $ $ $ @
! @ $ @ !
! ! @ ! !
! @ $ @ !
@ $ $ $ @
Example 3 ENTER SIZE : 65 OUTPUT : SIZE OUT OF RANGE

Programming Code:

Java
/**
* The class MatrixFill creates a matrix using 3 characters taken as inputs
* Upper and lower elements formed by the intersection of the diagonals are filled by character 1.
* Left and right elements formed by the intersection of the diagonals are filled by character 2.
* Both the diagonals are filled by character 3.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2 
*/

import java.util.*;
class MatrixFill
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of the matrix : ");
        int n = sc.nextInt();
        
        if(n<2 || n>10)
            System.out.println("Size out of Range");
        else
        {
            char A[][]=new char[n][n];
            System.out.print("Enter the 1st character : ");
            char c1 = sc.next().charAt(0);
            System.out.print("Enter the 2nd character : ");
            char c2 = sc.next().charAt(0);
            System.out.print("Enter the 3rd character : ");
            char c3 = sc.next().charAt(0);
            
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    if(i==j || (i+j)==(n-1))
                        A[i][j] = c3; // Filling the diagonals with 3rd character
                    else
                        A[i][j] = c2; // Filling all other positions with 2nd character
                }
            }
            
            for(int i=0; i<n/2; i++)
            {
                for(int j=i+1; j<n-1-i; j++)
                {
                    A[i][j] = c1; // Filling the upper positions formed by intersection of diagonals
                    A[n-1-i][j] = c1; // Filling the lower positions formed by intersection of diagonals
                }
            }
            
            // Printing the Matrix
            System.out.println("\nOutput : \n");
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    System.out.print(A[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
}

Output:

Plain
Enter size of the matrix : 7
Enter the 1st character : @
Enter the 2nd character : #
Enter the 3rd character : %

Output : 

% @ @ @ @ @ % 
# % @ @ @ % # 
# # % @ % # # 
# # # % # # # 
# # % @ % # # 
# % @ @ @ % # 
% @ @ @ @ @ %

Tags and Categories

Array Related ProgramsClass 12ISC Important ProgramsISCComputer Applications