ISC Paper 2019 Matrix Program

ISC Computer Science Practical Paper - 2019

Question 2 Solved

Write a program to declare a single dimensional array a[] and a square matrix b[][] of size N, where N>2 and N<10. Allow the user to input positive integers into the single dimensional array.

Example :-
Input    : N = 3
                Enter elements of single dimensional array : 3  1  7

Output  : Sorted Array : 1  3  7
                 Filled Matrix
                     1  3  7
                     1  3  1
                     1  1  3

For more examples see the question paper image below :-

Question Paper :-

Algorithm, Logic and Program given below :-

Logic :-


Algorithm :-
  • Input capacity of array in a variable
  • Define two array (1D and 2D) with the entered capacity 
  • Store elements in 1D array
  • Display the unsorted array
  • Now using any sorting technique sort the array
  • Now using pattern program logic store the 1st pattern in 2D array
  • After storing 1st pattern using logic of 2nd pattern, store the 2nd pattern in 2D array
  • Display the sorted 1D array
  • Display required 2D array.
  • Compile and run the program. 
  
Program :-

import java.io.*;
import java.util.*;
class DDA
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n,i,j,x,l,m,temp;
        System.out.println("Enter Array Capacity");
        n= in.nextInt();
// Input Array Capacity
        int a[]= new int[n];
// Single Dimensional Array
        int b[][]= new int[n][n];
// Double Dimension Array
        System.out.println("Enter Array Elements");
        for(i=0;i<n;i++)
        {
            a[i]= in.nextInt();
// Storing numbers in array
        }
        System.out.println("Original 1D Array");
        for(i=0;i<n;i++)
        {
            System.out.print(a[i]+"\t");
// Displaying unsorted array
        }
        System.out.println();
        for(i=0;i<n-1;i++)
        {
            for(j=0;j<((n-1)-i);j++)
            {
                if(a[j]>a[j+1])
// Sorting array
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        x=0;
// variable used for storing elements in array.
        for(i=n;i>=1;i--)
// 1st pattern condition
        {
            for(j=1;j<=i;j++)
            {
                b[x][j-1]= a[j-1];
// storing first pattern elements in 2D array
            }
            x++;
        }
        m=1;
        l=n;
        for(i=1;i<n;i++)
// 2nd pattern condition
        {
            for(j=1;j<=i;j++)
            {
                b[m][l-1]= a[j-1];
// storing 2nd pattern in 2D array
                l++;
            }
            m++;
            l=l-m;
        }
        System.out.println("Sorted 1D Array");
        for(i=0;i<n;i++)
        {
            System.out.print(a[i]+"\t");
// displaying sorted array
        }
        System.out.println();
        System.out.println("2D Array");
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(b[i][j]+"\t");
// display double dimensional matrix
            }
            System.out.println("\t");
        }
    }
// end of main method
}
// end of class 



Note : The program is written in the easiest way possible so try to understand it. If you face any problem in understanding this program than feel free to comment.



All the Best :)
Keep Learning :)
 

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA