Monday 17 December 2012

C CODE TO GENERATE TO VARIABLE SIZE SEVEN SEGMENT DISPLAY

Now guyz ...
Here's a small crap of how to display a 7 seg. o/p by entering any number ....

Well .. Its a bit crappy to write the i/p and o/p here ..
But just try the code :


#define P printf
#define S scanf
#include<stdio.h>
#include<conio.h>
main()
{
 int n[4],m,size=0,i=0,j=0,cnt=1;
 int temp=0,row=0,col=0,p=0;
while(1)
 {
 P("Enter the size preffered :\n");
 S("%d",&size);
 row=2*size+3;
 col=size+2;
 char matrix[4][row][col];
 temp=(row/2)-1;           

 for(i=0;i<row;i++)
 {
  for(j=0;j<col;j++)
  {
   for(p=0;p<=3;p++)
   {
     matrix[p][i][j]='\0';                 
   }                 
  }
}

 i=0;j=0;
 P("Enter the number.\n");
 fflush;
S("%d %d %d %d",&n[0],&n[1],&n[2],&n[3]);
i=0;j=0;

for(p=0;p<=3;p++)
{
                 
switch(n[p]) //Switch 1
 {
   case 2:
   case 3:
   case 5:
   case 6:
   case 7:
   case 8:
   case 9:
   case 0:
        {
          for(j=1;j<=col-2;j++)
          matrix[p][0][j]='_';              
        
        }break;       
 }//Switch_1 ends

  switch(n[p]) //Switch 2
 {
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 8:
   case 9:
        {
          
          for(j=1;j<=col-2;j++)
          matrix[p][row/2][j]='_';              
        }break;       
}//Switch_2 ends


  switch(n[p]) //Switch 3
 {
   case 0:
   case 2:
   case 3:
   case 5:
   case 6:
   case 8:
        {
          for(j=1;j<=col-2;j++)
          matrix[p][row-1][j]='_';              
        }break;       
 }//Switch_3 ends
  

  switch(n[p]) //Switch 4
 {
   case 4:
   case 5:
   case 6:
   case 8:
   case 9:
   case 0:

        {
          for(j=1;j<=temp;j++)
          matrix[p][j][0]='|';              
        }break;       
 }//Switch_4 ends 

 switch(n[p]) //Switch 5
 {
   case 0:
   case 2:
   case 6:
   case 8:
        {
          for(j=temp+2;j<=row-2;j++)
          matrix[p][j][0]='|';              
        }break;       
      
 }//Switch_5 ends 


  switch(n[p]) //Switch 6
 {
   case 1:
   case 2:
   case 3:
   case 4:
   case 7:
   case 8:
   case 9:
   case 0:
        {
     
          for(i=1;i<=temp;i++)
          matrix[p][i][col-1]='|';              
        }break;       

 }//Switch_6 ends


 switch(n[p]) //Switch 7
 {
   case 0:
   case 1:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
   case 9:
        {
          for(j=temp+2;j<=row-2;j++)
          matrix[p][j][col-1]='|';              
        }break;       
 }//Switch_7 ends 

}//Assignment ends


 for(i=0;i<=row-1;i++)
{
 for(p=0;p<=3;p++)   
   { 
    for(j=0;j<=col-1;j++)
    {  
      P("%c",matrix[p][i][j]);                 
    }
  for(cnt=0;cnt<=size/2;cnt++)
  P(" ");
 }
P("\n"); 
}

P("\n\n\n");
}//While 1 ends ....
getch();

}

Sunday 16 December 2012

USE OF HEADER NODE IN LINKED LIST

The header node is a node of linked list which may or may not have the same data structure of that of a typical node.The only common thing between a typical node and a header node is that they both have a pointer pointing to a typical node.

Such a node can be used to store some extra pointers,for example

1)In a circular linked list it may store a pointer to middle node or an integer that stores the total number of nodes of the list.

2) In a simple linked list it may store a sentinel value like total number of nodes in the list or may point to the last node of the linked list.

Saturday 15 December 2012

TOWER OF HANOI- UNDERSTANDING THE RECURSION


PROBLEM: A stack of disks placed on pole A has to be moved to pole C using pole B as an intermediate pole for transition keeping in mind the condition that a disk of larger diameter cannot be place on that of a smaller diameter.   

SOLUTION : The very essence of solving this problem lies in understanding the recursive nature of the problem.Considering that there  'n' disks on pole A ;the problem can be broken into 3 repeating steps:

AIM: do something to move n disks from A to C using B.

step1: do something to move n-1 disks from A to B using C.

step2:move 'n' th disk from A to C.

step3:do something to move n-1 disks from B to C using A.

Note that the term "do something" is common with the AIM of procedure
steps 1 and 3 . Step 1 and 3 are itself new aims and will follow the same procedure (step 1,2,3 under the aim of step 1 )

Every time step 1 and step 3 will create a new set of procedure which again involve a sub set of procedures.

Here is a code for the tower of hanoi problem :

#include<stdio.h>
void tow(char x,char y,char z,int n);  // function declaration

int main()
{
int h;char a='a',b='b',c='c';
printf("enter the no of disk on stack a");
scanf("%d",&h);
printf("\nmovement of disks from stack a to stack c\n");
tow(a,b,c,h);                                         //function call
return 0;
}

void tow(char x,char y,char z,int n) // function
{
if(n-1>0)
 {
 tow(x,z,y,n-1);                                   // step1
 printf("\nmove  %d th  disk from %c to %c \n",n,x,z); //step2
 tow(y,x,z,n-1);                                  //step3
 }
else
 printf("move %d th disk from %c to %c \n",n,x,z);// when only one disk is left
}







SEGMENTATION FAULT (CORE DUMPED) IN C

Common reason why you would get such an error message despite of no syntax error being spotted after compilation are:

1)you are not passing the memory address of the variable to a scanf statement. for example:

int n;
scanf("%d",n); // wrong statement
scanf("%d",&n);//correct statement
In case of a string ,it is not necessary to place '&' :
char ans[10];
scanf("%s",ans);//correct statement

2)your user defined function is falling into an infinite loop;

void recur(int x)
{
recur(x);

3)if you are playing with strings in your code the it is preferable that you use fflush(stdin) after every input statement;

scanf("%s",ans);
fflush(stdin);