Thursday, May 26, 2011

notes on calloc and malloc

calloc() allocates memory for an array of nmemb elements of sizebytes each and returns a pointer to the allocated memory. The memory is set to zero.


malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(),


calloc() orrealloc(). Otherwise, or if free(ptr)has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unlessptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

Saturday, May 7, 2011

Java program for display difference between two date

class date_difference
{
int r=0;
public int datechk(int d,int m,int y)
{
if((y>100)&&(y<9999))
{
if((m==2)&&(y%4==0))
{
if((d>=1)&&(d<=29))
 
      r=1;
  else
  r=0;
    }
    else if((m==2)&&(y%4!=0))
    {
        if((d>=1)&&(d<=28))
       
            r=1;
            else
            r=0;
       
    }
   
    else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
    {
        if((d>=1)&&(d<=31))
       
            r=1;
            else
            r=0;
       
    }
    else if((m==4)||(m==6)||(m==9)||(m==11))
    {
        if((d>=1)&&(d<=30))
       
            r=1;
            else
            r=0;
       
    }   
    else
       r=0;
      
    }
    return r;
}
      
       public void main(int d1,int d2, int m1,int m2,int y1,int y2)
       {
           int mo[]={31,28,31,30,31,30,31,31,30,31,30,31};
           int f,f1,i,tot=0;
           f=datechk(d1,m1,y1);
           f1=datechk(d2,m2,y2);
          
          
           if(f==1 && f1==1)
        {
            if(y1==y2)
            {
                if(y1%4==0)
                mo[1]=29;
                for(i=m1+1;i<m2;i++)
                    tot+=mo[i];
                if(m1 == m2)
                    tot+=d2-d1;
                else
                    {
                    tot+=((mo[m1-1]-d1)+1);
                    tot+=d2;
                    }
                System.out.println(tot);
                }
            else if(y1<y2)
            {
                for(i=(y1+1);i<y2;i++)
                {
                    tot+=(i%4==0?366:365);
                }
                if(y1%4==0)
                mo[1]=0;
                for(i=m1;i<12;i++)
                tot+=mo[i];
                tot+=((mo[m1-1]-d1)+1);
                if(y2%4==0)
                mo[1]=29;
                for(i=0;i<m2-1;i++)
                tot+=mo[i];
                tot+=d2;
                System.out.println(tot);
            }
            else
            System.out.println("invalid years");
            }
        }
    }
       
      
       
       
       
       
       
       

Java program for display difference between two date

class date_difference
{
int r=0;
public int datechk(int d,int m,int y)
{
if((y>100)&&(y<9999))
{
if((m==2)&&(y%4==0))
{
if((d>=1)&&(d<=29))
 
      r=1;
  else
  r=0;
    }
    else if((m==2)&&(y%4!=0))
    {
        if((d>=1)&&(d<=28))
       
            r=1;
            else
            r=0;
       
    }
   
    else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
    {
        if((d>=1)&&(d<=31))
       
            r=1;
            else
            r=0;
       
    }
    else if((m==4)||(m==6)||(m==9)||(m==11))
    {
        if((d>=1)&&(d<=30))
       
            r=1;
            else
            r=0;
       
    }   
    else
       r=0;
      
    }
    return r;
}
      
       public void main(int d1,int d2, int m1,int m2,int y1,int y2)
       {
           int mo[]={31,28,31,30,31,30,31,31,30,31,30,31};
           int f,f1,i,tot=0;
           f=datechk(d1,m1,y1);
           f1=datechk(d2,m2,y2);
          
          
           if(f==1 && f1==1)
        {
            if(y1==y2)
            {
                if(y1%4==0)
                mo[1]=29;
                for(i=m1+1;i<m2;i++)
                    tot+=mo[i];
                if(m1 == m2)
                    tot+=d2-d1;
                else
                    {
                    tot+=((mo[m1-1]-d1)+1);
                    tot+=d2;
                    }
                System.out.println(tot);
                }
            else if(y1<y2)
            {
                for(i=(y1+1);i<y2;i++)
                {
                    tot+=(i%4==0?366:365);
                }
                if(y1%4==0)
                mo[1]=0;
                for(i=m1;i<12;i++)
                tot+=mo[i];
                tot+=((mo[m1-1]-d1)+1);
                if(y2%4==0)
                mo[1]=29;
                for(i=0;i<m2-1;i++)
                tot+=mo[i];
                tot+=d2;
                System.out.println(tot);
            }
            else
            System.out.println("invalid years");
            }
        }
    }
       
      
       
       
       
       
       
       

Java program for insert or delete element using array

import java.io.*;
class arr
    {
        public static void main(String args[])throws IOException
            {
                BufferedReader br=new BufferedReader(
                    new InputStreamReader(System.in));
                int ar[]=new int[100];
                int n,i,j,pos,ch;
                System.out.print("Enter number of element :-");
                n=Integer.parseInt(br.readLine());
                if(n<=99)
                    {
                        for(i=0;i<n;i++)
                            {
                                System.out.print("Enter any no :-");
                                ar[i]=Integer.parseInt(br.readLine());
                            }
                        do
                            {
                                System.out.println("\n1.Ele insert...");
                                System.out.println("2.Ele delete...");
                                System.out.println("3.Ele display...");
                                System.out.println("4.Exit");
                                System.out.print("Enter your choice :-");
                                ch=Integer.parseInt(br.readLine());
                                switch(ch)
                                    {
                                        case 1:
                                            System.out.print("Enter position number :-");
                                            pos=Integer.parseInt(br.readLine());
                                            if(pos<n)
                                                {
                                                    for(j=n-1;j>=pos-1;j--)
                                                        ar[j+1]=ar[j];
                                                    System.out.print("Enter new inserting element :-");
                                                    ar[j+1]=Integer.parseInt(br.readLine());
                                                    n++;
                                                }
                                            else
                                                System.out.println("Sory invalid position number enterde.....");
                                            break;
                                        case 2:
                                            System.out.print("Enter position number :-");
                                            pos=Integer.parseInt(br.readLine());
                                            if(pos<n)
                                                {
                                                    for(j=pos-1;j<n;j++)
                                                        ar[j]=ar[j+1];
                                                    System.out.println("element deleted......");
                                                    n--;
                                                }
                                            else
                                                System.out.println("Sory invalid position number enterde.....");
                                            break;
                                        case 3:
                                            for(i=0;i<n;i++)
                                                System.out.print(ar[i]+"\t");
                                            System.out.println();
                                            break;
                                        case 4:
                                            System.out.println("Program end.......");
                                            break;
                                        default:
                                            System.out.println("Invalid enter select......");
                                    }
                            }while(ch != 4 );
                    }
                else
                    System.out.println("Out of range........");
                   
            }
    }

Java Prog for convert any base to any base

import java.io.*;
class anybase_anybase
{
public String deci_to_other(int no,int b)
{
String cno="";
int r;
         while(no>0)
         {
             r=no%b;
             if(r>9)
             {
                 r+=55;
                 char c=(char)r;
                 cno=c+cno;
                }
                else
                {
                    cno=r+cno;
                }
                no=no/b;
            }
                   return cno;
           
        }
   
   
    public int other_to_decimal (String s,int base)
{
int g,num=0,a=0;
   
  
int l=s.length();
for(int i=l-1;i>=0;i--)
{
char c=s.charAt(i);
if(c>=65 && c<=70)
{c-=55;
 g=c;

num=num+(g*((int)Math.pow(base,a)));
    }
    else
    {
        c-=48;
         g=c;
        
         num=num+(g*((int)Math.pow(base,a)));
        }
        a++;
    }
   return num;
}
   
   
    public  void meth()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        {
            System.out.println("enter choice");
            System.out.println("1.to convert from decimal number to number in other base");
            System.out.println("2.to convert from number in other base to decimal no");
            System.out.println("3.to convert no in any base to number in any base");
           
            int n=Integer.parseInt(br.readLine());
           
            switch(n)
            {
                case 1:
                {
                    System.out.println("enter no and base both ");
                    int no=Integer.parseInt(br.readLine());
                    int b=Integer.parseInt(br.readLine());
                   
                    String f=deci_to_other(no,b);
                    System.out.println(f);break;
               
            }
                case 2:
             {  
                    System.out.println("enter no in String and base");
                    String s=br.readLine();
                    int b=Integer.parseInt(br.readLine());
                   
                    int f=other_to_decimal(s,b);
                    System.out.println(f);break;
                } 
                    case 3:
                {
                    System.out.println("enter no ,its base and the base to which it is to be converted");
                     String s=br.readLine();
                 int b=Integer.parseInt(br.readLine());
                  int bas=Integer.parseInt(br.readLine());
                    int k=other_to_decimal(s,b);
                    String f=deci_to_other(k,bas);
                    System.out.println(f);
                }}
            }
                   
                }
            }
                   

Java Prog for convert any base to any base

import java.io.*;
class anybase_anybase
{
public String deci_to_other(int no,int b)
{
String cno="";
int r;
         while(no>0)
         {
             r=no%b;
             if(r>9)
             {
                 r+=55;
                 char c=(char)r;
                 cno=c+cno;
                }
                else
                {
                    cno=r+cno;
                }
                no=no/b;
            }
                   return cno;
           
        }
   
   
    public int other_to_decimal (String s,int base)
{
int g,num=0,a=0;
   
  
int l=s.length();
for(int i=l-1;i>=0;i--)
{
char c=s.charAt(i);
if(c>=65 && c<=70)
{c-=55;
 g=c;

num=num+(g*((int)Math.pow(base,a)));
    }
    else
    {
        c-=48;
         g=c;
        
         num=num+(g*((int)Math.pow(base,a)));
        }
        a++;
    }
   return num;
}
   
   
    public  void meth()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        {
            System.out.println("enter choice");
            System.out.println("1.to convert from decimal number to number in other base");
            System.out.println("2.to convert from number in other base to decimal no");
            System.out.println("3.to convert no in any base to number in any base");
           
            int n=Integer.parseInt(br.readLine());
           
            switch(n)
            {
                case 1:
                {
                    System.out.println("enter no and base both ");
                    int no=Integer.parseInt(br.readLine());
                    int b=Integer.parseInt(br.readLine());
                   
                    String f=deci_to_other(no,b);
                    System.out.println(f);break;
               
            }
                case 2:
             {  
                    System.out.println("enter no in String and base");
                    String s=br.readLine();
                    int b=Integer.parseInt(br.readLine());
                   
                    int f=other_to_decimal(s,b);
                    System.out.println(f);break;
                } 
                    case 3:
                {
                    System.out.println("enter no ,its base and the base to which it is to be converted");
                     String s=br.readLine();
                 int b=Integer.parseInt(br.readLine());
                  int bas=Integer.parseInt(br.readLine());
                    int k=other_to_decimal(s,b);
                    String f=deci_to_other(k,bas);
                    System.out.println(f);
                }}
            }
                   
                }
            }