Monday, September 18, 2017

A Program to calculate Area of Circle or Perimeter of Circle as per the User Choice (If- Else Example)

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int choice,radius;
cout<<"Enter Radius of Circle\n";
cin>>radius;
cout<<"Enter your choice\n";
cout<<"1. Area of Circle\n";
cout<<"2. Perimeter of Circle\n";
cout<<"3. Exit\n";
cin>>choice;
if(choice==1)
{
   cout<<"Area = "<<3.14*radius*radius;
}
else if (choice==2)
{
   cout<<"Perimeter="<<2*3.14*radius;
}
else if (choice==3)
exit(0);
else
cout<<"Wrong Choice";
getch();
}

STACK USING LINKED LIST (DYNAMIC STACK)

#include<iostream.h>
#include<stdio.h>
#include<process.h>
struct Book
{
int Bookid;
char BookName[20];
Book *Link;
}*start = NULL;
void Push();
void Pop();
void Display();
void main()
{
int choice;
do
{
cout<<"Enter Your Choice\n1. Push\n2. Pop\n3. Display\n4.Exit\n";
cin>>choice;
switch(choice)
{
case 1: Push();
        break;
case 2: Pop();
        break;
case 3: Display();
        break;
case 4: exit(0);
default:cout<<"Wrong Choice. Try Again\n";
}
}while(1);
}
void Push()
{
Book *b = new Book;
cout<<"Enter the Book Id and Book Name\n";
cin>>b->Bookid;
gets(b->BookName);
b->Link = NULL;
if(start==NULL)
start = b;
else
{
b->Link = start;
start = b;
}
}
void Pop()
{
if(start == NULL)
cout<<"Empty Stack\n";
else
{
Book *n = start;
start = start->Link;
cout<<n->Bookid<<" deleted\n";
delete n;
}
}
void  Display()
{
if(start == NULL)
cout<<"Empty Stack\n";
else
{
Book *s = start;
while(s!=NULL)
{
cout<<"Book Id : "<<s->Bookid<<"\n";
cout<<"Book Name : "<<s->BookName<<"\n";
s=s->Link;
}
}
}

Program to create stack using array (as a member of a class)

#include<iostream.h>                  
#include<stdio.h>

#include<process.h>
class STACK
{
int ST[50];
int top;
public:
STACK()
{
top=-1;
}
void Push();
void Pop();
void Display();
};
void STACK::Push()
{
if(top==49)
cout<<"Stack is Full\n";
else
{
cout<<"Enter Element to push on to the stack\n";
cin>>ST[++top];
}
}
void STACK::Pop()
{
if(top==-1)
cout<<"Empty Stack. Nothing is there to pop\n";
else
cout<<ST[top--]<<" deleted\n";
}
void  STACK::Display()
{
int t = top;
if(t==-1)
cout<<"Empty Stack\n";
else
{
while(t>=0)
{
cout<<ST[t--]<<"-->";
}
cout<<"\bEND\n";
}
}
void main()
{
int choice;
STACK S;
do
{
cout<<"Enter Your Choice\n1. Push\n2. Pop\n3. Display\n4. Exit\n";
cin>>choice;
switch(choice)
{
case 1:  S.Push();
        break;
case 2:  S.Pop();
         break;
case 3:  S.Display();
         break;
case 4:  exit(0);
default:cout<<"Wrong Choice. Try Again\n";
}
}while(1);
}


PROGRAM DETAILS WITH EXPLANATION OF EACH LINE
#include<iostream.h>                          //This header file is required to add code for cout and cin.         #include<process.h>                           //For exit(0) 
class STACK                                      WE DECLARED A CLASS NAMED STACK
{                                                         //class starts here
int ST[50];                                         //An array with size 50 to work as an static stack.
int top;                                               //An element to keep track on the topmost element on the stack.
public:
STACK()                                          //Constructor to initialize top as -1 because at the time of first 
{                                                       //entry it will move to 0 by ++top as 0 is the first element of the 
top=-1;                                             //stack. we use ++top because of using pre-increment it becomes
}                                                      //0 first and then input will be in ST[0] index.
void Push();                                    //WE created Push( ), a function to input new entry on stack.
void Pop();                                    //WE created Pop( ), a function to remove top entry on stack.
void Display();                              //WE created Display( ), a function to display ell entries on stack.
};
void STACK::Push()                    //Here definition of Push( ) function.
{
if(top==49)                                  //if topmost element is at index 49, then stack is full, no more entries
cout<<"Stack is Full\n";             //possible.
else
{                                                  //otherwise ask user to enter the new value to push on to stack.
cout<<"Enter Element to push on to the stack\n";
cin>>ST[++top];                       //Here first top element will move to next index in array and then 
}                                                //it enter the new value at the new index which is blank.
}
void STACK::Pop()                  //Here definition of Push( ) function.
{
if(top==-1)                                 //if topmost element is at index -1, then stack is vacant, no more       
cout<<"Empty Stack. Nothing is there to pop\n";       //values are on to the stack to pop.
else
cout<<ST[top--]<<" deleted\n";               //Otherwise top element is popped at topomost index 
}                                                               //and then top index will move down by one index as --top.
void  STACK::Display()                         //Here definition of Display( ) function.
{
int t = top;                                               //I saved the value of top in a new variable because i will 
if(t==-1)                                                 //move this new variable and it will not affect the original top.
cout<<"Empty Stack\n";                       //So again, if top index is -1, stack is empty.
else
{
while(t>=0)                                            //Otherwise it will display all entries in backwards direction
{                                                             //from top to bottom as per the concept of STACK
cout<<ST[t--]<<"-->";                           // and moves the new index to 0 and stops.
}                                                             //Note - Our original top is still at the top. (t is not top)
cout<<"\bEND\n";                                //\b is used to delete the last > So that it will display values
}                                                            // in a format like 15-->14-->13-->12-->11--END
}                                                            //Note in the end > sign is not there to not to confuse.

void main()                                          //Main program starts here.
{
int choice;                                        //A variable to take input for 1.PUSH/2.POP/3.DISPLAY/4.EXIT.
STACK S;                                       //Taking object of STACK class.
do                                                    //Starting loop to make and infinite menu to get more and more 
{                                                      //entries, loops will stop only when user press 4. (for Exit).
cout<<"Enter Your Choice\n1. Push\n2. Pop\n3. Display\n4. Exit\n";              //Display Menu
cin>>choice;                                  //Taking choice of user out of 1.PUSH/2.POP/3.DISPLAY/4.EXIT.
switch(choice)                               //switch is used to execute one function as per user choice.
{
case 1:  S.Push();                          //Push function invoked if used pressed 1 and wants to push entry.
        break;
case 2:  S.Pop();                          //Pop function invoked if used pressed 2 and wants to pop top entry.
         break;
case 3:  S.Display();                    //Display function invoked if used pressed 1 and wants to display
         break;                                 //all entries from top to bottom.
case 4:  exit(0);                           //If user wants to quit the program and exits the application.
default : cout<<"Wrong Choice. Try Again\n";          //If user enter anything other than 1 to 4.                 
}
}while(1);                                  /Makes menu display infinte. Exit only by exit(0) (press 4 in menu.)
}

Friday, July 22, 2016

Hello World - Starting Learning C++

Before we start learning C++, i have to tell you that as you already know C++ is a language just like English, Urdu etc. and the main goal of learning a language is to communicate whether its an order, request or just information passing. In the same way C++ is a language to communicate between user (programmer) and computer (through compiler).

As we already know computer knows only 0's and 1's and it understand everything using combination of 0'2 and 1's. It may seems confusing a t a first glance that how can someone even if its a machine can understand everything using 0's and 1's or the various combinations of 0's and 1's like 001,100 etc. But you know in rel life too, we understand or communicate everything using alphabets but we are not having only 26 names in this world but millions of names and things using combination of these alphabets i.e. a-z or A-Z or  the various combinations of these alphabets like JAYA and AJAY are having equal no. of A(2), J(1) and Y(1) but using different combination of same characters