The Programming Project

Friday, August 1, 2014

ISC COMPUTER SCIENCE PRACTICAL 1998 SPIRAL MATRIX using recursion in JAVA, Python and C++


Spiral Matrix
Write a program which takes N as input, where N is the order of an square matrix. Generate the spiral matrix of order N.
INPUT:
N = 1
OUTPUT:
1

INPUT:
N=2
OUTPUT
1 2
4 3

INPUT:
N=3
OUTPUT
1 2 3
8 9 4
7 6 5

INPUT:
N=4
OUTPUT
1     2    3    4
12  13  14   5
11  16  15   6
10  9    8     7

INPUT:
N=7
OUTPUT
1   2    3   4  5   6   7
24 25 26 27 28 29 8
23 40 41 42 43 30 9
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13

Logic: Go through the code!!!!!! All the best!

JAVA CODE

import java.util.*;

public class ISC1998Spiral {
    public static void main(String[] args) {
        int N, val = 0, row, col, ri, ci;
        int i, j;
        int[][] matrix;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the order of the matrix:");
        N = in.nextInt();
        matrix = new int[N + 1][N + 1];
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                matrix[i][j] = 1;
            }
        }
        row = col = 1;
        ri = (N - (N - 2));
        ci = (N - (N - 1));
        SpiralGeneration sp = new SpiralGeneration();
        System.out.println("OUTPUT:");
        sp.spiral(matrix, N, row, col, val, ri, ci);
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                String s = String.valueOf(matrix[i][j]);
                while (s.length() < 4) {
                    s += " ";
                }
                System.out.print(s);
            }
            System.out.println();
        }
    }
}

class SpiralGeneration {
    public void spiral(int[][] matrix, int N, int row, int col, int val, int ri, int ci) {
        int a, b, tmp, i, j, l = 0;
        a = row;
        b = col;
        tmp = val + 1;
        if (N == 1 || N == 2) // stopping condition
        {
            if (N == 1) {
                matrix[row][col] = tmp;
            } else {
                matrix[row][col] = tmp;
                matrix[row][col + 1] = tmp + 1;
                matrix[row + 1][col + 1] = tmp + 2;
                matrix[row + 1][col] = tmp + 3;
            }
        } else {
            for (b = 1; b <= N; b++) {
                matrix[row][b - 1 + col] = tmp++;
                if (row != b - 1 + col)
                    matrix[b - 1 + col][row] = (4 * N - 2) - (matrix[row][b - 1 + col] - val) + val;
            }
            b--;
            for (a = 1; a <= N - 1; a++) {
                matrix[a + row][b - 1 + col] = tmp++;
                if (a + row != b - 1 + col)
                    matrix[b - 1 + col][a + row] = (4 * N - 2) - (matrix[a + row][b - 1 + col] - val) + val;
            }
            val = matrix[ri][ci];
            spiral(matrix, N - 2, ++row, ++col, val, ++ri, ++ci);
        }
    }
}




C code

#include<stdio.h>
#include<malloc.h>
void spiral(int **matrix,int N, int row, int col, int val,int ri,int ci);
int main() {
    int N,val=0,row,col,ri,ci;
    int i,j;
    int **matrix;
    printf("\n Enter the order of the matrix:");
    scanf("%d",&N);
    matrix=(int **)malloc((N+1)*sizeof(int*));
        for(i=0;i<=N;i++)
            matrix[i]=(int *)malloc((N+1)*sizeof(double));
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            matrix[i][j]=1;
            }
        }   
    row=col=1;   
    ri = (N-(N-2));
    ci = (N-(N-1));
    spiral(matrix,N,row,col,val,ri,ci);
    printf("\n");
    for(i=1;i<=N;i++) {
        for(j=1;j<=N;j++) {
            if ( i== 1)
            printf("%d          ",matrix[i][j]);
            else
            printf("%d         ",matrix[i][j]);
            }
        printf("\n");
        }
    return 0;
}
void spiral(int **matrix,int N,int row,int col,int val,int ri,int ci) {
    int a,b,tmp,i,j,l=0;
    a = row;
    b = col;
    tmp = val +1;
    if (N== 1 || N==2 ) // stopping condition
        {
            if ( N == 1) {
            matrix[row][col] = tmp;
            }
            else {
            matrix[row][col] = tmp;
            matrix[row][col+1] = tmp+1;
            matrix[row+1][col+1] = tmp+2;
            matrix[row+1][col] = tmp+3;
            }
        }
    else    {       
        for( b = 1 ; b <= N ; b++) {
            matrix[row][b-1+col] = tmp++;
            if( row!=b-1+col )
            matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val ;
            }
        b--;
        for( a = 1 ; a <= N-1; a++) {
            matrix[a+row][b-1+col] = tmp++;
            if( a+row!=b-1+col)
            matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val;
            }
        val = matrix[ri][ci];   
        spiral(matrix,N-2,++row,++col,val,++ri,++ci);   
    }   
}


Python Code

Passing List to function, recursion in python


def SpiralMatrix():
    N = int(input("Enter the order of the Magic Square (odd):"))
    matrix=[[ 0 for row in range(N+2)]for col in range(N+2)]
    for i in range (N+2):
        for j in range (N+2):
            matrix[i][j]=1
    row=col=1  
    val = 0
    ri = (N-(N-2))
    ci = (N-(N-1))  
    def spiral(matrix,N,row,col,val,ri,ci):
        a = row
        b = col
        tmp = val +1
        #print a,b,tmp
        if N== 1 or N==2:
            if N == 1:
                matrix[row][col] = tmp;
            else:
                matrix[row][col] = tmp
                matrix[row][col+1] = tmp+1
                matrix[row+1][col+1] = tmp+2
                matrix[row+1][col] = tmp+3
           
        else:      
            for b in range(N):
                b = b+1
                matrix[row][b-1+col] = tmp
                tmp = tmp+1
                if row != b-1+col:
                    matrix[b-1+col][row]  = (4*N-2)-(matrix[row][b-1+col]-val) + val
               
            for a in range(N-1):
                a = a+1
                matrix[a+row][b-1+col] = tmp
                tmp = tmp+1
                if a+row != b-1+col:
                    matrix[b-1+col][a+row]  = (4*N-2)-(matrix[a+row][b-1+col]-val) +val
            val = matrix[ri][ci]
            row = row+1
            col = col+1
            ri = ri+1
            ci = ci+1    
            spiral(matrix,N-2,row,col,val,ri,ci)
    spiral(matrix,N,row,col,val,ri,ci)      
    print ("OUTPUT~~ SPIRAL MATRIX:")
    for i in range (N):
        for j in range (N):
            print (matrix[i+1][j+1],end =' ')
        print()
SpiralMatrix()      

Wednesday, July 30, 2014

ISC COMPUTER SCIENCE PRACTICAL Solved ~ 2004



The computer department of the Agency of International Espionage is trying to decode intercepted messages.
The agency's spies have determined that the enemy encodes messages by first converting all characters to
their ASCII values and then reversing the string. For example, consider A_z ( the underscore is just to hightlight the space).
The ASCII values of A, <space>, z are 65,32,122 respectively.
Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it.
It will contain only alphabets (A......Z, and a........z) and spaces.
ASCII values of A.......Z are 65........90 and those of a.....z are 97......122.
Test your program for the following data and some random data.


SAMPLE DATA:
INPUT:
Encoded Message: 2312179862310199501872379231018117927
OUTPUT:
The Decoded Message: Have a Nice Day

The idea behind the program is to store the Encoded message in a String.
Reverse the string and extract the individual characters and convert it 
into its corresponding integer values.
So, if input is 2212356 we will reverse it to get
6532122
Now the integer values 6,5,3,2,1,2,2 will be stored in some array;
To get the integer value of the character '6' (say) we need to subtract 48 from it.
since 54 is the ascii value of '6'. thus 54-48 = 6.
This has been achived by the following code
 public void reverseString() {
        int j = 0;
        for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
              codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position from the String msg in
            }                                          // in reverse order
        }  
Now the idea is to extrac the ascii values from the array and print the 
corresponding character
For example, we need to extrac 65, 32 and 122 from the array.
For this note that the ascii values of characters ranges from 65 to 90
and 97 to 122 and one extra value of 32 for the space.
So we will take the first 3 values from the integer, 6 5 and 2 and convert
it into decimal number by 6*10*10+5*10+2=652
but this is outside our range, so we need to take only 6 and 5 and convert it
to get 65. then print the corresponding character. For this we have used the code

while ( i < Decryptmsg.length() ) {
              if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
                     tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
              else
                     tmp = codeValues[i]*10+codeValues[i+1];
              if (tmp >= 97 && tmp <=122) {
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     i +=3;
                     }
              else {
                     tmp = codeValues[i]*10+codeValues[i+1];
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     if(tmp==32)
                           System.out.print("");
                     i +=2;
                     }
              }
Below is the link of solved ISC Computer Science Practical Paper year wise.


JAVA CODE

import java.util.*;
import java.io.*;
public class ISC2004Q1 {
    public static void main(String[] args) throws IOException
        {
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence (Encoded Message):");
        msg = br.readLine();   
        Decrypt ob = new Decrypt(msg);
        ob.reverseString();
        System.out.println("OUTPUT The Decoded Message: ");
        ob.decryptCode();       
        }
    }
class Decrypt {
    Decrypt(String msg) {
        Decryptmsg = msg;
        codeValues = new int[Decryptmsg.length()];
        }
    public void reverseString() {
        int j = 0;
        for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
              codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position from the String msg in
            }                                          // in reverse order
        }   
    public void decryptCode() {
       int tmp,i=0;
        while ( i < Decryptmsg.length() ) {
              if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
                     tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
              else
                     tmp = codeValues[i]*10+codeValues[i+1];
              if (tmp >= 97 && tmp <=122) {
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     i +=3;
                     }
              else {
                     tmp = codeValues[i]*10+codeValues[i+1];
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     if(tmp==32)
                           System.out.print("");
                     i +=2;
                     }
              }
       }   
    private String Decryptmsg;   
    private int[] codeValues;
    }  

Monday, July 28, 2014

ISC COMPUTER SCIENCE PRACTICAL 2014~ COMPOSITE MAGIC

/*
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number : A composite number is a number which has more than 2 factors.
For example : 10
Factors are : 1,2,5,10.
Magic number : A magic number is a number in which the eventual sum of the
digits is equal to 1.
For example:- 28 = 2+ 8 =10 = 1+0=1
Accept 2 positive integers m and n, where m is less than n as user input. Display the number of
composite magic integers that are in the range between m and n (both inclusive) and output them
along with the frequency, in the format specified below.
Test your program with the sample data and some random data:

Example 1:
INPUT:
m=10
n=100
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
10,28,46,55,64,82,91,100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :8

Example 2:INPUT:
m=1200
n=1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
1207,1216,1225,1234,1243,1252,1261,1270,1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :9

Example 3:
INPUT:
m=120
n=99
OUTPUT:
INVALID INPUT
*/

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2014 */
import java.util.*;
public class CompositeMagic {
    public static void main(String[] args) {
        int m,n;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF m & n:");
        m = in.nextInt();
        n = in.nextInt();
        while( m > n || n < 1 || m < 1) {
            System.out.println("INVALID CHOICE:");
            m = in.nextInt();
            n = in.nextInt();
            }
        CheckCompositeMagic cm = new CheckCompositeMagic(m,n);
        System.out.println("THE COMPOSITE MAGIC NUMBERS ARE:");
        cm.checkComposite();
        System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:"+cm.fCount());
        System.out.println();
        }
    }
class CheckCompositeMagic {
    CheckCompositeMagic (int m, int n) {
        M = m; N = n;
        freqCompositeMagic = 0;
        }
    public int fCount() {
        return     freqCompositeMagic;
        }
    public void checkComposite() {
        for ( int i = M; i <= N ;i++) {
            flag = false;
            for ( int j = 2; j <= Math.sqrt(i); j++) {
                if( i%j == 0) {
                    flag = true;
                    break;
                    }
                }
            if(flag == true) {
                 checkMagic(i);
                 if(fg == true)   
                     System.out.println(i);
                 }   
            }
        }    
    private void checkMagic(int mg) {
        fg = false;
        int tmp,rem;
        tmp = mg;
        sum = 0;
        while( tmp > 0) {
            rem = tmp % 10;
            sum += rem;
            tmp = tmp/10;
            }
        if( sum == 1 ) {
            freqCompositeMagic++;
            fg = true;
            }   
        else {
            if(sum < 10)
                fg = false;
            else
                checkMagic(sum);
            }   
           
        }   
    private int M;
    private int N;   
    private int freqCompositeMagic;
    private int sum;
    private boolean flag,fg;
    }