Pages

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");
    }
  }
}


















No comments:

Post a Comment