Loading

Archive for 29 Aug 2012

C Program to Print Date

C - Program to Print Date


#include<stdio.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   struct date d;
 
   getdate(&d);
 
   printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
   getch();
   return 0;
}

Posted in | 1 Comment

C Program to Get IP Address of Your Computer

C Program to Get IP Address of Your Computer


#include<stdlib.h>
 
main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");
 
   return 0;
}

Posted in | 2 Comments

c program to list files in directory

C - Program to List Files in Directory


#include<stdio.h>
#include<conio.h>
#include<dir.h>
 
main()
{
   int done;
   struct ffblk a;
 
   printf("Press any key to view the files in the current directory\n");
 
   getch();
 
   done = findfirst("*.*",&a,0);
 
   while(!done)
   {
      printf("%s\n",a.ff_name);
      done = findnext(&a);
   }
 
   getch();
   return 0;
}

Posted in | 1 Comment

C Program to Delete Element from Array

C - Program to Delete Element from  Array


#include <stdio.h>
 
main()
{
   int array[100], position, c, n;
 
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);
 
   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];
 
      printf("Resultant array is\n");
 
      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }
 
   return 0;
}

Posted in | 1 Comment

C Program to Insert Element in Array

C - Program to Insert an Element in an Array


#include <stdio.h>
 
int main()
{
   int array[100], position, c, n, value;
 
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);
 
   printf("Enter the value to insert\n");
   scanf("%d", &value);
 
   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];
 
   array[position-1] = value;
 
   printf("Resultant array is\n");
 
   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);
 
   return 0;
}

Posted in | Leave a comment

C Program to Reverse an Array

C - Program to Reverse an Array


#include <stdio.h>
 
int main()
{
   int n, c, d, a[100], b[100];
 
   printf("Enter the number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter the array elements\n");
 
   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);
 
   /*
    * Copying elements into array b starting from end of array a
    */
 
   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];
 
   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */
 
   for (c = 0; c < n; c++)
      a[c] = b[c];
 
   printf("Reverse array is\n");
 
   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);
 
   return 0;
}

Posted in | 1 Comment

C Program for Linear Search

C - Program for Linear Search


#include<stdio.h>
 
main()
{
   int array[100], search, c, number;
 
   printf("Enter the number of elements in array\n");
   scanf("%d",&number);
 
   printf("Enter %d numbers\n", number);
 
   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d",&search);
 
   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
  break;
      }
   }
   if ( c == number )
      printf("%d is not present in array.\n", search);     
 
   return 0;
}

Posted in | Leave a comment

C - Program to Print Floyd's Traingle

C - Program to Print Floyd's  Traingle


#include <stdio.h>
 
int main()
{
  int n, i,  c, a = 1;
 
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
 
  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
 
  return 0;
}

Posted in | Leave a comment

C- Program for Binary Search

C - Program for Binary Search

 

#include<stdio.h>
#include<conio.h>

void insertionsort(int a[],int n)

{
   int i,j,temp;
    for(i=1;i<n;i++)
        temp=a[i];
        for(j=n-1;j>=0 && a[j]<temp;j--)
        a[j+1]=a[j];
        a[j+1]=temp;

}


void main()
{
  int i,j,key,n,a[100],flag,c;
  clrscr();
  printf("\nENTER THE NO. OF ELEMENTS:");
  scanf("%d",&n);
  printf("\nENTER THE ELEMENTS:");
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  insertionsort(a,n);
  printf("\nENTER THE ELEMENT TO BE SEARCHED:");
  scanf("%d",&key);
  flag=0;
  for(i=0,j=n-1;i<=j;)
  {
    c=(i+j)/2;
    if(a[c]<key)
        i=c+1;
    if(a[c]>key)
        j=c-1;
    if(a[c]==key)
           {
        printf("ELEMENT FOUND AT POSITION:%d",c+1);
        flag=1;
        break;
        }
   }
    if(flag==0)
        printf("ELEMENT NOT FOUND!!!!");
   getch();
}

Posted in | Leave a comment

C Program for Bubble Sort

C - Program for Bubble Sort


#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
 
  return 0;
}

Posted in | Leave a comment

C Program for Dijkstras Algorithm

C - Program to Implement Dijkstra's Algorithm


#include<stdio.h>
#include<conio.h>
#define MAX 30
#define infinity 9999

void dijkstra(int g[MAX][MAX],int n,int startnode);

void main()
{
  int g[MAX][MAX],i,j,n;
  int edges,u,v,w;
  clrscr();

  printf("\nEnter the no. of Vertices::");
  scanf("%d",&n);
  printf("\nEnter the no. of Edges::");
  scanf("%d",&edges);
  printf("\nEnter the Edges(u,v,weight)::\n");
  for(i=0;i<edges;i++)
  {
    scanf("%d%d%d",&u,&v,&w);
    g[u][v]=g[v][u]=w;
  }

  printf("\nEnter the Start node::");
  scanf("%d",&u);
  dijkstra(g,n,u);
  getch();
}

void dijkstra(int g[MAX][MAX],int n,int startnode)
{
  int cost[MAX][MAX],dist[MAX],pred[MAX];
  int visited[MAX],count,mindist,nextnode,i,j;

  for(i=0;i<n;i++)                                 //create cost matrix
    for(j=0;j<n;j++)
        if(g[i][j]==0)
            cost[i][j]=infinity;
        else
            cost[i][j]=g[i][j];

  for(i=0;i<n;i++)
  {
    dist[i]=cost[startnode][i];
    pred[i]=startnode;
    visited[i]=0;
   }

   visited[startnode]=1;
   dist[startnode]=0;
   count=1;
   while(count<n-1)
   {
     mindist=infinity;
     //next node is node at min. distance
     for(i=0;i<n;i++)
    if(dist[i]<mindist && !visited[i])
    {
      mindist=dist[i];
      nextnode=i;
    }
     visited[nextnode]=1;
     //check if better path exists
     for(i=0;i<n;i++)
    if(!visited[i])
        if(mindist+cost[nextnode][i]<dist[i])
        {
            dist[i]=mindist+cost[nextnode][i];
            pred[i]=nextnode;
         }
     count++;
  }

  //print the path and distance of each node
  for(i=0;i<n;i++)
    if(i!=startnode)
    {
      printf("\nDistance of %d=%d",i,dist[i]);
      printf("\nPath=%d",i);
      j=i;
      do
      {
       j=pred[j];
       printf("<-%d",j);
      }while(j!=startnode);
    }
}

Posted in | Leave a comment

C Program for Huffmans Algorithm

 C - Program for Huffman's Algorithm



#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

// stucture of tree node
typedef struct treenode
{
    float freq;
    char data;
    struct treenode *left,*right;
}treenode;

/* structure of node of linked list */
typedef struct node
{
    treenode *data; //address of tree
    struct node *next;
}node;

node *insert(node *,treenode *);
treenode *create();
void encode();
void decode(treenode *);
int n;
char alphabets[30];
char codes[30][10];

void preorder(treenode *p,int i ,char word[])
 {
    if(p!=NULL)
       {
        if(p->left==NULL && p->right==NULL)
           {
            word[i]=0;
            printf("\n%c --- %s",p->data,word);
            alphabets[n]=p->data;
            strcpy(codes[n++],word);
           }
        word[i]='0';
        preorder(p->left,i+1,word);
        word[i]='1';
        preorder(p->right,i+1,word);
       }
 }

void main()
 {
    int op;char word[10];
    treenode *root=NULL;
    clrscr();
    do
       {
        printf("\n\n1)Create Huffman Tree ");
        printf("\n2)Encode a Message ");
        printf("\n3)Decode a message ");
        printf("\n4)Quit");
        printf("\nEnter Your Choice : ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: n=0;root=create();
                printf("\nPrefix codes : \n");
                preorder(root,0,word); // create the encoding sequence
                break;
            case 2: encode(); break;
            case 3: decode(root);break;
           }
       }while(op!=4);
}

treenode *create()
{
    treenode *p,*t1,*t2;
    node *head;
    int n,i;
    char x;
    float probability;
    head=NULL;       //empty linked list
    printf("\nEnter No. of alphabets :");
    scanf("%d",&n);
    for(i=0;i<n;i++)
       {
        flushall();
        printf("\nEnter alphabet :");
        scanf("%c",&x);
        printf("\nEnter frequency :");
        scanf("%f",&probability);
      /* create a new tree and insert it in
         the priority linked list */
        p=(treenode*)malloc(sizeof(treenode));
        p->left=p->right=NULL;
        p->data=x;
        p->freq=probability;
        head=insert(head,p);
      }
    /* create the final tree by merging of two trees
       of smaller weights (n-1) merges will be required*/
    for(i=1;i<n;i++)
       {
        t1=head->data;         //first tree
        t2=head->next->data;   //second tree
        head=head->next->next; /*remove first 2 trees
                 from linked list*/
      /*merge t1 and t2 with new tree in P */
        p=(treenode *)malloc(sizeof(treenode));
        p->left=t1;
        p->right=t2;
        p->freq=t1->freq+t2->freq;
        head=insert(head,p); /*insert the new tree in the linked list*/
       }

    return(head->data);
// preorder(head->data);
 //getch();
}

node *insert(node *head,treenode *t)
{
    node *p,*q;
    p=(node *)malloc(sizeof(node));
    p->data=t;
    p->next=NULL;
    if(head==NULL)  //empty linked list
        return(p);
    if(t->freq<head->data->freq)
       {
        p->next=head;
        return(p);
       }
    // locate the point of insertion
    q=head;
    while(q->next!=NULL && t->freq>q->next->data->freq)
        q=q->next;
    p->next=q->next;
    q->next=p;
    return(head);
}

void encode()
{
    char word[30];int i,j;
    flushall();
    printf("\n Enter a Message : ");
    gets(word);
    printf("\n Encoded Message \n");
    for(i=0;word[i]!='\0';i++)
      {
        for(j=0;alphabets[j]!=word[i] && j<n;j++);
            if(j<n)
                printf("%s",codes[j]);
      }
}

void decode(treenode *p)
{
    char word[90];int i;treenode *q;
    flushall();
    printf("\nEnter an Encoded message : ");
    gets(word);
    q=p;i=0;
    printf("\nDecoded Message = ");
    while(word[i]!='\0')
       {
        if(word[i]=='0')
            q=q->left;
        else
            q=q->right;
        if(q->left==NULL && q->right==NULL)
            {
            printf("%c",q->data);
            q=p;
            }
        i++;
       }

}

Posted in | Leave a comment

C Program for Paint

 C - Program for Paint


#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>

union REGS i, o;
int leftcolor[15];

int get_key()
{
   union REGS i,o;

   i.h.ah = 0;
   int86(22,&i,&o);

   return ( o.h.ah );
}

void draw_color_panel()
{
   int left, top, c, color;

   left = 100;
   top = 436;

   color = getcolor();
   setcolor(GREEN);
   rectangle(4,431,635,457);
   setcolor(RED);
   settextstyle(TRIPLEX_FONT,0,2);
   outtextxy(10,431,"Colors : ");

   for( c = 1 ; c <= 15 ; c++ )
   {
      setfillstyle(SOLID_FILL, c);
      bar(left, top, left+16, top+16);
      leftcolor[c-1] = left;
      left += 26;
   }

   setcolor(color);
}

void draw_shape_panel()
{
   int left, top, c, color;

   left = 529;
   top = 45;

   color = getcolor();
   setcolor(GREEN);
   rectangle(525,40,633,255);

   for( c = 1 ; c <= 7 ; c++ )
   {
      rectangle(left, top, left+100, top+25);
      top += 30;
   }
   setcolor(RED);
   outtextxy(530,45,"Bar");
   outtextxy(530,75,"Line");
   outtextxy(530,105,"Pixel");
   outtextxy(530,135,"Ellipse");
   outtextxy(530,165,"Freehand");
   outtextxy(530,195,"Rectangle");
   outtextxy(530,225,"Clear");
   setcolor(color);
}

void change_color(int x, int y)
{
   int c;

   for( c = 0 ; c <= 13 ; c++ )
   {
      if( x > leftcolor[c] && x < leftcolor[c+1] && y > 437 && y < 453 )
         setcolor(c+1);
      if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 )
         setcolor(WHITE);
   }
}

char change_shape(int x, int y)
{
   if ( x > 529 && x < 625 && y > 45 && y < 70 )
      return 'b';
   else if ( x > 529 && x < 625 && y > 75 && y < 100 )
      return 'l';
   else if ( x > 529 && x < 625 && y > 105 && y < 130 )
      return 'p';
   else if ( x > 529 && x < 625 && y > 135 && y < 160 )
      return 'e';
   else if ( x > 529 && x < 625 && y > 165 && y < 190 )
      return 'f';
   else if ( x > 529 && x < 625 && y > 195 && y < 220 )
      return 'r';
   else if ( x > 529 && x < 625 && y > 225 && y < 250 )
      return 'c';
      return 0;
}

void showmouseptr()
{
   i.x.ax = 1;
   int86(0x33,&i,&o);
}

void hidemouseptr()
{
   i.x.ax = 2;
   int86(0x33,&i,&o);
}

void restrictmouseptr( int x1, int y1, int x2, int y2)
{
   i.x.ax = 7;
   i.x.cx = x1;
   i.x.dx = x2;
   int86(0x33,&i,&o);

   i.x.ax = 8;
   i.x.cx = y1;
   i.x.dx = y2;
   int86(0x33,&i,&o);
}

void getmousepos(int *button,int *x,int *y)
{
   i.x.ax = 3;
   int86(0x33,&i,&o);

   *button = o.x.bx;
   *x = o.x.cx;
   *y = o.x.dx;
}

main()
{
   int gd = DETECT,gm;

   int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
   char ch = 'f' ;            // default free-hand drawing

   initgraph(&gd,&gm,"C:\\TC\\BGI");

   maxx = getmaxx();
   maxy = getmaxy();

   setcolor(BLUE);
   rectangle(0,0,maxx,maxy);

   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

   draw_color_panel();
   draw_shape_panel();

   setviewport(1,1,maxx-1,maxy-1,1);

   restrictmouseptr(1,1,maxx-1,maxy-1);
   showmouseptr();
   rectangle(2,2,518,427);
   setviewport(1,1,519,428,1);

   while(1)
   {
      if(kbhit())
      {
         key = get_key();

         if( key == 1 )
         {
            closegraph();
            exit(0);
         }
      }

      getmousepos(&button,&x,&y);

      if( button == 1 )
      {
         if( x > 4 && x < 635 && y > 431 && y < 457 )
            change_color( x, y );
         else if ( x > 529 && x < 625 && y > 40 && y < 250 )
             ch = change_shape( x, y );

         temp1 = x ;
         temp2 = y ;

         if ( ch == 'f' )
         {
            hidemouseptr();
            while( button == 1 )
            {
               line(temp1,temp2,x,y);
               temp1 = x;
               temp2 = y;
               getmousepos(&button,&x,&y);
            }
            showmouseptr();
         }

         while( button == 1)
            getmousepos(&button,&x,&y);

         /* to avoid interference of mouse while drawing */
         hidemouseptr();

         if( ch == 'p')
            putpixel(x,y,getcolor());

         else if ( ch == 'b' )
         {
            setfillstyle(SOLID_FILL,getcolor());
            bar(temp1,temp2,x,y);
         }
         else if ( ch == 'l')
            line(temp1,temp2,x,y);
         else if ( ch == 'e')
            ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
         else if ( ch == 'r' )
            rectangle(temp1,temp2,x,y);
         else if ( ch == 'c' )
         {
            ch = 'f';          // setting to freehand drawing
            clearviewport();
            color = getcolor();
            setcolor(WHITE);
            rectangle(2,2,518,427);
            setcolor(color);
         }

         showmouseptr();
      }
   }
}

Posted in | Leave a comment

C Program to Find Length of String Using Command line Argument

C - Program to Find the Length of  String Using Command line Argument


#include < stdio.h >
#include < conio.h >

void main(int argc,char *argv[])
{
    int len;
    char *ptr;   
    clrscr();
    if ( argc != 2)
    {
        printf("\n Invalid arguments ");
        exit(0);
    }
    else
    {
        ptr=argv[1];
        len=0;
        while ( *(ptr+len) != '\0')
        {
            len++;
        }
        printf("\n Length is %d",len);
    }
}

Posted in | Leave a comment

C Program to Sort Names in Alphabetical Order

 C Program to Sort Names in Alphabetical Order


#include < stdio.h >
#include < conio.h >
#include < alloc.h >

void main()
{
    char *list[100],temp[10];
    int n,i,j;
    clrscr();
    printf("How many names?\n");
    scanf("%d",&n);
    printf("Enter %d names \n",n);
    for(i=0;i < n;i++)
    {
        list[i]=(char *)malloc(100);
        flushall();
        gets(list[i]);
    }
    for(i=0;i < n-1;i++)
    {
        for(j=i+1;j < n;j++)
        {
            strcmp(list[i],list[j]);
            {
                strcpy(temp,list[i]);
                strcpy(list[i],list[j]);
                strcpy(list[j],temp);
            }
        }
    }
    printf("The %d names in sorted order are\n",n);
    for(i=0;i < n;i++)
    {
        puts(list[i]);
    }
}//main

Posted in | Leave a comment

C Program for Heap Sort

C - Program for Heap Sort


#include < stdio.h >
#include < conio.h >

void main()
{
    int a[50];
    int n;
    clrscr();
    printf("\nEnter n: ");
    scanf("%d",&n);
    read_data(a,n);
    printf("\nBefore sort : \n");
    print_data(a,n);
    heap_sort(a,n);
    printf("\nAfter sort : \n");
    print_data(a,n);
}

int read_data(int a[],int max)
{
    int i;
    printf("\nEnter %d values \n",max);
    for(i=1;i<=max;i++)
    {
        scanf("%d",&a[i]);
    }
    return;
}

int print_data(int a[],int max)
{
    int i;
    for(i=1;i<=max;i++)
    {
        printf("%10d",a[i]);
    }
    return;
}

int heap_sort(int a[],int n)
{
    int i,j,t;
    for(i=n/2;i>=1;i--)
    {
        adjust(a,i,n);
    }
    for(i=n-1;i>=1;i--)
    {
        printf("\n");
        print_data(a,n);
        t=a[i+1];
        a[i+1]=a[1];
        a[1]=t;
        adjust(a,1,i);
    }
    return;
}
int adjust(int a[],int cur_pos,int max)
{
    int cur_rec,j;
    cur_rec=a[cur_pos];
    j=cur_pos * 2;
    while(j<=max)
    {
        if(j < max)
        {
        if(a[j] < a[j+1])
        {
            j=j+1;
        }
     }
    if(a[j] > cur_rec)
    {
        a[j/2]=a[j];
        j=j*2;
    }
 else
  break;
 }
 a[j/2]=cur_rec;
 return;
}

Posted in | Leave a comment

C Program to Convert decimal number to hexadecimal

C - Program to Convert decimal number to Hexadecimal

 


#include < stdio.h >
#include < conio.h > 
void main()
{
 int dec,rem[20],i,max,r;
 
 clrscr();
 printf("\n Enter a decimal number : ");
 scanf("%d",&dec);
 i = 0; 
 while ( dec != 0 )
 {
  r = dec % 16;
  dec = dec / 16;
  rem[i] = r;
  i++;
 } 
 i = i-1; 
 while ( i >=0)
 {
  if ( rem[i] < 10 )
  printf("%d",rem[i]);
  else
  printf("%c",55 + rem[i]);
  i--;
 } 
}



Posted in | Leave a comment