The Programming Project

Thursday, March 14, 2019

ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 2 : MATRIX

QUESTION 2:

ISC COMPUTER SCIENCE PRACTIACAL SOLVED
Write a program to declare a single dimensioan 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 dimensinal array.
Perform the following task on the matrix:

(a) Sort the elements of the single dimensional array in ascending order using any sorting technique
      and display the sorted elements.

(b)  Fill the matrix b[][] in the following format.
       If the array a[] ={5,2,8,1} then after sorting, a[]={1,2,5,8}
       then the matrix b[][] would fill as below:
                1   2   5   8
                1   2   5   1
                1   2   1   2
                1   1   2   5

(c)  Display the filled matrix in the above format

Test your program for some random data.

To undersatnd the logic of creation of the two dimensional array, take 
2,5,6,10,23 to be the elements of single dimensional array after sorting.

As per condition, the two dimensional array must be of size 5x5.
Now the position of elements of single dimensional array is 0,1,2,3,4

We now mark the position of the two dimensiona array:

(0,0)    (0,1)    (0,2)    (0,3)    (0,4)   
2[0]     5[1]     6[2]     10[3]    23[4]

(1,0)    (1,1)    (1,2)    (1,3)    (1,4)  
2[0]     5[1]     6[2]     10[3]     2[1]

(2,0)    (2,1)    (2,2)    (2,3)    (2,4)  
2[0]     5[1]      6[2]    2[1]      5[2]

(3,0)    (3,1)    (3,2)    (3,3)    (3,4)  
2[0]     5[1]      2[0]     5[1]     6[2]

(4,0)    (4,1)    (4,2)    (4,3)    (0,4)   
 2[0]    2[0]      5[1]     6[2]    10[3]

where (i,j) represent row-coloumn position and 
val[k] represent value at position k in single dimensiona array.

It is easy to see that whenever i+j > 5 ( order of the matrix)
in any row, from that position onwards(along the row), elements
from position 0 onwards (from single dim array) starts filling the row.

So whenever i+j> 5 in any row we need to move the counter again at 
position 0 in the single dim array.

For this see that i+j-5 (in general i+j-order) will help the purpose.

For example, for the third row,
(3,0)    (3,1)    (3,2)    (3,3)    (3,4)  
2[0]     5[1]      2[0]     5[1]     6[2]

From position (3,2) onwards,
i+j-order =       3+2-5,   3+3-5,   3+4-5
                    0        1        2
which gives positions from the starting point in the single dim-array!
Thus the code!

for(int i = 0; i < orderi++) {
                for(int j = 0; j < orderj++) {
                        if(i+j < order)
                          Mat[i][j]=single_dim_array[j];
                        else
                          Mat[i][j]=single_dim_array[(i+j)-order];
                  }
           }

Always try to choose the problem involving matrices! Once you hit the 
idea it will me much easier and faster compared to other problems!


import java.util.*;
public class ISC2019Q2 {
       public static void main(String[] args) {
             int N;
             Scanner in = new Scanner(System.in);
             do {
                 System.out.println("Enter the value of N:");
                 N = in.nextInt();
                 if(N <= 2 || N >= 10)
                              System.out.println("INVALID INPUT:");
                 }while(N < 1 || N > 366);
             Matrix obj = new Matrix(N);
             System.out.println("Enter the elements of single dimensional array:");
             obj.inputSingleDimArray();
             obj.SortMatrix();
             obj.inputElementsToTwoDimMatrix();
             obj.displayMatrix();
             in.close();
             }
       }
     
class Matrix{
       Matrix(int N){
             order = N;
             single_dim_array = new int[order];
             Mat = new int[order][order];
       }
       public void inputElementsToTwoDimMatrix() {
           for(int i = 0; i < order; i++) {
                for(int j = 0; j < order; j++) {
                        if(i+j < order)
                          Mat[i][j]=single_dim_array[j];
                        else
                          Mat[i][j]=single_dim_array[(i+j)-order];
                  }
           }
         
     }
      
       public void inputSingleDimArray() {
          Scanner in = new Scanner(System.in);
          for(int i=0;i<order;i++) {
                single_dim_array[i]=in.nextInt();
                if(single_dim_array[i] <= 0) {
                     System.out.println("Only positive values allowed,enter the value again:");
                     i--;
                     continue;
                 }
              }
          in.close();
       }
       public void SortMatrix() {
             int temp;
             for (int i =0;i<order;i++) {
             for(int j=i;j<order;j++) {
                     if(single_dim_array[i] > single_dim_array[j]) {
                           temp = single_dim_array[j];
                         single_dim_array[j]=single_dim_array[i];
                         single_dim_array[i]=temp;
                         }
                  }
              }        
        }
     
       public void displayMatrix() {
            System.out.println("SINGLE DIMENSIONAL ARRAY AFTER SORTING:");
            for(int k=0;k<order;k++)
               System.out.print(" "+single_dim_array[k]);
            System.out.println();
            System.out.println("TWO DIMENSIONAL MATRIX:");
             for(int i = 0; i < order; i++) {
                    for(int j = 0; j < order; j++) {
                          System.out.print(Mat[i][j]+"  ");
                    }
                    System.out.println();
             }
       }
     
       private int[][] Mat;
       private int[] single_dim_array;
       private int order;
       }

ISC COMPUTER SCIENCE PRACTICAL 2019 QUESTION 1 : DATE CALCULATION


QUESTION 1:
ISC COMPUTER SCIENCE PRACTIACAL SOLVED
Design a program to accept a day number (between 1 and 366), year(in 4 digits) from the user to generate and display the corresponding date. Also, accept ‘N’ ( 1<= N <= 100) from the user to compute and display  the future date corresponding to ‘N’ days after the generated date. Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.
Test your program with the following data and some random data:
INPUT:

EXAMPLE 1: DAY NUMBER:                  255
                        YEAR:                                  2018
                        DATE AFTER (N DAYS):    22
OUTPUT:       DATE:                                   12 TH SEPTEMBER, 2018
                        DATE AFTER 22 DAYS      4 TH OCTOBER, 2018
NOTE: THIS PROBLEM ALREADY CAME IN ISC 2009 COMPUTER SCIENCE PRACTICAL

import java.util.*;
public class ISC2019Q1 {
    public static void main(String[] args) {
       int day_num;
             int yr;
             int N;
        Scanner in = new Scanner(System.in);
        DaysCalculation date = new DaysCalculation();
        do {
                    System.out.println("Enter a day number (between 1 and 366)");
                    day_num = in.nextInt();
                    if(day_num < 1 || day_num > 366)
                                 System.out.println("INVALID DAY NUMBER:");
                    }while(day_num < 1 || day_num > 366);
            
             do {
                    System.out.println("Enter the year");
                    yr = in.nextInt();
                    if(Integer.toString(yr).length()!=4)
                                 System.out.println("INVALID YEAR:");
                    }while(Integer.toString(yr).length()!=4);
             do {
                    System.out.println("Enter the value of N:");
                    N = in.nextInt();
                    if(N < 1 || N > 100)
                                 System.out.println("INVALID DAY NUMBER (between 1 and 100):");
                    }while(N < 1 || N > 100);
        date.setDate(day_num,yr,N);  
        System.out.println("OUTPUT:");
        date.dateSpecifier(); 
        in.close();
        }   
    }
class DaysCalculation {
    public void setDate(int dayNumber, int year, int dayAfter) {
        this.dayNumber = dayNumber;
        this.year = year;
        this.dayAfter = dayAfter;
        }
    public void dateSpecifier() {
        int m = 0,k=1;
        for(int i = 1; i <= dayNumber; i++) {
            if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
                k = 1;
                m++;
                }
            k++;   
            }
        String prefix;   
        prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
        System.out.println("DATE:     "+(k-1)+prefix+" "+months[m]+" ,"+year); 
        for (int i = 1; i <= dayAfter; i++) {
            if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
                k = 1;
                m++;
                if(m > 11) {
                    year++;
                    m = 0;
                    }
                }
            k++;   
            }
        prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
        System.out.println("DATE AFTER "+dayAfter+" DAYS:"+(k-1)+""+prefix+" "+months[m]+"  ,"+year);       
        }   
    private boolean checkLeap(int year) {
        if(year%400==0)
           leap=true;
        else if (year%100==0)
           leap=false;
        else if (year%4==0)
           leap=true;
        else
           leap=false;
           return leap;
        }    
    private boolean flag;
    private static boolean leap;   
    private int dayNumber,year;
    private int dayAfter;
    String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
    int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};   
    }