Tuesday, 26 February 2013

Implementation of STACK in C++


#include<iostream>
using namespace std;
int main()
{
int stack[20],top=-1,ch;
//MAX. SIZE IS 5.
while(1)
{
cout<<"PRESS 1 TO PUSH\nPRESS 2 TO POP\nPRESS 3 TO DISPLAY\nPRESS 4 TO EXIT\n";
cin>>ch;
switch(ch)
{
case 1: if(top==4) 
       cout<<"OVERFLOW\n";
       else
{
         cout<<"ENTER THE ELEMENT TO BE PUSHED:";
         cin>>stack[++top];

break;
case 2:if(top==-1)
       cout<<"UNDERFLOW\n";
else
cout<<"ELEMENT POPED:"<<stack[top--]<<endl;
break;
case 3: if(top==-1)
cout<<"EMPTY STACK\n";
else
{ for(int i=top;i>=0;i--)
 cout<<stack[i]<<"\t";
 cout<<endl;

break;

case 4 :break;
default:cout<<"INVALID ENTRY\n";
}//END OF SWITCH
if(ch==4)
break;
}//END OF WHILE
return(0);
}

Monday, 25 February 2013

C++ program :Array of all prime numbers less then or equal to n (SIEVEN)


#include<iostream>
using namespace std;
int main()
{
int A[20],B[20],i,n,j;
cout<<"Enter the positive intiger:";
cin>>n;
for(i=2;i<=n;i++)
B[i]=i;
for(i=2;i<=n;i++)
{
if(B[i]!=0)
{   j=i*i;
   while(j<=n)
  {  B[j]=0;
     j+=i;
   }
}
}

j=0;
for(i=2;i<=n;i++)

if(B[i])
{
A[j++]=B[i];
cout<<B[i]<<"\t";
}
}
cout<<endl;
return(0);
}

Saturday, 23 February 2013

Swap two numbers

Here a,b are two variables of type int.

Method 1:
                          a=a+b;
                          b=a-b;
                          a=a-b;

Method 2:
                          a=a*b;
                          b=a/b;
                          a=a/b;

Method 3:
                          a=a^b;
                          b=a^b;
                          a=a^b;

Method 4:
                          a=(a+b)-(b=a);


Method 5:
                 /*With temp. variable*/
                          temp=a;
                          a=b;
                          b=temp;


                         
                         
                     

Tuesday, 19 February 2013

A Trick to trace tower of Hanoi manually


  • What procedure suggests:               

         If n=1 move the disk from source to destination.
         Otherwise
(i) Move n-1 disks from source to temp.  peg by using destination peg as  auxiliary.
(ii)Move nth  disk from temp. to destination then move n-1 disks using source peg as c   auxiliary.

  • An alternative for easy manual tracing.
           Construct a tree a shown in fig.
         (I) If n!=1then lftchild ={n-1,S,D,T} and rtchild={n-1,T,S,D}
         Do this till n=1;
         For n=3 you will get tree as shown in fig. to get moves go for inorder traversal of tree,
         ( Move nth disk from source to destination.{n, s, t, d} )  
Inorder Traversal
1. Traverse the left sub tree in inorder.  
2. Visit the root.
3. Traverse the right sub tree in inorder.

Do the Inorder traversal for the above problem   
Move disk 1 from A to C.
Move disk 2 from A to B.
Move disk 1 from C to B.
Move disk 3 from A to C.
Move disk 1 from B to A.
Move disk 2 from B to C.
Move disk 1 from A to C.

Why C?

  • C is building block for many other currently known languages.
  • Learning C provides a strong procedural background with worthy skill set.
  • Language constructs in C such as “if” statements,”for”,”while” loops and types of variables can found in many modern language.
  • For a programmer who hasn’t learned something like C, will face difficulty in handling concepts like pointers, data types, passing values by reference.
  • Knowing C takes you closer to the hardware, to better understand how things work on system.
  • Many blocks of operating systems like UNIX, Linux & windows are coded in C, because no other language stands when it comes to performance.
  • While learning C++ or JAVA we come across concepts like class, objects, inheritance, polymorphism, templates etc, which are very difficult to understand without being comfortable with basics.

Wednesday, 13 February 2013

C program to get size of a file (approx.)


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char c,name[10];
long int size=0;
clrscr();
printf("Enter the file name with extension(like .txt,.c,.cpp):");
scanf("%s",name); 
fp=fopen(name,"r");
if(fp==NULL)
{
printf("Unable to open the file");
getch();
exit(5);
}
while((fscanf(fp,"%c",&c))!=EOF)
size++;
printf("Size of file: %ld bytes",size);
getch();
}

C program which prints its code.


/*Save this program as proprint.c*/ 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("proprint.c","r");
/*You can replace here program name to get that program printed */
if(fp==NULL)
{
printf("Unable to open");
getch();
exit(5);
}
while(fscanf(fp,"%c",&ch)!=EOF)
printf("%c",ch);
getch();
}


Sunday, 10 February 2013

C programs for pascal triangle

/* Method 1*/

#include<stdio.h>
unsigned long int fact (int a);

int main(){
int i,j,k,n;
printf("Enter the number:");
scanf("%d",&n);
for(i=0;i<n;i++){
    k=0;
for(j=0;j<n;j++){
     if(i+j>=n-1){
           printf(" %d",fact(i)/(fact(k)*fact(i-k)));
           k++;
    }
     else
            printf(" ");
}

 printf("\n");
}
return 0;
}



 unsigned long int fact(int a)
{
unsigned long int q,w=1;
for(q=1;q<=a;q++)
w=w*q;
return(w);
}




                                                        /* Method 2*/




#include<stdio.h>
#include<conio.h>
void main()
{
int l,i,r=0,sum=0,sp;
clrscr();
printf("Enter the req. number of lines\n");
scanf("%d",&l);
while(r<l)
{
for(sp=30-2*r;sp>0;sp--)
printf(" ");
for(i=0;i<=r;i++)
{
if(i==0||r==0)
sum=1;
else
sum=(sum*(r-i+1))/i;
printf("%4d",sum);
}
printf("\n");
r++;
}
getch();
}



Out put-














Fill you screen with smiley icon


/*ASCII value of Smiley Icon is 1.*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int y;
 clrscr();
 for(y=1; y<=1000; y++)
   printf("%2c",1);
 getch();
}

Saturday, 9 February 2013

Some use full comands in turbo C

Alt+F9 = Compile 
Ctrl+F9 = Compile+Link+Run

Basic Cursor Movement Commands.

Character left          Left
Character right         Right
Line up                 Up
Line down               Down
Scroll up               Ctrl-W
Scroll down             Ctrl-Z
Page up                 PgUp
Page down               PgDn

Quick Cursor Movement Commands.

Beginning of line           Home
End of line                 End
Beginning of file           Ctrl Home
End of file                 Ctrl End

Insert and Delete Commands.

Insert Mode On/Off          Insert
Delete line                 Ctrl-Y


Block Commands.

Mark block begin            ctrl-KB
Mark block end              ctrl-KK
Copy marked block           ctrl-KC
Move marked block           ctrl-KV
Delete Marked Block         ctrl-KY
Write block to file         ctrl-KW
Read from a file            ctrl-KR
To unmark the block         ctrl-QY

Miscellaneous Commands.


Load file                          F3
Save                               F2
Quit TurboC                        alt-X
Find                              ctrl-QF
To add watch                      ctrl+F7
To find meaning of key word        ctrl+F1
To save the program                F2
To open the file                   F3
Undo                               Alt+BkSp
Redo                               Shift+Alt+BkSp
Close                              Alt+F3
User screen                        Alt+F5
Trace into                         F7
Step over                          F8

Saturday, 2 February 2013

C programming books.


  1. Let us C-Yashavant Kanetkar.
  2. Programming in ANSI C- Balaguruswamy.
  3. Head first C , by David Griffiths and Dawn Griffiths
  4. Click here for more books-http://www.cprogramming.com/books/c.html