Saturday 29 December 2012

Mobile Bill Generator Program in C

#include<stdio.h>

int main()
{
    int calls,c1; float bill,rental;
  
    printf("Program for call billing\n\n");
    printf("                    WELCOME TO RELAINCE COMMUNICATION \n\n\n");
    printf("Enter the no. of Calls= ");
    scanf("%d",&calls);

    if (calls>0 && calls<=100)   /*100*0-->rs.0/call 100*.50-->50 paisa/call*/
        bill=calls*0;

    else if (calls>100 && calls<=200)
          {    c1=calls-100;
        bill= (100*0)+(c1*1);  }

    else if (calls>200 && calls<=300)
          {    c1=calls-200;
        bill=(100*0)+(100*1)+(c1*0.5);  }

    else if (calls>300 && calls<=500)
          {    c1=calls-300;
        bill=(100*0)+(100*1)+(100*0.5)+(c1*0.3); }

    else
          {    c1=calls-500;
        bill=(100*0)+(100*1)+(100*0.5)+(100*0.3)+(c1*0.2); }

    rental=bill+100;

    printf("Your Monthly bill is=Rs.%0.2f \n",bill);
    printf("Your Complete bill with monthly Rental charge is=Rs.%0.2f",rental);
    return 0;
}

Wednesday 26 December 2012

Merging 2 Sorted Array's

#include<stdio.h>

 int main()
{
    int a[20],b[20],c[40],i,j,k,n1,n2;
    clrscr();
    printf("Merging 2 sorted arrays...\n\n");

    //array 1 input
    printf("Enter how many elements in array1: ");
    scanf("%d",&n1);

    for(i=0;i<n1;i++)
    {
        printf("Enter element %d: ",i+1);
        scanf("%d",&a[i]);
    }

    //array 2 input
    printf("Enter how many elements in array2: ");
    scanf("%d",&n2);

    for(i=0;i<n2;i++)
    {
        printf("Enter element %d: ",i+1);
        scanf("%d",&b[i]);
    }

    //merging
    for(i=0,j=0,k=0;i<n1&&j<n2;k++)
    {
        if(a[i]<b[j])
        {
            c[k]=a[i];
            i++;
        }
        else if(a[i]==b[j])
        {
            c[k]==a[j];
            j++;
        }
        else
        {
            c[k]=b[j];
            j++;
        }
    }

    //remaining elements of array 1
    while(i<n1)
    {
        c[k]=a[i];
        i++;
        k++;
    }

    //remaining elements of array 2
    while(j<n2)
    {
        c[k]=b[j];
        j++;
        k++;
    }

    //output
    printf("\n\nArray after merging elements of both arrays are..\n");
    for(i=0;i<(n1+n2);i++)
        printf("%d\t",c[i]);
return 0;
}

Thursday 20 December 2012

Frequency of letters in a String-NAGARRO's placement question

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

int main()
{
    int i=0;
    char str[20];
    int cap[26]={0};
    int small[26]={0};
    printf("\nEnter your string: ");
    gets(str);
    while(str[i]!='\0')
    {
        if (str[i]>='A'&&str[i]<='Z')
            cap[str[i]-'A']++;
        else if(str[i]>='a'&&str[i]<='z')
            small[str[i]-'a']++;
        i++;
    }
    int max=0;
    int capf = 1;
    int pos=0;
    for(i=0;i<26;i++)
    {
        if(max<cap[i])
        {
            max=cap[i];
            pos=i;
            capf=1;
        }
        if(max<small[i])
        {
            max=small[i];
            pos=i;
            capf=0;
        }
    }
    if (capf)
        printf("\nThe maximum occurance alphabet is %c with %d times",pos+'A',max);
    else
        printf("\nThe maximum occurance alphabet is %c with %d times",pos+'a',max);
    return 0;
}


-Gaurav Singhal

Wednesday 19 December 2012

C program to convert Figures into Words

#include<stdio.h>

int main()
{
    long int n,t;
    char a[][10]={" ","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
    char b[][10]={" "," ","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    char c[][10]={" ","Leven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    printf("Enter a number\n");
    scanf("%ld",&n);
    if(n>=1000&&n<10000)
    {
        t=n/1000;
        printf("%s thousand ",a[t]);
        n=n-(t*1000);
    }
    if(n>=100&&n<1000)
    {
        t=n/100;
        if((t*100)==n)
            printf("%s hundred",a[t]);
        else
            printf("%s hundred and ",a[t]);
        n=n-(t*100);
    }
    if(n>10&&n<100)
    {
        if(n>10&&n<20)
        {
            t=n-10;
            printf("%s ",c[t]);
            n=100000;
        }
        else
        {   t=n/10;
        if((t*10)==n)
            printf("%s ",b[t]);
        else
            printf("%s ",b[t]);
        }   n=n-(t*10);
    }
    if(n>0&&n<=10)
        printf("%s",a[n]);
    return 0;
}



-Gaurav Singhal

Monday 17 December 2012

Sathiya Pattern in C

#include<stdio.h>
int main()
{
    int n=15,i,j,n1;
    n1=n-1;
    printf("");
    printf("\t\t\t\t      *\n");
    printf("\t\t\t\t      *\n");
    for(i=1;i<=n;i++)
    {
        printf("\t\t\t  ");
        if(i==1)
        {
            printf("\b\b**");
            printf("*");
            for(j=2;j<(n1/2);j++)
                printf(" ");
            for(j=n1/2;j<n1;j++)
                printf("*");
            printf("\n");
        }
        if(i>=2 && i<(n/2))
        {
            if(i==n/4+1)
            {
                printf("*  *  *  *");
            }
            else
            {
                printf("*");
                for(j=2;j<(n1/2);j++)
                    printf(" ");
                printf("*");
            }
            printf("\n");
        }
        if(i==n/2)
        {
            for(j=1;j<n1;j++)
                printf("*");
            printf("\n");
        }
        if(i>(n/2) && i<n)
        {
            if(i==n-4)
            {
                printf("   *  *  *  *");
            }
            else
            {
                for(j=1;j<(n1/2);j++)
                    printf(" ");
                printf("*");
                for(j=((n1/2)+1);j<n1-1;j++)
                    printf(" ");
                printf("*");
            }
            printf("\n");
        }
        if(i==n)
        {
            for(j=1;j<=(n1/2);j++)
                printf("*");
            for(j=(n1/2)+1;j<n1-1;j++)
                printf(" ");
            printf("***");
            printf("\n");
        }
    }
    printf("\t\t\t  *\n");
    printf("\t\t\t  *\n");
    printf("\n\n\t\t\t\t\t\tGaurav\n");
    return 0;
}


output:

-Gaurav Singhal

Saturday 15 December 2012

Sum of Prime Numbers

 #include <stdio.h>
 #include <math.h>
 int main(void)
 {
     unsigned int i,j,s=0,is_prime,sq;

     for(i=3;i<=1000;i++)  

//you can make the program for upperbound and lowerbound also.....
     {
         is_prime = 1; // Assuming that current value of i is prime
         sq=sqrt(i);
         // Checking for prime
         for(j=2;j<=sq;j++)
         {
             if(i%j==0) // Not prime, set is_prime to 0 and break
             {
                 is_prime = 0;
                 break;
             }
         }

         if(is_prime) // If the number is prime, sum it up
         {
             s += i;
             // optionally you can print the prime numbers too
             printf("%d ",i);
         }
     }

     printf("\n\nThe sum of the prime numbers = %d",s);
     return 0;
 }

Thursday 13 December 2012

Langrange Interpolation Formula

#include <stdio.h>

#define N 100

int main()
{
    /*
    y=(x-x2)(x-x3)...(x-xn)/(x1-x2)(x1-x3)...(x1-xn)*y[1]
    +(x-x1)(x-x3)...(x-xn)/(x2-x2)(x2-x3)...(x2-xn)*y[2]
    +...+
    (x-x1)(x-x2)...(x-x(n-1))/(xn-x1)(xn-x2)...(xn-x(n-1))*y[n]
    */
    float x[N]={0},y[N]={0};
    int i,j;
    float res,ans=0,givx=0;
    int n=0;
    printf("\nEnter the total number of points for interpolation\n");
    scanf("%d",&n);
    printf("\nEnter the values of x\n");
    for(i=0;i<n;i++)
    scanf("%f",&x[i]);
    printf("\nEnter the values of y\n");
    for(i=0;i<n;i++)
    scanf("%f",&y[i]);
    printf("\nEnter the values of x to find y\n");
    scanf("%f",&givx);
    for(i=0;i<n;i++)
    {
       res=1;
       for(j=0;j<n;j++)
       {
       if(i==j)
        continue;
       res=res*(givx-x[j])/(x[i]-x[j]);
       }
       ans=ans+res*y[i];
    }
    printf("\nanswer = %g\n",ans);
    return 0;
}

Tuesday 11 December 2012

Memory Allocation in C


When an ELF executable is executed, a process is created and its process image is create in the RAM. However, it is here in the process image in RAM, all the variables are assigned memory. Sometimes memory is allocated statically i.e. defined how much memory at the compile time, and at times it has to be allocated dynamically. Where, it is specified at run time the amount of memory needed.

Static Memory Allocation

First of all, please note, static memory allocation is not related to the ‘static’ variables in C programing.
Look at the following statement of a program:

int var = 30;
It is an initialized variable which is of datatype ‘int’ and has been allocated memory of the size of ‘int’ (lets say 4 bytes, which is a standard in Linux). Such kind of memory allocations are called static memory allocations. Note, at compile time we know here that ‘sizeof(int)’ amount of memory will be allocated. Here are some more examples of static memory allocation

char c;
float fvar;
int array[12];
This also explains why arrays take only constants as their sizes.

Worth mentioning, all such static memory allocations take place in the stack of the process image with certain exceptions like uninitialised global variables, const string pointers etc.

Dynamic Memory Allocations

In several cases, we might not know how much memory do we need at the compile-time. Its can only be specified at the run-time based on the value of a certain variable. For example, in the case of linked lists, where a node can be added/deleted at runtime, or matrices of varying rows/columns. For such scenarios, we have dynamic memory allocations. C provides following methods to allocate memory. Dynamic memory allocation always allocates memory in the heap of the process image.

       void *malloc(size_t size);
       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);
1. ‘malloc()’ is used for allocating a specific amount (in bytes) of memory. It returns a pointer to the memory allocated. However, the returned pointer is a void pointer which needs to be type-casted for regular use. It does not initialise the allocated memory. It returns a NULL pointer in case the allocations fails due to any reason. So, one can check for success/failure of this method by checking if the returned pointer is NULL or not. For more such details regarding malloc method, please refer its man page.
ex:
int *ptr = NULL;
int amt = 30;
ptr = (int*) malloc ( amt * sizeof(int));
2. ‘calloc()’ is used for allocating a specific amount of a particle datatype of a memory. Hence, it takes two arguments as one can see in the syntax provided above. ‘calloc()’ also returns the pointer to the allocated chunk of memory, and it also needs the type casting. However, it attempts to initialise all the bits of the allocated memory to zero. It returns a NULL pointer in case the allocations fails due to any reason. For more details on this method, please refer to its man page.
ex:
int *ptr = NULL;
ptr = (int*) malloc ( amt,  sizeof(int));
3. Another method for dynamic memory allocation is ‘realloc()’. The above two methods are there for fresh memory allocations. However, what if one just wants to expand/shrink an already allocated chunk of memory? For such cases we have ‘realloc()’. Depending upon the memory layout, it either just allocates the extra memory demanded or re-allocated the whole requested memory. It behaves similar to ‘malloc()’ as far as memory allocations are concerned.
ex:
int *newPtr = NULL;
newPtr = (int*) realloc (ptr, amt + extra);
Note, ‘realloc()’ can also do fresh allocation of memory if the input pointer is a NULL.

Freeing up

Power comes with responsibility. Since C gives you the power to use and allocated memory as per our needs, hence it is the onus of the programmer to take care of allocated and free memory. Hence, freeing up of allocated memory is very essential in C programming and avoids many vulnerabilities.

All the statically allocated memory is free-ed automatically as its scope ends. However, the dynamically allocated memory have to be free-ed by the programmer. Here is the method which is used to free memory allocated on heap.
ex:
void free(void *ptr);

A complete example implementation


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

int main()
{
    int size = 15; //static memory allocation
    int i; //static memory allocation
    int *mptr = NULL; 
    int *cptr = NULL;

    //Using malloc
    mptr = (int*) malloc (size *sizeof(int)); //dynamic memory allocation
    if (mptr == NULL)
    {
         printf("Error in  memory allocation\n");
    }
    else
    {
        printf("Use new chunk of memory\n");
        for (i = 0; i < size; i++)
        {
            mptr[i] = i;
        }
    }

    //Using calloc
    cptr = (int*) calloc (size, sizeof(int)); //dynamic memory allocation
    if (cptr == NULL)
    {
         printf("Error in  memory allocation\n");
    }
    else
    {
        printf("Use new chunk of memory\n");
        for (i = 0; i < size; i++)
        {
            cptr[i] = i;
        }
    }

    //Using realloc
    size += size; //Increase size
    mptr = (int*) realloc (mptr, size * sizeof(int)); //extend the dynamic memory allocation
    if (mptr == NULL)
    {
         printf("Error in  memory allocation\n");
    }
    else
    {
        printf("Use new chunk of memory\n");
        for (i = 0; i < size; i++)
        {
            mptr[i] = i;
        }
    }

    //Free-ing up memory
    if (cptr)
        free(cptr);
    cptr = NULL;
    if (mptr)
        free(mptr);
    mptr = NULL;
    // No need to free size and i
    
    return 0;
}


- Gaurav Singhal

Monday 10 December 2012

Final Class in C++

Concept of Final Class in C++

•Class which Can't be inherited by other class, that class is called final class.
•Types of Final Class

 1. Final class: Object of it will be on heap.

class final2 { public:     static final2* Create()     {         return (new final2()) ;     } private:     ~final2()     {        ;     } }; int main() {     final2 *f ;     f = final2::Create() ; // Object only on Heap }
No one can Inherit this final2 class. Suppose if one is going to inherit it then
class child : public final2 { public:     child(){ ;} } ;
Now Error will come.Child class can't inherit final class.. Because destructor of final class is private.


2. Final class: Object of it will be on Stack
class temp { private:     ~temp() {; }     friend class Final; // Due to friend , Final class can use private member functions of temp class }; class Final: virtual public temp { // Define all data members and functions as you want public:     Final()     {     ;     }     ~Final(); };
Now this final class can't be inherited by other class.Suppose one is going to inherit it to his class then,
Example:
Code: CPP
class Child : public Final { public:     Child(){ ;}     ~Child() { ;} };
Due to temp class is inherited virtually to Final class. So when child class's constructor called, then first temp class constructor will be called. By Child class's constructor, we are calling temp class constructor. Child class can't call temp class's private destructor because child is not a friend of temp class. This is the rule of C++.
Due to this rule in C++, Compiler sees that it is violating the rules, then it flashes error.




-Gaurav Singhal

Friday 7 December 2012

Priority Queue program in C

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

void insert();
void show();
void del();

typedef struct node
{
    char info;
    int prior;
    struct node *next;
}n;
n *front=NULL,*rear=NULL;
int main()
{
    int ch;
    while(1)
    {
    printf("\n1. Insert Element");
    printf("\n2. Delete Element");
        printf("\n3. Show");
        printf("\n4. Exit");
        printf("\nChoice -: ");
    fflush(stdin);
    ch=getche();
    switch(ch)
    {
        case '1':
        insert();
        break;
        case '2':
        del();
        break;
        case '3':
        show();
        break;
        case '4':
        exit(0);
        break;
        default:
        printf("Wrong Input");
        getch();
        }
    }
}
void insert()
{
    n *ptr,*temp,*par;
    temp=(n*)malloc(sizeof(n));
    if(temp==NULL)
    {
        printf("Memory not allocated..");
        getch();
        exit(1);
    }
    printf("\nEnter data: ");
    fflush(stdin);
    scanf("%c",&temp->info);
    while(1)
    {
    printf("\nEnter Priority for '%c' data: ",temp->info);
    scanf("%d",&temp->prior);
    if(temp->prior > 0)
        break;
    }
    temp->next=NULL;

    if(front==NULL && rear==NULL)
    {
        front=rear=temp;
    }
    else
    {
        par=ptr=front;
        while((ptr->prior) <= (temp->prior) && ptr!=NULL)
        {
            par=ptr;
            ptr=ptr->next;
        }
        //node entering at rear
        if(ptr==NULL)
        {
            par->next=temp;
            temp->next=NULL;
            rear=temp;
        }
        //node entering before front
        else if(par==ptr)
        {
            front=temp;
            temp->next=ptr;
        }
        //node entering in middle
        else
        {
            par->next=temp;
            temp->next=ptr;
        }
    }
}
void show()
{
    n *ptr;
    printf("\nQueue: ");
    ptr=front;
    while(ptr!=NULL)
    {
        printf("%c\t",ptr->info);
        ptr=ptr->next;
    }
    getch();
}
void del()
{
    if(front==NULL)
    {
        printf("Priority Queue is Empty");
        getch();
        return;
    }
    printf("Element Deleted is '%c'",front->info);
    front=front->next;
}



-Gaurav Singhal
Like this page and comment about the program....
Thank You...

Thursday 6 December 2012

Vertical Histogram in C

#include<stdio.h>
#include<conio.h>
int main()
{
    int a[10],i,j,n,max,copy_max;;

    printf("Enter the no. of elements: ");
    scanf("%d",&n);

    //input array
    for(i=0;i<n;i++)
    {
        printf("Enter the element %d: ",i+1);
        scanf("%d",&a[i]);
    }

    //finding max no.
    max=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
            max=a[i];
    }
    copy_max=max;

    //logic for vertical histogram
    for(i=0;i<copy_max;i++,max--)
    {
        for(j=0;j<n;j++)
        {
            if(a[j]>=max)
                printf(" * ");
            else
                printf("   ");
        }
        printf("\n");
    }
    return 0;
}


output..
Enter no of Elements: 3
Enter element 1: 4
Enter element 1: 5
Enter element 1: 3

    *
*  *
*  *  *
*  *  *
*  *  *
 

Sunday 2 December 2012

Insertion Sort In Detail

This technique actually goes through N-1 passes through the list where N is number of elements in the array. Each pass P can be considered to be for the elements from 1 to N-1. Each pass is actually an effort to place the element P in its correct position among the elements from position 0 to P.

Example:
Let us take an array of numbers A= (34, 8 , 64, 51, 32, 21).



Code:
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  |  Original     |   34  |   8   |   64  |   51  |   32  |  21  | Position Moved  |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  | After P1      |    8  |   34  |   64  |   51  |   32  |  21  |           1     |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  | After p2      |    8  |   34  |   64  |   51  |   32  |  21  |           0     |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  | After p3      |    8  |   34  |   51  |   64  |   32  |  21  |           1     |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  | After p4      |    8  |   32  |   34  |   51  |   64  |  21  |           3     |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
  | After p5      |    8  |   21  |   32  |   34  |   51  |  64  |           4     |
  +---------------+-------+-------+-------+-------+-------+------+-----------------+
Pseudo code: or Algorithm
Code:
  insertionSort(array A)
  begin
      for i := 1 to length[A]-1 do
      begin
          value := A[i];
          j := i - 1;
          done := false;
          repeat
              if A[j] > value then
              begin
                  A[j + 1] := A[j];
                  j := j - 1;
                  if j < 0 then
                      done := true;
              end
              else
                  done := true;
          until done;
          A[j + 1] := value;
      end;
  end;
Code in C language:
Code:
  void InsertionSort (int A[], int N)
  {
              int j, P;
              int Tmp;
   
              for (P=1; P<N; P++)
              {
                          Tmp=A[P];
                          for (j=P; j>0 && A[j-1]>Tmp; j--)
                          A[j] =A[j-1];
                          A[j]=Tmp;
              }
  }
Runtime Complexity:

It is O (N2). This is because there are two for loops and in the worst case the two for loops are both going to run N times. The point where Insertion Sort scores over Bubble Sort is its best case analysis. Note that when the elements are fully sorted, then the inner for loop will immediately break. This will give a runtime of O(N).




- Gaurav Singhal

Friday 30 November 2012

Program WITHOUT "main()" Function

#include<stdio.h>
#include<conio.h>
#define gaurav main

int gaurav()
{
    printf("I am in Main");
    getch();
}

Saturday 24 November 2012

DeBUG this C-Bug

A very small challenging question in C....

if(condition)
    printf("Hello");
else
    printf("Bye");

Q: You have to Enter a Condition in if statement so that the program outputs "HelloBye" without quotes..

Answer the question by making it a comment at below the program with your correct name...


thank you
-Gaurav Singhal..

Thursday 15 November 2012

3-D CAR IN C




#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<process.h>

void main()
{
   int gd=DETECT,gm;
   initgraph(&gd,&gm,"c:\\tc\\bgi");
int c=12;
setbkcolor(0);
//setlinestyle(0,1,2);
int t;
while(1)
{
settextstyle(2,0,5);
outtextxy(100,10,"Press L,H ,T,P");
outtextxy(100,30,"Press 1 for Quit");
as:
setcolor(13);
ellipse(380,127,20,152,130,35);
//////////////////////////////rear//////////////////////////

line(490,109,560,142);
line(560,142,569,142);
line(569,142,582,102);
line(582,102,620,92);
line(593,132,617,125);

line(617,124,627,96);
line(620,92,628,97);

line(472,86,602,96);
line(501,113,575,121);
line(443,77,475,80);

line(443,77,432,93);
line(475,80,472,85);
//setcolor(4);
   line(593,132,593,137);
   line(593,137,600,141);
   line(600,141,600,185);
   line(600,185,608,192);
   line(608,192,608,234);
   line(608,234,586,253);
    line(586,253,577,248);

/////////////////////////           mirror
   line(263,112,363,127);
      line(193,160,263,112);
   line(193,160,220,170);
      line(220,170,280,180);
      line(280,180,320,185);
      line(320,185,363,127);
////////////////////////////////sidemirror
line(340,194,460,169);
 line(460,169,519,152);

ellipse(512,144,300,30,10,10);
ellipse(467,143,28,100,50,30);
line(510,128,521,138);
line(435,116,440,171);

//   setcolor(4);          
////////////////////////////////////////cont//
   line(339,194,372,144);
//    line(372,140,386,128);
ellipse(454,208,87,123,128,95);
line(372,144,384,128);
   int b,x,y;
////////////////////////lower
line(365,298,524,264);
line(365,298,330,310);
line(330,310,323,310);


///////////////////////////////bumper
ellipse(162,221,135,190,90,40);
line(96,193,140,174);
line(140,174,160,168);
line(160,168,192,161);

//////////////////////front
ellipse(75,246,95,190,18,18);
line(57,251,57,286);
//setcolor(4);
ellipse(181,178,232,263,200,137);
ellipse(195,180,256,286,200,137);


ellipse(191,171,228,247,200,100);
ellipse(231,198,234,275,200,80);

//setcolor(9);
//ellipse(195,170,256,286,200,137);
//setcolor(12);

ellipse(196,167,228,246,200,90);
ellipse(231,184,234,276,200,80);


ellipse(191,200,228,246,200,90);
ellipse(228,218,234,276,200,80);

ellipse(258,268,180,220,200,40);
ellipse(178,296,244,355,16,10);

ellipse(238,249,227,250,200,60);


/////////////wheel1
ellipse(302,281,320,77,26,45);
ellipse(290,277,65,162,40,45);
ellipse(278,288,144,212,31,45);

/////////////wheel2
//setcolor(5);
ellipse(302+260,229,328,87,26,45);
ellipse(290+280-7,277-50+2,90,162,40,45);
ellipse(278+270,288-50,144,215,27,45);
 b=0;
int v=0;

/////////
ellipse(302+250+v,227+b,295,90,29,41);
ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+248+v,229+b,0,360,21,30);

ellipse(302+247+v,229+b,0,360,8,10);
setfillstyle(6,11);
//floodfill(302+248+v,230+b,13);
//line(546,201,546,257);
//line(554,201,554,257);
//setcolor(4);

line(546+v,201+b,546+v,220+b);
line(551+v,201+b-2,551+v,220+b);

line(546+v,238+b,546+v,257+b);
line(551+v,238+b+2,551+v,257+b+2);


line(530+v,225+b,541+v,225+b);
line(530+v,230+b,541+v,230);

line(557+v,225+b,570+v,225+b);
line(557+v,230+b,570+v,230+b);



line(563+v,206+b,552+v,222+b);
line(534+v,246+b,543+v,232+b);

line(566+v,210+b,556+v,223+b);
line(536+v,250+b,544+v,238+b);

line(536+v,207+b,546+v,222+b);
line(532+v,213+b,542+v,224+b);

line(556+v,235+b,566+v,247+b);
line(551+v,237+b,563+v,253+b);



////////////////////////////////////////////////////
v=-260;
b=56;
ellipse(302+233+v,221+b,260,60,49,51);
//ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+243+v,224+b,0,360,28,35);
//  line(249,328,269,328);
ellipse(300+245+v,223+b,0,360,10,12);

ellipse(285+249+v,239+b,210,260,30,33);
//floodfill(285+258+v,230+b,12);
b=45;
v=v-4;
line(546+v,201+b,546+v,220+b+2);
line(551+v,201+b,551+v,220+b+2);
b=b+8;
line(546+v,238+b,546+v,257+b+4);
line(551+v,238+b,551+v,257+b+4);
v=v-2;
line(530+v-6,225+b,541+v,225+b);
line(530+v-6,230+b,541+v,230+b);
v=v+5;
line(557+v,225+b,570+v+3,225+b);
line(557+v-1,230+b,570+v+3,230+b);


b=b-5;
v=v-5;
line(565+v+3,206+b,552+v+4,222+b-2);
b=b+15;

line(534+v,246+b,543+v+3,232+b-5);
b=b-10;
line(566+v+7,210+b-5,556+v+4,220+b);
line(536+v-5,250+b,544+v-2,238+b-4);


line(536+v,207+b-8,545+v,222+b-5);
line(531+v,212+b-8,542+v,224+b-2);

line(556+v,235+b,566+v+3,247+b+5);
line(551+v,237+b,563+v+2,253+b+3);

///////////////////lights
ellipse(199,250,144,345,18,8);
line(185,245,206,230);
//setcolor(4);
ellipse(223,234,340,110,8,5);
line(230,237,217,252);
line(206,230,220,229);
//setfillstyle(1,4);

//floodfill(200,240,12);

/////////////////////////////////////
line(90,223,152,236);
line(152,236,137,254);
line(90,223,90,242);

//setfillstyle(10,9);
//floodfill(91,230,14);
ellipse(240,270,104,136,100,60);
ellipse(185,237,120,160,100,60);
ellipse(80,221,357,134,10,10);

line(152,236,168,228);
///////////////////////////////////////////////
line(435,116,440,171);
//////////////////////////////////////////hp
//line(134,185,220,210);
line(134,185,196,160);
line(214,212,318,185);
/////////////////////////////////////////////////light

//setcolor(14);
ellipse(166,247,99,330,8,8);
ellipse(171,243,310,129,7,7);
putpixel(174,250,13);
putpixel(173,251,13);
putpixel(164,239,13);
putpixel(165,238,13);

/////////////////////////////////////////road/////////////////////
setcolor(13);
line(1,430,639,300);
line(1,445,639,315);

line(1,210,93,194);
line(1,195,194,158);

//line(1,170,639,71);
//line(1,170,229,135);
line(520,90,639,71);
line(478,86,639,56);

   int c=0;

   line(10,194+c,10,208+c);
line(40,189+c,40,204+c);
line(70,183+c,70,198+c);
line(100,176+c,100,190+c);
line(130,170+c,130,177+c);
line(160,166+c,160,168+c);
line(190,160+c,190,161+c);

 line(190+330,78+c,190+330,89+c);

line(190+360,72+c,190+360,85+c);
line(190+390,67+c,190+390,81+c);
line(190+420,62+c,190+420,76+c);
line(190+449,57+c,190+449,71+c);



   c=236;

line(10,192+c,10,208+c);
line(40,189+c-2,40,204+c-3);
line(70,183+c-3,70,198+c-3);
line(100,176+c-2,100,190+c-2);
line(130,170+c-2,130,177+c+5);
line(160,166+c-3,160,168+c+8);
line(190,160+c-4,190,161+c+9);

line(190+30,156+c-5,190+30,170+c-5);


line(190+30+30,156+c-12,190+30+30,170+c-12);

line(190+90,156+c-18,190+90,170+c-17);

line(190+120,156+c-25,190+120,170+c-25);

line(190+150,156+c-30,190+150,170+c-30);

line(190+180,156+c-37,190+180,170+c-36);


line(190+210,156+c-42,190+210,170+c-42);


line(190+240,156+c-48,190+240,170+c-48);


line(190+270,156+c-55,190+270,170+c-54);


line(190+300,156+c-61,190+300,170+c-61);



 line(190+330,78+c+10,190+330,89+c+13);

line(190+360,72+c+11,190+360,85+c+13);
line(190+390,67+c+10,190+390,81+c+10);
line(190+420,62+c+8,190+420,76+c+10);
line(190+449,57+c+8,190+449,71+c+8);




/////////////////road

setcolor(12);          /////////////////////////////1

line(1,310,25,306);
line(6,318,30,315);
line(1,310,6,318);
line(25,306,30,314);
int k,m;
k=13*45+19;
m=16*(-8);
   //2
setcolor(12);

line(605,310-128,629,306-128);
line(610,318-128,634,315-128);
line(605,310-128,610,318-128);
line(629,306-128,634,314-128);

setcolor(12);    //////////////////////////////////3
k=45;
m=-8;
line(46,302,70,298);
line(51,310,75,307);
line(46,302,51,310);
line(70,298,75,306);


setfillstyle(1,0);
floodfill(64,303,12);

setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(1,14);
floodfill(64,303,12);

t=getch();
if(t=='1')
exit(0);
if(t=='h')
{
sound(710);
delay(500);
nosound();
//break;
}
if(t=='t')
{
while(!kbhit())  {
setfillstyle(1,0);
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);

setfillstyle(1,random(12));

floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);

 floodfill(170,250,13);
 floodfill(80,230,13);


 }

setfillstyle(1,0);

floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);

 floodfill(170,250,13);
 floodfill(80,230,13);

}


if(t=='l')
{
while(!kbhit())
{

delay(120);
setfillstyle(6,0);         //////////////////////////ty
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);

setfillstyle(1,0);
floodfill(64,303,12);

///////////////////////////////////road

setfillstyle(9,0);       /////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);


//////////////////////
floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);




setfillstyle(6,14); //////////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
////////////////////////////////////////road

setfillstyle(9,10);///////////////////////////////////color
floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
//////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);

setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);

///////////////////////////////////////
setfillstyle(10,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);
///light
setfillstyle(6,11);                             ///////////

floodfill(200,250,13);

floodfill(170,250,13);
 floodfill(80,230,13);

delay(120);

setfillstyle(6,0);/////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
/////////////////////////////////////road
setfillstyle(9,0);      ///////////////color

floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
///////////////////////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);
/////////////////////////////
setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);

setfillstyle(6,10);            /////////////ty

floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
////////////////////////////////////////////////road
setfillstyle(9,14);/////////////////////////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);
/////////////////////////////////////////

floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);
setfillstyle(1,14);
floodfill(64,303,12);

setfillstyle(9,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);

delay(20);
setfillstyle(1,14);

floodfill(200,250,13);

floodfill(170,250,13);
 floodfill(80,230,13);

 delay(20);
setfillstyle(1,0);

floodfill(200,250,13);

floodfill(170,250,13);
 floodfill(80,230,13);




}          }




if(t=='p')
{
int n=0;
while(!kbhit())
{
if(n<=60)
n++;
setcolor(0);
rectangle(1+1,-10,90-1,-12+n);
delay(14);

setcolor(9);
rectangle(1,-10,90,-10+n);
if(n==60)
{

outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");
 delay(400);
 }


}
setcolor(0);
rectangle(1,-10,90,-10+n);
rectangle(1,-10,90,-11+n);
outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");

}

}



circle(300,100,3);

nosound();

getch();
}

                                                                                                                 -Gaurav Singhal

Friday 2 November 2012

Quick Sort program in C


#include<stdio.h>
#define MAX 50

enum bool{FALSE,TRUE};
int main()
{
    int a[MAX],n,i;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter element %d : ",i+1);
        scanf("%d",&a[i]);
    }
    printf("Unsorted list is :\n");
    show(a,0,n-1);
    printf("\n");
    quick(a,0,n-1);
    printf("Sorted list is :\n");
    show(a,0,n-1);
    return 0;
}
quick(int a[],int low,int up)
{
        int loc,temp,left,right;
        enum bool locpos=FALSE;
        left=low;
        right=up;
        loc=low;
        if(low>=up)
            return;
        printf("Sublist : ");
        show(a,low,up);
        while(locpos==FALSE)
        {
            while( a[loc]<=a[right] && loc!=right )
                right=right-1;
            if( loc==right )
                locpos=TRUE;
            if( a[loc] > a[right] )
            {
                a[loc]=a[loc]+a[right];
                a[right]=a[loc]-a[right];
                a[loc]=a[loc]-a[right];
                loc=right;
            }
            while( a[loc]>=a[left] && left != loc )
                left=left+1;
            if(loc==left)
                locpos=TRUE;
            if( a[loc] < a[left] )
            {
                a[loc]=a[loc]+a[left];
                a[left]=a[loc]-a[left];
                a[loc]=a[loc]-a[left];
                loc=left;
            }
        }
        printf("-> item Placed is %d ->", a[loc]);
        show(a,low,up);
        printf("\n");
        quick(a,low,loc-1);
        quick(a,loc+1,up);
}
show(int a[],int low,int up)
{
    int i;
    for(i=low;i<=up;i++)
        printf("%d\t",a[i]);
}

Tuesday 30 October 2012

BINARY SEARCH TREE in C....

All Operations in BST......

   
 #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct btree
{
 struct btree *left ;
 int data ;
 struct btree *right ;
}tree;
void insert ( tree **, int ) ;
void del ( tree **, int ) ;
void search ( tree **,int,tree **, tree **, int *);
void inorder ( tree * );
int main( )
{
 tree *bt ;
 int i,num;
 bt = NULL ;  /* empty tree */
    while(1)
    {
 printf("1. Insert");
 printf("\n2. Delete");
 printf("\n3. Show");
 printf("\n4. Exit");
 printf("\nEnter your choice: ");
 scanf("%d",&i);
 switch(i)
 {
     case 1:
  printf("Enter no to insert: ");
  scanf("%d",&num);
  insert(&bt,num);
  break;
     case 2:
  printf("Enter no to insert: ");
  scanf("%d",&num);
  del(&bt,num);
  break;
     case 3:
  inorder(bt);
  getch();
  break;
     case 4:
  exit(0);
  break;
 }
    }
    return 0;
}
void insert ( tree **sr, int num )
{
 if ( *sr == NULL )
 {
  (*sr)= (tree *)malloc (sizeof(tree)) ;
  ( *sr ) -> left = NULL ;
  ( *sr ) -> data = num ;
  ( *sr ) -> right = NULL ;
 }
 else  /* search the node to which new node will be attached */
 {
  /* if new data is less, traverse to left */
  if ( num < ( *sr ) -> data )
   insert ( &( ( *sr ) -> left ), num ) ;
  else
   /* else traverse to right */
   insert ( &( ( *sr ) -> right ), num ) ;
 }
}
void del ( tree **root, int num )
{
 int found ;
 tree *parent, *x, *xsucc ;
 if ( *root == NULL )
 {
  printf ( "\nTree is empty" ) ;
  return ;
 }
 parent = x = NULL ;
 /* call to search function to find the node to be deleted */
 search ( root, num, &parent, &x, &found ) ;
 if ( found == FALSE )
 {
  printf ( "\nData to be deleted, not found" ) ;
  return ;
 }
 /* if the node to be deleted has two children */
 if ( x -> left != NULL && x -> right != NULL )
 {
  parent = x ;
  xsucc = x -> right ;
  while ( xsucc -> left != NULL )
  {
   parent = xsucc ;
   xsucc = xsucc -> left ;
  }
  x -> data = xsucc -> data ;
  x = xsucc ;
 }
 /* if the node to be deleted has no child */
 if ( x -> left == NULL && x -> right == NULL )
 {
  if ( parent -> right == x )
   parent -> right = NULL ;
  else
   parent -> left = NULL ;

  free ( x ) ;
  return ;
 }
 /* if the node to be deleted has only right */
 if ( x -> left == NULL && x -> right != NULL )
 {
  if ( parent -> left == x )
   parent -> left = x -> right ;
  else
   parent -> right = x -> right ;

  free ( x ) ;
  return ;
 }
 /* if the node to be deleted has only left child */
 if ( x -> left != NULL && x -> right == NULL )
 {
  if ( parent -> left == x )
   parent -> left = x -> left ;
  else
   parent -> right = x -> left ;
  free ( x ) ;
  return ;
 }
}
void search ( tree **root, int num, tree **par, tree **x, int *found )
{
 tree *q ;
 q = *root ;
 *found = FALSE ;
 *par = NULL ;
 while ( q != NULL )
 {
  if ( q -> data == num )
  {
   *found = TRUE ;
   *x = q ;
   return ;
  }
  *par = q ;
  if ( q -> data > num )
   q = q -> left ;
  else
   q = q -> right ;
 }
}
void inorder ( tree *sr )
{
 if ( sr != NULL )
 {
  inorder ( sr -> left ) ;
  printf ( "%d\t", sr -> data ) ;
  inorder ( sr -> right ) ;
 }
}