Thursday 7 March 2013

Anagrams comparision with character array


import java.util.*;
public class Anagram {

 //quite complex cos i was using array for comparison
    public static void main(String me[])  
    {
       String str1 =new String();
       String str2 =new String();
        char c1[] = new char[100];
        char c2[] = new char[100];
        int size1, size2;
        int match = 0;
           
   
        Scanner in = new Scanner(System.in);
       
             System.out.println(" Enter First Word: ");
             str1 = in.nextLine();
             System.out.println(" Enter Second Word: ");
             str2 = in.nextLine();
       
   

         size1 =str1.length(); //take the length of the first word
         size2 =str2.length(); //take the length of the second word
         System.out.println("sizes: " + size1 + "," + size2);
       
       
          if (size1 != size2)
          {
              System.out.println("Words are not of same lengt - NOT ANAGRAMS");
          }
         
          else
          {
         
              c1 = str1.toCharArray(); //takes word1 to char1
              c2 = str2.toCharArray();// takes word2 to char2
             
             int flag;
             flag = 0;
              for (int i=0;i<size1;i++)
              {
                  for (int k=0;k<size1;k++)
                  {
                     
                     // System.out.println(c1[i] + " ");
                     
                          if (c1[i] == c2[k])
                         {  flag = 1;} //move to next letter
                         
                   
                     
                      }
                  if (flag == 1){ match++;}
                 
                  }
               }
              System.out.println("match: " + match);
             
              if (match == size1)
  {   System.out.println("the words " + str1 + " and " + str2  + " are Anagrams!"); }
              else
              {
   System.out.println("the words " + str1 + " and " + str2  + " are NOT Anagrams!");
              }
             
          }
     

                     
    }
}

Wednesday 6 March 2013

Game of Life Problem



//COURSE: CSC822
import java.util.*; //for input from keyboard

public class game

{
 public static void main(String me[])  
 {
           System.out.println("Program Simulation for Game of Life Problem");


  Scanner input = new Scanner(System.in);

   int INITIAL_ARRAY[][] = new int[8][16];
   int FINAL_ARRAY[][] = new int[8][16];
   int  No_of_Neighbors,row,column,m,n,x,y,iterate,w;
   No_of_Neighbors = 0;

INITIAL_ARRAY[0][0] =1; INITIAL_ARRAY[0][1] =1; INITIAL_ARRAY[0][2] =0;
INITIAL_ARRAY[0][3] =1; INITIAL_ARRAY[0][4] =1; INITIAL_ARRAY[0][5] =1; INITIAL_ARRAY[0][6] =1; INITIAL_ARRAY[0][7] =1; INITIAL_ARRAY[0][8] =1; INITIAL_ARRAY[0][9] =1; INITIAL_ARRAY[0][10] =1; INITIAL_ARRAY[0][11] =0; INITIAL_ARRAY[0][12] =1; INITIAL_ARRAY[0][13] =1; INITIAL_ARRAY[0][14] =0; INITIAL_ARRAY[0][15] =1; INITIAL_ARRAY[1][0] =1; INITIAL_ARRAY[1][1] =0; INITIAL_ARRAY[1][2] =0; INITIAL_ARRAY[1][3] =0; INITIAL_ARRAY[1][4] =0; INITIAL_ARRAY[1][5] =0; INITIAL_ARRAY[1][6] =1; INITIAL_ARRAY[1][7] =1;
 INITIAL_ARRAY[1][8] =1; INITIAL_ARRAY[1][9] =0; INITIAL_ARRAY[1][10] =1; INITIAL_ARRAY[1][11] =0; INITIAL_ARRAY[1][12] =0; INITIAL_ARRAY[1][13] =0; INITIAL_ARRAY[1][14] =0; INITIAL_ARRAY[1][15] =1; INITIAL_ARRAY[2][0] =1; INITIAL_ARRAY[2][1] =0; INITIAL_ARRAY[2][2] =0; INITIAL_ARRAY[2][3] =0;
 INITIAL_ARRAY[2][4] =0; INITIAL_ARRAY[2][5] =1; INITIAL_ARRAY[2][6] =1; INITIAL_ARRAY[2][7] =1; INITIAL_ARRAY[2][8] =1; INITIAL_ARRAY[2][9] =1; INITIAL_ARRAY[2][10] =0; INITIAL_ARRAY[2][11] =1; INITIAL_ARRAY[2][12] =0; INITIAL_ARRAY[2][13] =0; INITIAL_ARRAY[2][14] =1; INITIAL_ARRAY[2][15] =1;
 INITIAL_ARRAY[3][0] =1; INITIAL_ARRAY[3][1] =0; INITIAL_ARRAY[3][2] =0; INITIAL_ARRAY[3][3] =1; INITIAL_ARRAY[3][4] =0; INITIAL_ARRAY[3][5] =1; INITIAL_ARRAY[3][6] =1; INITIAL_ARRAY[3][7] =1; INITIAL_ARRAY[3][8] =1; INITIAL_ARRAY[3][9] =0; INITIAL_ARRAY[3][10] =0; INITIAL_ARRAY[3][11] =1;
 INITIAL_ARRAY[3][12] =1; INITIAL_ARRAY[3][13] =1; INITIAL_ARRAY[3][14] =0; INITIAL_ARRAY[3][15] =0; INITIAL_ARRAY[4][0] =1; INITIAL_ARRAY[4][1] =0; INITIAL_ARRAY[4][2] =0; INITIAL_ARRAY[4][3] =1; INITIAL_ARRAY[4][4] =0; INITIAL_ARRAY[4][5] =1; INITIAL_ARRAY[4][6] =1; INITIAL_ARRAY[4][7] =0;
 INITIAL_ARRAY[4][8] =1; INITIAL_ARRAY[4][9] =0; INITIAL_ARRAY[4][10] =0; INITIAL_ARRAY[4][11] =1; INITIAL_ARRAY[4][12] =1; INITIAL_ARRAY[4][13] =1; INITIAL_ARRAY[4][14] =1; INITIAL_ARRAY[4][15] =1; INITIAL_ARRAY[5][0] =1; INITIAL_ARRAY[5][1] =0; INITIAL_ARRAY[5][2] =0; INITIAL_ARRAY[5][3] =0; INITIAL_ARRAY[5][4] =0; INITIAL_ARRAY[5][5] =0; INITIAL_ARRAY[5][6] =1; INITIAL_ARRAY[5][7] =1; INITIAL_ARRAY[5][8] =1; INITIAL_ARRAY[5][9] =1; INITIAL_ARRAY[5][10] =1; INITIAL_ARRAY[5][11] =1; INITIAL_ARRAY[5][12] =0; INITIAL_ARRAY[5][13] =0; INITIAL_ARRAY[5][14] =1; INITIAL_ARRAY[5][15] =0; INITIAL_ARRAY[6][0] =1; INITIAL_ARRAY[6][1] =0; INITIAL_ARRAY[6][2] =0; INITIAL_ARRAY[6][3] =0; INITIAL_ARRAY[6][4] =0; INITIAL_ARRAY[6][5] =0; INITIAL_ARRAY[6][6] =1; INITIAL_ARRAY[6][7] =1; INITIAL_ARRAY[6][8] =1; INITIAL_ARRAY[6][9] =0; INITIAL_ARRAY[6][10] =1; INITIAL_ARRAY[6][11] =1; INITIAL_ARRAY[6][12] =0; INITIAL_ARRAY[6][13] =1; INITIAL_ARRAY[6][14] =1; INITIAL_ARRAY[6][15] =1; INITIAL_ARRAY[7][0] =1; INITIAL_ARRAY[7][1] =1; INITIAL_ARRAY[7][2] =1; INITIAL_ARRAY[7][3] =1; INITIAL_ARRAY[7][4] =1; INITIAL_ARRAY[7][5] =1; INITIAL_ARRAY[7][6] =1; INITIAL_ARRAY[7][7] =1; INITIAL_ARRAY[7][8] =0; INITIAL_ARRAY[7][9] =0; INITIAL_ARRAY[7][10] =0; INITIAL_ARRAY[7][11] =1; INITIAL_ARRAY[7][12] =1; INITIAL_ARRAY[7][13] =1; INITIAL_ARRAY[7][14] =1; INITIAL_ARRAY[7][15] =1;

       for (row = 0;row<=7;row++)   //Moving result  to Final_ array
   {
        for (column = 0;column<=15;column++)
        {
           FINAL_ARRAY[row][column] = INITIAL_ARRAY[row][column];
          System.in
        }
   }

 //Display the result
 System.out.println("Initial Result");

   for ( row = 0;row<=7;row++)   //inputing into the array
   {
         System.out.println(); //make new line
        for ( column= 0;column<=15;column++)
        {

           if (FINAL_ARRAY[row][column] == 1)
           {  System.out.print("." + "  "); }
           else
           {
           System.out.print(FINAL_ARRAY[row][column] + "  ");
           }
        }
        System.out.println(); //make new line
   }
 
  System.out.println("Enter number of iterations? ");
     int iterations = input.nextInt();

 //start iteration
 for ( iterate=1;iterate<=iterations;iterate++)
 {

   for (row = 0;row<=7;row++)   //running through the whole array (board)
   {
        for ( column = 0;column<=15;column++)
        {
          No_of_Neighbors = 0;
        x = row;
        y = column;

        //Check for the neighbours around this cell
           for (m = x - 1;m<=x + 1 ;m++)   //loop through the eight cells around each cell
           {
                for ( n = y - 1;n<=y + 1 ;n++)
                {
                   if ((m >= 0) && (m <=7) && (n >=0 ) && (n<=15) )
                   {

                        if (INITIAL_ARRAY[m][n] == 0)
                        {
                        No_of_Neighbors++;
                        }

                   }


                }
           }   //this ends the loop for searching out neighbors


             //this is to make sure the cell in question is not counted as a neighbor
             if (INITIAL_ARRAY[row][column] == 0)
             {
                No_of_Neighbors-- ;
             }

            //All rules are applied at this stage with the number of neighbors gotten
       if (   ( (No_of_Neighbors == 2) || (No_of_Neighbors == 3))  && (INITIAL_ARRAY[row][column] == 0) )
           {
           FINAL_ARRAY[row][column] = 0; //this cell should survive
           }

       if (   ( (No_of_Neighbors >= 4) || (No_of_Neighbors == 1))  && (INITIAL_ARRAY[row][column] == 0) )
           {
           FINAL_ARRAY[row][column] = 1; //this cell should die
           }

       if (  (No_of_Neighbors == 3)   && (INITIAL_ARRAY[row][column] == 1)  )
           {
           FINAL_ARRAY[row][column] = 0; //a counter is born to this cell
           }

        }

    } //end of main loop through whole array
 

   for (row = 0;row<=7;row++)   //updating Array A with current B
   {
        for ( column  = 0;column<=15;column++)
        {

          INITIAL_ARRAY[row][column] = FINAL_ARRAY[row][column];
        }
   }




 //this part bring out the Result
 System.out.println("Final Result");

   for ( row = 0;row<=7;row++)   //inputing into the array
   {
         System.out.println(); //make new line
        for ( column= 0;column<=15;column++)
        {

           if (FINAL_ARRAY[row][column] == 1)
           {  System.out.print("  " + "  "); }
          else
           {
           System.out.print(FINAL_ARRAY[row][column] + "  ");
           }
        }
        System.out.println(); //arrange in a new line
   }

}//this is the end of n iterations

 }//end of public stat
}//end of the program class
                                                         

Output: