Pages

Saturday, January 31, 2015

2D ARRAY

Declaration :--

datatype arr[][];
      
      or

datatype [][]arr;

int a[][];


Memory Allocation:--


arr = new datatype[rowwise][colum wise];

a = new int [3][4];

marks = new int[25][5];

scales = new float[10][4];

int[][] = new int [3][4];




INITIALIZATION:--


int a[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};


      1   2   3   4
      5   6   7   8
      9  10 11 12






















SHORTING IN JAVA

SHORTING



1.)Selection Sort:--


class sel Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=i+1; j<n; j++)
{
  if(a[i] > a[j])
 {
   t = a[i];
   a[i] = a[j];
   a[j] = t;
 }
}
}

System.out.println("\n shorted list");

for(i=o; i<n; i++)
system.out.print( a[i] + "\t");
   }
  }
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}



2.) Insertion Short:--


class inser Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=i; j<0; j--)
{
  if(a[j-1] > a[j])
 {
   t = a[j-1];
   a[j-1] = a[j];
   a[j] = t;
 }
}
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}




2.) Bubble Short:--


class bub Shot
{
 public static void main(string args[]){

int a[] = {                   };
int i,j.t;
int n= a.length;

system.out.println("Array");

for(i=0; i<n; i++)
{
system.out.println(a[i] + "\t")
}

for(i=0; i<n; i++)
{
 for(j=0; j<n-1; j++)
{
  if(a[j] > a[j+1])
 {
   t = a[j];
   a[j] = a[j+1];
   a[j+1] = t;
 }
}
}
 system.out.println("\n shorted array")
 for(i=o; i<n; i++)
system.out.print(a[i]+ "\t");
}
}
}





























SEARCHING


1.) Sequential Search(Linear Searching)


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int i, pas = -1;

for(i=0; i<a.length; i++)
 {
   if(a[i] == x)
    {
      pas = i;
      break;
     }
  }
if(pas == -1)

system.out.println("value not found");
else
   system.out.println("value found at index:-" +pas);
  }
}






2.) Binary Search :--


class seqSearch
{
 puclic static void main (string args[]){

int x = Integer.parseInt(args[0]);

int a[x] = {                          };

int first = 0, last = a.length-1, mid, pas = -1;

while(first<=last)
{
 mid = (first+last)/2;
if( x > a[mid])
first = mid +1;
else if(x<a[a])
 last= mid-1;
else
{
 pass= mid;
break;
 }
}











Friday, January 30, 2015

ARRAYs

ARRAYs



Declaration:-


datatype arr name[];
        or
datatype[] arrname;



MEMORY ALLOCATION :-

array name = new datatype[size]

a = new int [10]
per =  new float[20]



INITIALIZATION :-



int a[] = {3,6,4,1,8,5,9,2}



SIZE:-

n = a.length;


RUN TIME INPUT 


import java.io.*;

class runime
{
 public static void main (string args[])throws IO Exception{

 InputStreamReader input = new InputStreamReader(system.in);
 BufferedReader in = new BufferedReader(input);

 system.out.println("enter 2 numbers");
 string val = in.read lin();
 int a = Integer.parseInt(val);
 int b = Integer.parseInt(in.readLine());
 int c = a+b;
 system.out.println("sum=" +c);
 }
}




CREATING A SIZED ARRAY :--



import java.io.*;

class arr
{
 public static void main(string args[])throws IO Exception{
BufferReader in = new BufferReader(new ISR(system.in));

int n,i;

system.out.println("enter size of array");
n = Integer.parseInt(in.readLIne());

int a[] = new int[n];

for(i=0; i<a.length; i++);
 {
  system.out.println("enter value");
  a{i] = Integer.parseInt(in.readLine);
 }

system.out.println("Array");

int s = 0;

for(i=0; i<n; i++)
 {
   system.out.println(a[i] +"\t");
   s = s+a[i];
 }

system.out.println("\n sum =" +s);

float avg = (float) s/n;
system.out.println("Avg =" +avg);
 }
}








COUNT EVEN OR ODD ELEMENTs :--







class arr
{
 public static void main(string args[])throws IO Exception{

BufferReader in = new BufferReader(new ISR(system.in));

int n,i;

system.out.println("enter size of array");
n = Integer.parseInt(in.readLIne());

int a[] = new int[n];

for(i=0; i<a.length; i++);
 {
  system.out.println("enter value");
  a{i] = Integer.parseInt(in.readLine);
 }

system.out.println("Array");

int even = 0;
int odd = 0;

for(i=0; i<n; i++)
{
system.out.println( a[i] + "\t");
 if(a[a]%2==0)
    even++;
else
    odd++;
 }

system.out.println("Even elements =" +even);

system.out.println("Odd elements =" +odd);
  }
}




REVERSE OF ARRAY:--



class ArrRev
{
 public static void main(srting args[]){

int a[] = {                         };
int i.j.t;
system.out.println("Array");

for(i=0; i<a.length; i++)
 {
  system.out.println(a[i] +"\t");
 }

for(i=0, j= a.length-1; i<a.length/2; i++, j--)

 {
  t = a[i];
  a[i] = a[j];
  a[j] = t;
 }

system.out.println("Reversed Array");

for(i=0; i=a.length; i++)
   {
     system.out.println(a[i] + "\t");
    }
  }
}


















Thursday, January 29, 2015

LOOPs

LOOPs


1.) for loop

2.) while loop

3.) do while loop



FIRSTLY WE ARE GOING TO TALK ABOUT FOR LOOP 

In programming language FOR loop is used to allow code executed repeatedly. For loop is useful when you know how many times a task to be repeatedly.

example :-
       
                 for(exp1; exp2; exp3)
                      {
                         statements;
                      }

where : 
    
     exp1 :- initialization expression 
     exp2 :- test expression
     exp3 :- update expression.


EXAMPLE FOR CHECK WEATHER NUMBER IS PRIME OR NOT


 class prime 
{
 public static void main(string args[]){

int n = Integer.parseInt(args[0]);
boolean flag = true;
for(int i=2; i<n/2; i++)
 {
   if(n%i==0)
    {
      d=i;
      flag = false
      break;
     }
  }
if(flag)
system.out.println("number is prime");
else
system.out.println("number is not prime");
  }
}



NESTED LOOP



The placing of one loop inside the body of another loop is called nesting. The most common nested loop are for loop.

Example of nested loop a table up to n numbers :-


class table
{
 public static void main(string args[]){

for(int i=1; i<=10; i++)
  {
    for(int j=1; j<=10; j++)
    {
      system.out.println(i + "" + j);
    }
  }
}




WHILE LOOP




In programming language WHILE loop is used to control flow statement that allow code to repeatedly executed based on the condition.


example :-

              while(condition)
                   {
                    statements
                    ________
                     ________
                    }



Example for sum of digits


class digitSum

{
  public static void main(string args[]){

  int n = Integer.parseInt(args[0]);
  int r,s = 0;
  while (n!=0)
    {
      r = n%10;
      s = s+r;
      n = n/10;
    }
system.out.println("sum of digits="+s);

 }
}




DO WHILE LOOP


In programming language DO WHILE loop is used to control flow statement that executes a part of code single time and then repeatedly executes the part of code, its depend on the giving condition in the program. 


example:-

        do
            {
              statements;
                _______
                _______
             }
         while(condition)


Example to print number from 1 to 19


class example
{
 public static void main(string args[]){

int x= 1;

do
  {
    system.out.println("x is :=" +x);
    x++;
   system.out.println("/n");
   }
 while(x=<20);
 }
}