Friday, March 25, 2011

Tower of Hanoi

Tower  :                     x                  z                  y
Solution of Above Tower of Hanoi Problem will be soon..... using c++

#include<conio.h>
#include<iostream.h>
class tower
{
    //Private variables
        int NoDisk;
        char FromTower,ToTower,AuxTower;
    public:
        void hanoi(int,char,char,char);
};
void tower::hanoi(int NoDisk,char FromTower,char ToTower, char AuxTower)
{
     //if only one disk, make the move and return
        if (NoDisk == 1)
        {
            cout<<"\nMove from disk 1 from tower "<<FromTower<<" to tower "<<ToTower;
            return;
        }
            //Move top n1 disks from X to Y, using Z as aux. tower
       
        hanoi(NoDisk - 1,FromTower,AuxTower,ToTower);
       
  //Move remaining disk from X to Z      
        cout<<"\nMove from disk "<<NoDisk<<" from tower "<<FromTower<<" to tower "<<ToTower;
       
  //Move n1 disk from Y to Z using X as aux. tower
        hanoi(NoDisk - 1,AuxTower,ToTower,FromTower);
        return;
       
}

void main()
{
    int No;
    tower Ob;
    clrscr();
    cout<<"\n\t\t\t--- Tower of Hanoi ---\n";

  //Imput the number of disk in the tower
    cout<<"\n\nEnter the number of disks = ";
    cin>>No;

  //We assume that the towers are X, Y and Z
    Ob.hanoi(No,'X','Z','Y');

    getch();
   
}
Output :

Another Solution without recursion Technique :






   Move    Binary Number Interpretation
100001Move disk 1 to empty peg.
200010Move disk 2 to empty peg.
300011Move disk 1 to cover disk 2.
400100Move disk 3 to empty peg.
500101   Move disk 1 NOT to cover disk 3. 
600110Move disk 2 to cover disk 3.
700111Move disk 1 to cover disk 2.
801000Move disk 4 to empty peg.
901001Move disk 1 to cover disk 4.
1001010Move disk 2 NOT to cover disk 4.
1101011Move disk 1 to cover disk 2.
1201100Move disk 3 to cover disk 4.
1301101Move disk 1 NOT to cover disk 3.
1401110Move disk 2 to cover disk 3.
1501111Move disk 1 to cover disk 2.
1610000Move disk 5 to empty peg.
1710001Move disk 1 NOT to cover disk 5.
1810010Move disk 2 to cover disk 5.
1910011Move disk 1 to cover disk 2.
2010100Move disk 3 NOT to cover disk 5.
2110101Move disk 1 NOT to cover disk 3.
2210110Move disk 2 to cover disk 3.
2310111Move disk 1 to cover disk 2.
2411000Move disk 4 to cover disk 5.
2511001Move disk 1 to cover disk 4.
2611010Move disk 2 NOT to cover disk 4.
2711011Move disk 1 to cover disk 2.
2811100Move disk 3 to cover disk 4.
2911101Move disk 1 NOT to cover disk 3.
3011110Move disk 2 to cover disk 3.
3111111Move disk 1 to cover disk 2.

 
Think Above Solution for Five Ring Tower. and Consider Cases As Below Detail.
Complexity Consider on (2^5 -1)=31




   Move    Binary Number Interpretation
801000Move disk 4 to empty peg.
1301011Move disk 1 to cover disk 2.
2511001Move disk 1 to cover disk 4.
1810010Move disk 2 to cover disk 5.
1011010   Move disk 2 NOT to cover disk 4. 
1710001Move disk 1 NOT to cover disk 5.

Monday, March 14, 2011

check whether given no is odd or even without using any type of arithmetic operator


#include<stdio.h>
void main()
{
    int i,n;
    
    printf("enter the number ");
    scanf("%d",&n);
    i=n&1;
    if(i==0)                                
    {
        printf("Given number is even");
    } 
    else
    {
        printf("Given number is odd ");
    }

}

Thursday, March 10, 2011

Cyclic Nature of char data type


What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char c=256;
    char *ptr="Leon";
    if(c==0)                                
         while(!c)
             if(*ptr++)
                 printf("%+u",c);
             else
                 break;
}


Explanation:

In the above program c is signed (default) char variable. Range of signed char variable in Turbo c is from -128 to 127. But we are assigning 256 which is beyond the range of variable c. Hence variable c will store corresponding cyclic value according to following diagram:



Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.

if(c==0)
It is true since value of c is zero.
Negation operator i.e. ! is always return either zero or one according to following rule:
!0 = 1
!(Non-zero number) = 0
 So,
!c = !0 =1
As we know in c zero represents false and any non-zero number represents true. So
while(!c) i.e. while(1) is always true.
In the above program prt is character pointer. It is pointing to first character of string “Leon” according to following diagram:













In the above figure value in circle represents ASCII value of corresponding character.
Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76
if(*ptr++) is equivalent to : if(‘L’) is equivalent to: if(76) . It is true so in first iteration it will print +0. Due to ++ operation in second iteration ptr will point to character constant ‘e’ and so on. When ptr will point ‘\0’ i.e. null character .It will return its ASCII value i.e. 0. So if(0) is false. Hence else part will execute. 

Wednesday, March 9, 2011

C Pointer to array of string Actual Representation and allocation



void main(){
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
}
Output: ava
Explanation:
In this example
ptr: It is pointer to array of string of size 4.
array[4]: It is an array and its content are string.
Pictorial representation:


Note: In the above figure upper part of box represent content and 
lower part represent memory address. We have assumed arbitrary address.
++(*ptr)[2]
=++(*&array)[2] //ptr=&array
=++array[2]
=++”java”
=”ava” //Since ptr is character pointer so it 
// will increment only one byte
Note: %s is used to print stream of characters 
up to null (\0) character.

Tuesday, March 8, 2011

Create DOS - DIR Command using c Language




Step 1: Write following code.
#include <stdio.h>
#include <dos.h>
void main(int count,char *argv[])
{
  struct find_t q ;
  int a;
  if(count==1)
  argv[1]="*.*";
  a = _dos_findfirst(argv[1],1,&q);
  if(a==0)
  {
     while (!a)
   {
    printf("  %s\n", q.name);
    a = _dos_findnext(&q);
    }
  }
  else
  {
  printf("File not found");
  }
}
Step 2: Save the as paras.c (You can give any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of 
Window XP operating system and select properties.
Step 5: Select Advanced -> Environment Variables
Step 6: You will find following window:
Click on new button (Button inside the red box)







Step 7: Write following:
Variable name: path
Variable value: c:\tc\bin\paras.c  
(the path where you have saved)
Step 8: Open command prompt and write paras and 
press enter button.

Friday, March 4, 2011

Difference Between uninitialized pointer and null pointer .



An uninitialized pointer is a pointer which points unknown memory location while null pointer is pointer which points a null value or base address of segment. For example: 
int *p;   //Uninitialized pointer
int *q= (int *)0;  //Null pointer
#include<stdio.h>
int *r=NULL;   //Null pointer
What will be output of following c program?

#include<string.h>
#include<stdio.h>
void main(){
    char *p;  //Uninitialized pointer
    char *q=NULL;   //Null pointer;
    strcpy(p,"cquestionbank");
    strcpy(q,"cquestionbank");
    clrscr();
    printf("%s  %s",p,q);
    getch();
}

Output: cquestionbank  (null)

Thursday, March 3, 2011

Let Know actual Memory address allocation of variable and initialization in c

Suppose

 int a = 7; Than what ?



Memory representation of: 

signed int a=7;            (In Turbo c compiler) 
signed short int a=7   (Both turbo c and Linux gcc compiler) 
Binary equivalent of data 7 in 16 bit:  00000000 00000111 
Data bit: 0000000 00000111 (Take first 15 bit form right side) 
Sign bit: 0                 (Take leftmost one bit) 

First eight bit of data bit from right side i.e. 00000111 will store in the leftmost byte from right to left side and rest seven bit of data bit i.e. 0000000 will store in rightmost byte from right to left side as shown in the following figure:   

Explain wild pointer in c .


A pointer in c which has not been initialized is known as wild pointer. 

Example: 

What will be output of following c program? 

void main(){ 
int *ptr; 
printf("%u\n",ptr); 
printf("%d",*ptr); 

} 

Output: Any address
Garbage value 


Here ptr is wild pointer because it has not been initialized. 
There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segmentwhile wild pointer doesn’t point any specific memory location.