The Programming Project

Thursday, May 8, 2014

ISC Computer Science Practicals: Solved


Write a program to declare a matrix A [][] of order (MXN)
where 'M' is the number of rows and 'N' is the number of
columns such that both M and N must be greater than 2 and
less than 20. Allow the user to input integers into this matrix.
Perform the following tasks on the matrix:

1. Display the input matrix
2. Find the maximum and minimum value in the matrix and display
them along with their position.
3. Sort the elements of the matrix in ascending order using any
standard sorting technique and rearrange them in the matrix.

Output the rearranged matrix.

Sample input Output
INPUT:
M=3
N=4
Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4
Original matrix:

8 7 9 3
-2 0 4 5
1 3 6 -4

Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3

Rearranged matrix:

-4 -2 0 1
3 3 4 5
6 7 8 9

Monday, May 5, 2014

open GL Programming in LINUX


To compile a program (using open GL graphics library) in Linux, go to the current directory where the program is residing say test.c. type the following command gcc -o xyz test.c -lglut -lGLU -lGL -lGLU make sure you have the open GL library installed! After the execution of the command a output file with name xyz will be created in the current directory. To run the output file type in the following command ./xyz

To sum up consider a program vinod.c in the folder Graphics, to execute it perform the following steps.
administrator@ubuntu:~/Graphics$ gcc -o vin vinod.c -lglut -lGLU -lGL -lGLU
administrator@ubuntu:~/Graphics$ ./vin


Here is an example of a simple program which will just create a window with a text

#include <GL/glut.h>
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("MY FIRST GRAPHICS PROGRAM");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Sunday, April 20, 2014

Pascal trialngle



CODE

import java.io.*;
import java.util.*;
public class Pascal {
    public static void main(String[] args) {
        int rows;
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        s1.push(1);
        s1.push(1);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows:");
        rows = in.nextInt();
        System.out.println("Pascal Traingle:");
        for(int i = 1; i <= rows; i++) {
            if(i==1 || i==2) {
                if(i==1)
                    System.out.println("1");
                else {
                    System.out.println();
                    System.out.println("1  1");
                    }
                }
            else {
            System.out.println();
            int p = s1.pop();
            System.out.print(p+" ");
            s2.push(p);
            while(s1.empty() == false) {
                    int q = s1.pop();
                    int next = p+q;
                    System.out.print(next+" ");
                    s2.push(next);
                    p = q;
                    }
            System.out.println(" 1 ");
            s2.push(1);
            while(s2.empty() == false) {
                    s1.push(s2.pop());
                    }
                }
            } // end of i loop
        }
    }

Saturday, April 19, 2014

Longest increasing subsequence (LIS)


Longest increasing subsequence (LIS) problem is to find the length of longest subsequnce out of a given sequnce such that all the element of the subsequence is sorted in strictly increasing order of their value.

INPUT SPECIFICATION
An array (may be unsorted)
OUTPUT SPECIFICATION:
Length of LIS

Examples:

Input Array 10,22,9,33,21,50,41,60,80
Length of LIS is 6 & LIS is 10,22,33,50,60,80

Input Array 1,2,3,4,5,6,7,8,7,6,5,4,5,6,7,8,9,10,11
Length of LIS is 11 & LIS is 1,2,3,4,5,6,7,8,9,10,11



 import java.io.*;
import java.util.*;
public class ArraySubsequence {
    public static void main(String[] args) {
        int n;
        int[] arr;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        n = in.nextInt();
        arr = new int[n];
        System.out.println("Enter the elements of the array:");
        for(int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        LIS ls = new LIS();
        ls.lisGenerate(arr);
        }
    }

class LIS {
    public void lisGenerate(int[] seq) {
        int[]L=new int[seq.length];
            L[0]=1;
            for(int i=1;i<L.length;i++) {
                  int maxn=0;
                   for(int j=0;j<i;j++){
                    if(seq[j]<seq[i]&&L[j]>maxn){
                          maxn=L[j];
                        }
                      }
                  L[i]=maxn+1;
            }
            int maxi=0;
            for(int i=0;i<L.length;i++) {
                  if(L[i]>maxi) {
                    maxi=L[i];
                     }
              }
        System.out.println(maxi);
        }
    }
   

Friday, April 18, 2014

Fibonacci Series in Python

#File: Fibonacci.py
#A simple program to generate Fibonacci series

def main():
    print "Fibonacci Series"
    n = input("Enter the value of n, number of terms:")
    x = n
    def fibo(n):
        if n == 0 or n == 1:
            return 1           
        else:
            return fibo(n-1)+fibo(n-2)
           
    for i in range(x+1):
        print fibo(i)   
main()

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





C Code
#include<stdio.h>
long int Binomial(long int, long int);
int main()
    {
    long int n,k;
    printf("\n Enter the value of n and k, n is greater than k:");
    scanf("%ld %ld",&n,&k);
    while( k < 0 || n < 1 || k > n) {
        printf("\n Wrong input, enter again:");
        scanf("%ld %ld",&n,&k);
        }
    printf("\n Value of %ldC%ld: %ld \n", n,k,Binomial(n,k));
    return 0;
    }
long int Binomial(long int n, long int k)
    {
    return (k <= 1 ? (k == 0 ? 1 : n) : (n*Binomial(n-1,k-1))/k);
    } 


Python Code
 
#File: binomial.py
#A simple program to calculate nCk using recursion
def main():
    print "Binomial Coefficient"
    n = input("Enter the value of n:")
    k = input("Enter the value of k:")
    x,y= n,k
    def binomial(n,k):
        if k < 2:
            if k == 1:
                return n
            else:
                return 1
        else:
            return (n*binomial(n-1,k-1))/k
   
    print "Value of nCk is",binomial(x,y)
   
main()


OUTPUT:

administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import binomial
Binomial Coefficient
Enter the value of n:1200
Enter the value of k:600
Value of nCk is 396509646226102014036716014862605729531608195706791651137874888645453416610941265150218719101931467943643355545203450497992759241509133380338379948816055787676920090991204851973167965845932778899299658455186568000803781988756668261491937388063732393433822461878746138860879613812823280622769290795808335597761702284943981759657834318699226167559487050708295600
>>>

/* To calculate the Binomial Coeffcient using recursion */
/* We have use the formula nCk = (n/k)*{(n-1)C(k-1)} */

Wednesday, April 16, 2014

Kaprekar's Number in JAVA: ISC Computer Science Practicals 2010




Given the two positive integers p and q, where p < q. Write a program to determine how
many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them.
About 'kaprekar' number:

SAMPLE DATA:

INPUT:
p=1
Q=1000

OUTPUT:

THE KAPREKAR NUMBERS ARE:
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS:8

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2010 */