Thursday , 25 May 2017
Home » Array Related Programs » Java program to fill a Matrix with 3 characters in the given sequence (ISC Specimen 2016 Question 3)

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

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 :

# * * #
? # # ?
? # # ?
# * * #

Example 2

ENTER SIZE : 5
INPUT : FIRST CHARACTER : ‘$’
SECOND CHARACTER : ‘!’
THIRD CHARACTER : ‘@’

OUTPUT :

@ $ $ $ @
! @ $ @ !
! ! @ ! !
! @ $ @ !
@ $ $ $ @

Example 3

ENTER SIZE : 65
OUTPUT : SIZE OUT OF RANGE

Programming Code:

/**
* 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:

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

Output : 

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

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

7 comments

  1. vry vry hlpful…thnk u!!

  2. thanks sir your programs are in easy language and easy to understand… the programs were very helpful

  3. Sir why is this program not giving the required output,its not accepting c3

    import java.io.*;
    class x
    {
    public static void main (String args[])throws IOException
    {
    DataInputStream in=new DataInputStream(System.in);
    int n;
    System.out.println(“Enter the value of ‘n’ i.e the order”);
    n=Integer.parseInt(in.readLine());
    if(n>0&&n<10)
    {
    char a[][]=new char[n][n];
    System.out.println("Enter the value of c1,c2,c3");
    char c1,c2,c3;
    c1=(char)(in.read());
    c2=(char)(in.read());
    c3=(char)(in.read());

    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    if(i==j)
    {
    a[i][j]=c3;
    }
    if(i+j==n-1)
    {
    a[i][j]=c3;
    }
    else if((i!=j)&&(i+j!=n-1))
    {
    a[i][j]=c1;
    }
    }
    }
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    if(i!=j&&(j==(n-1)||j==0))
    {
    a[i][j]=c2;

    }}}
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    System.out.println(a[i][j] + " ");
    }}

    }
    else
    System.out.println("Out of bond");
    }
    }

    • yes because it is accepting the ‘enter key’ which you are pressing as an input for c3.
      Try using :
      c1 = in.readLine().charAt(0);
      c2 = in.readLine().charAt(0);
      c3 = in.readLine().charAt(0);

  4. How can we solve 1st and 2nd sum in 2016 specimen please help me

  5. Thnx for guidance

Leave a Reply to Snehasish Cancel reply

Your email address will not be published. Required fields are marked *