Thursday, June 26, 2014

Array - I



What are Arrays?

An array is a group of same typed variables that are referenced by a common name. Arrays can be any typed and one or more dimensions. A particular element can be addressed by array name and index of the array enclosed in third bracket ([]). Array can be declared with its initial values or can be defined with the size and can input the values latter. An array index start from 0 by default. Length of single dimension array can get using a function – array. length

int a[]={1,2,3,4};
double a[]=new double[10];

To store similar types of data under a common variable name, instead of declaring N number of variables for the N number of data, we use an array. An array is a collection of similar type of variables under a common variable. Thus we can have a collection or Array of integers, floats, characters etc., but remember that all elements of any given array must be of the same type, i.e. we cannot have an array of numbers some of which are int and some float etc. Either all should be int or all should be float or all should be char. The different components of an array are called the Array Elements. Arrays also known as subscripted variable
How to Declare an Array:
The array having one column and multiple rows are known as single dimensional array. Array with multiple rows and multiple columns are known as multi-dimensional arrays.
Like all other variables, an array needs to be declared first before it can be used. The declaration consists of stating the type of data-type used in the array, the name of the array and the number of elements in the array.
Single Dimensional Array

Data Type Array_Variable[ ] – new Data Type [Size] ;
Example :
int a[]=new int[20];
String name[]=new String[20];
Suppose we declare an array if integer with 5 elements, N[5] and when we store first five natural numbers to it, then it will look like the following table
N[0]
1
N[1]
2
N[2]
3
N[3]
4
N[4]
5
Here N is variable and [0] to [4] is subscript or element location. When we input data in N[0] to N[4] location we can recall back in any point in that particular program. Above array also known as single dimensional array. We can initialize the array with a list of values.
Example :
Java : int a[]={1,5,6,7};
String name[]={“Ashok”,”Anil”};
Initialization can be done when we use a fixed numbers of known data but there is no shortcut method to initialize large number of elements. 

Multi-Dimensional Array
A multi-Dimensional array declared with multiple rows and columns.
Int N[][] =  new int [3][3]
Above array will look like the following table.
Example


Column[0]
Column[1]
Column[0]





Row[0]

Cell[0][1]
Cell[0][1]
Cell[0][2]
Row[1]

Cell[1][1]
Cell[1][1]
Cell[1][2]
Row[2]

Cell[2][1]
Cell[2][1]
Cell[2][2]

We can initialize the Multi-dimensional array with a list of values too.
Int a[][]={{1,2,3},{3,4,5}};
String a[][]={{“A”,”B”},{“C”,”D”}}’;
Some example of Input and display the array elements.
Example




import java.util.*;
import java.util.Collections;
import java.util.Arrays;

   class Arr_Input_Output
     {
         public static void main(String[] args)
           {
               Arr_Input_Output AIR=new Arr_Input_Output();
               System.out.println("\n\tSingle Dimensional Array ");
               AIR.Single();
               System.out.println("\n\tSingle Dimensional Array ");
               AIR.Multi();              
           }
         public void Single()
           {
               Scanner Sc=new Scanner(System.in);
               int n;
               System.out.print("\n\tEnter Size : ");
               n=Sc.nextInt();
               int a[]=new int[n];
               System.out.println("\n\tEnter "+n+" Nos. : \n");
               for(int i=0;i
                 {
                     System.out.print("\t");
                     a[i]=Sc.nextInt();
                 }
               System.out.print("\n\tNos. in Array : "); 
               for(int i=0;i
                 {
                     System.out.print(a[i]+"  ");
                 }              
           }
         public void Multi()
           {
               Scanner Sc=new Scanner(System.in);
               int n,m;
               System.out.print("\n\tEnter Nos. of Rows : ");
               n=Sc.nextInt();
               System.out.print("\n\tEnter Nos. of Cols : ");
               m=Sc.nextInt();              
               int a[][]=new int[n][m];
               System.out.println("\n\tEnter "+(n*m)+" Nos. : \n");
               for(int i=0;i
                 {
                     for(int j=0;j
                       {
                           System.out.print("\t");
                           a[i][j]=Sc.nextInt();
                       }

                 }     
               System.out.print("\n\tNos. in Array : \n"); 
               for(int i=0;i
                 {
                     System.out.print("\t");
                     for(int j=0;j
                       {
                           System.out.print(a[i][j]+"  ");
                  
                       }
                     System.out.println(); 
                 }             
           }          
    }


Output


          Single Dimensional Array

          Enter Size : 3

          Enter 3 Nos. :

          1
          2
          3

          Nos. in Array : 1  2  3 
          Single Dimensional Array

          Enter Nos. of Rows : 2

          Enter Nos. of Cols : 2

          Enter 4 Nos. :

          1
          2
          3
          4

          Nos. in Array :
          1  2 
          3  4 






Thursday, June 19, 2014

String Encryption & Decryption



There is many ways one can encrypt and decrypt the message. Following program is one way and given by TCS as assignment to the job aspirant.



Question


Mo and Larry have devised a way of encrypting messages.
They first decide secretly on the number of columns and write the message (letters only)
down the columns, padding with extra random letters so as to make a rectangular array
of letters. For example,
if the message is Theres no place like home on a snowy night and there are five columns,
Mo would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

Note that Mo includes only letters and writes them all in lower case.
In this example, Mo used the character x to pad the message out to make a rectangle,
although he could have used any letter.
Mo then sends the message to Larry by writing the letters in each row,
alternating left-to-right and right-to-left. So, the above would be encrypted as


toioynnkpheleaigshareconhtomesnlewx


Your job is to recover for Larry the original message (along with any extra padding letters)
from the encrypted one.


Input Format
There will be multiple input sets.
Input for each set will consist of two lines.
The first line will contain an integer in the range 2...20 indicating the number of columns used.
The next line is a string of up to 200 lower case letters.
The last input set is followed by a line containing a single 0, indicating end of input.


Output Format

Each input set should generate one line of output,
giving the original plaintext message, with no spaces.


Sample Input

5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Sample Output

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab



Coding



import java.io.*;
public class ToandFro
    {

    public static void main(String args[])throws IOException
    {
        Convert_to();
        Convert_fro();
    }
    public static void Convert_to()throws IOException
     {
        char a[][];
        String s,s1;
        int n,n1,i,j,k;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter column and Sentence");
        n=Integer.parseInt(br.readLine());
        s=br.readLine();       
        s=s.replace(" ",""); 
        n1=(int)Math.ceil(s.length()/(double)n);
        s=s.toLowerCase();
        a=new char [n1][n];
        k=0;
        System.out.println("Convert To : ");
        System.out.println();
        for(i=0;i
         {
            for(j=0;j
             {
                if(k
                a[j][i]=s.charAt(k++);
                else
                a[j][i]='x';
             }
         }
        for(i=0;i
         {
           for(j=0;j
            {
                 System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();
        s1="";
        for(i=0;i
         {
            if(i%2==0)
             {
               for(j=0;j
                {
                  s1=s1+a[i][j];
                }
             }
           else
            {
               for(j=n-1;j>=0;j--)
                  {
                   s1=s1+a[i][j];
                  }
            }
        }
    System.out.println(s1);
   }
    public static void Convert_fro()throws IOException
     {
        char a[][];
        String s,s1="",s2="";;
        int n=1,n1,i,j,k,c=0;;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println(s1);
        outer:
        while(n!=0)
         {
           while(true)
           {
             System.out.println("Enter column and Sentence");
             n=Integer.parseInt(br.readLine());
             if(n==0)
                    break outer;
             s=br.readLine();
             if(n>=2 && n<=20 && s.length()<=200)
              {
                    break;
              }
           }
           s=s.replace(" ","");
           s=s.toLowerCase();
           s2=s2+n+" "+s+" ";
           c++;

        }
        System.out.println("Convert Fro : ");
        System.out.println();
        for(int t=1;t<=c;t++)
         {
            int L=s2.indexOf(" ");
            n=Integer.parseInt(s2.substring(0,L));
            s2=s2.substring(L+1);
            L=s2.indexOf(" ");
            s=s2.substring(0,L);
            s2=s2.substring(L+1);
            n1=(int)Math.ceil(s.length()/(double)n);
            a=new char [n1][n];
            k=0;
            for(i=0;i
             {
                 if(i%2==0)
                   {
                       for(j=0;j
                         {
                             a[i][j]=s.charAt(k++);
                         }
                   }
                else
                   {
                       for(j=n-1;j>=0;j--)
                        {
                            a[i][j]=s.charAt(k++);
                        }
                    }
             }

             for(i=0;i
              {
                  for(j=0;j
                    {
                        s1=s1+a[j][i];
                    }
              }
             for(i=0;i
              {
                  for(j=0;j
                    {
                        System.out.print(a[i][j]+" ");
                    }
                  System.out.println();
              }
             System.out.println();
             s1=s1+" ";
     }
       j=0;
       for(i=0;i
         {
           if(s1.charAt(i)==' ')
             {
               s2=s1.substring(j,i);
               System.out.println(s2);
               j=i+1;
             }
  }
   }  
 }




Output:



Enter column and Sentence
5
Theres no place like home on a snowy night

Convert To :

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

toioynnkpheleaigshareconhtomesnlewx

Enter column and Sentence
5
toioynnkpheleaigshareconhtomesnlewx
Enter column and Sentence
3
thisistheeasyoneab
Enter column and Sentence
0

Convert Fro :

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x

t h i
s i s
t h e
s a e
y o n
b a e

theresnoplacelikehomeonasnowynightx
tstsybhihaoaiseene