Friday 22 March 2013

Stack


Stack : Stack is a linear data structure in which insertion and deletion of elements takes place only one end known as TOP.


The stack is sometimes also called as LIFO List (Last-In-First-Out), because the last element pushed
into the stack is the first one to be popped out.

Example Code is given here :-


#include<iostream.h>
#include<conio.h>
struct node
{
  int data;
  node *next;
};
class stack
{
      node *top;
public :
      stack()
      { top=NULL;}
       void push();
       void pop();
       void display();
      ~stack();
};
void stack::push()
{
      node *temp;
      temp=new node;
      cout<<"Enter data :";
      cin>>temp->data;
      temp->next=top;
      top=temp;
}
void stack::pop()
{
      if(top!=NULL)
      {
            node *temp=top;
            top=top->next;
            cout<<temp->data<<"deleted";
            delete temp;
      }
      else
            cout<<"Stack empty";
}
void stack::display()
{
      node *temp=top;
      while(temp!=NULL)
      {
        cout<<temp->data<<" ";
        temp=temp->next;
      }
}
stack::~stack()
{
      while(top!=NULL)
      {
            node *temp=top;
            top=top->next;
            delete temp;
      }
}
void main()
{
      stack st;
      char ch;
      do
      {
            cout<<"stack options\nP for push \nO for Pop \nD for Display \nQ for quit";
            cin>>ch;
            switch(ch)
            {
                  case 'P': st.push();break;
                  case 'O': st.pop();break;
                  case 'D': st.display();break;
            }
      }while(ch!='Q');
}

Queue Implementation

Queue :- Queue is a linear data structure in which  insertion of new element take place at one end known as Rear or Back end and deletion of existing element takes place at the other end known as Front end.


Queues are sometimes also called  FIFO List (First-In-First-Out).


Queue Implementation 


#include<iostream.h>
#include<conio.h>
struct node
{
      int data;
      node *next;
};
class queue
{
      node *rear,*front;
public:
      queue()
      { rear=NULL;front=NULL;}
      void qinsert();
      void qdelete();
      void qdisplay();
      ~queue();
};
void queue::qinsert()
{
      node *temp;
      temp=new node;
      cout<<"Data :";
      cin>>temp->data;
      temp->next=NULL;
      if(rear==NULL)
      {
            rear=temp;
            front=temp;
      }
      else
      {
            rear->next=temp;
            rear=temp;
      }
}
void queue::qdelete()
{
      if(front!=NULL)
      {
            node *temp=front;
            cout<<front->data<<"deleted \n";
            front=front->next;
            delete temp;
            if(front==NULL)
                  rear=NULL;
      }
      else
            cout<<"Queue Empty..";
}
void queue::qdisplay()
{
      node *temp=front;
      while(temp!=NULL)
      {
            cout<<temp->data<<endl;
            temp=temp->next;
      }
}
queue::~queue()
{
      while(front!=NULL)
      {
            node *temp=front;
            front=front->next;
            delete temp;
      }
}
void main()
{
      queue obj; char ch;
      do
      {
            cout<< "i. insert\nd. Delete\ns. Display\n q. quit ";
            cin>>ch;
            switch(ch)
            {
                  case 'i' : obj.qinsert();break;
                  case 'd' : obj.qdelete();break;
                  case 's' : obj.qdisplay();
            }
      }while(ch!='q');
}


Friday 8 February 2013

Data Handling

Basic Data Types



C++ supports a large number of data types. Built in data type integers which are supported by C++ are integers, floating point and characters.

These are summarised in the table given below


TypeByteRangeDescription
int2-32768 to +32767Small whole number
long int4-2147483648 to +2147483647Large whole number
float43.4x10-38 to 3.4x10+38Small real number
double81.7x10-308 to 1.7x10+308Large real number
long double103.4x10-4932 to 3.4x10+4932Very Large real number
char10 to 255A Single Character


Variables

Variables are the location in computer memory where program stores the data and it is given a symbolic name for a easy reference.The variable can be used to hold different value on different times.

For example if we write this :-  A = 55 , than the value 55 is assigned to the Variable A.. 

Now after some time we write this :- A=78 , now the value of the Variable A becomes 78 and the previous value is now erased from the Variable A.

You can use any Word to assign a Value.
Declaring a Variable

Declaring a variable is easy. Before Declaring a variable we must declare the type of Variable.

For example if you are going to store a decimal number into a variable then you must declare it's type to be " Float " this makes compiler to make an appropriate space for storing the data.


Some Examples:-

1. If you want to assign an integer. For eg. 459 than type this.

 Int x;
  x=459;

2.If you want to store a Decimal Number. For eg. 459.87

Float x;
x=459.87

This process of assigning a value to Variable is Called Initializing.


Input/Output ( I/O)


C++ support the facility to take input while running the program.These can be used to feed new data in computer.


Following Stream is used for taking input/output in C++

Cin>>                                                                         //this is used for taking input.
Cout<<                                                                      //this is used for output.


Well don't forget to put ">>" and "<<" after cin and cout respectively. These are must. "<<" & ">>" and are operators which must be used with Cin and Cout.

Many people get confused about which operator to be used after cin and cout. The Best way to learn this is :-

Cout means outputting the data.. and the operator  " << " is also looks like that it is giving out something hence use "<<" with Cout and ">>" with Cin.


Now some exercise is for you..

Question 1. what is the size of word "Computer" in computer memory??
Question 2. Which data type will you declare for this number --> 45.76 ?? hint--> int or float??
Question 3. is this statement is written correctly??
                  cin<< what the F*** ???

Type Conversion

Now you know well about C++, so it's time to learn something new.

Type conversion is a process in which a pre-defined type of expression is converted into another type is called conversion.

There are two type of conversions in c++.
  1. Implicit Conversion.
  2. Explicit Conversion.

Implicit Conversion.

In this type of conversion data type can be mixed.
For Example :-


                  double a; 
                  int b = 5; 
                  float c = 8.5; 
                  a = b * c;
When two operands of different type are encountered in the same expression, the lower type variable is converted to the higher type variable. The following table shows the order of data types.

Order of data types
Data type
long double
double
float
long
int
char
order

(highest)
To

(lowest)
The int value of b is converted to type float and stored in a temporary variable before being multiplied by the float variable c. The result is then converted to double so that it can be assigned to the double variable a.

Basics Of C++

C++ Character Set



These are set of valid character which compiler understands.

LettersA-Z, a-z
Digits0-9
Special CharactersSpace  +  -  *  /  ^  \  ()  []  {}  =  !=  <>  ‘  “  $  ,  ;  :  %  !  & ? _  #  <=  >=  @ 
Formatting charactersbackspace, horizontal tab, vertical tab, form feed, and carriage return

I would suggest to write the full source code in small characters.

Tokens

Tokens are the group of characters which are logically belong together.The Programmer ( one who writes the program ) can write the source code using these tokens. C++ uses these type of tokens :

Keywords, Identifier, Literals, Punctuators, Operators.

1. Keyword

Keywords are the reserved words in C++ which have predefined meanings to compiler.The most common keywords are given below

asmautobreakcasecatch
charclassconstcontinuedefault
deletedodoubleelseenum
externinlineintfloatfor
friendgotoiflongnew
operatorprivateprotectedpublicregister
returnshortsignedsizeofstatic
structswitchtemplatethisTry
typedefunionunsignedvirtualvoid
volatilewhile

2. Identifiers

Symbolic names can be used in C++ program. A programmed uses these symbolic names for giving a particular value to it or for any other use. For example Vivek = Yuvraj .. In it vivek is an Identifier having some value.

These symbolic names (eg. Vivek ) are  called Identifiers.

There are some rules for creating an Identifier.

  • An Identifier can consist of Alphabet, Digits, or underscore "_"
  • Identifier must not start with a digit.
  • C++ is case sensitive ie. Upper case and Lower case words have different meaning. That's why i told you to use lower case for writing a source code.
  • It should not be a reserved word.

3. Literals

Literals are the data items whose value does not change from the starting of the program till it's execution.
The following types of Literals are there in C++ Compiler.

  • Integer Constants
  • Character Constants
  • Floating Constants
  • String Literals

 Integer Constant

     Integer Constant are whole number with no Fraction Part. C++ allows three types of Integer Constants.
     Decimal Integer Constant : It consist of sequence of digits like any ordinary number , but it should not 
                                                   start with a Zero ( 0 ) . Some Examples. :-  111,444,12314, -87

     Octal Integer Constant     : It consist of sequence of  digits starting with Zero. Eg. 041,054

      Hexadecimal Integer Constant : It consist of sequence of digits proceeded by ox and OX.


Character Constant

A character constant in C++ must contain one or more characters and must be enclose within brackets. For Example 'A' , '9' etc. C++ also allows non graphic characters ,these are the characters which can be typed directly by Keyboard, Example :- Backspace, Tab , Home etc.These characters can be represented by an escape sequence. An escape sequence represents a single character.The following table gives a listing of common escape sequences.

Escape SequenceNongraphic Character
\aBell (beep)
\nNewline
\rCarriage Return
\tHorizontal tab
\0Null Character

Floating Constants
Floating Constants are also called real constants. They are the number which have fractional Parts. They can be written in Fractional Form. In other words these are the constants which can store the value of a Decimal number. Example :- 4.5666  ,  5.4 etc.

String Literals

These are the sequence of characters enclosed within double quotes is called a string literal .String literal is automatically added with a special character '\0' which donates the end of a string. As a result size of a string in C++ is 1 more than the actual length. For Example :- Word " Facebook " has total 8 characters but in computer memory it will be represented by " Facebook\0 " , Now in computer memory it has total 9 Characters. 

1 more example is taken. Word " Lappy " has total 5 characters. Now without thinking add 1 to it and it becomes 6. Hence in computer memory word Lappy has 6 characters.

4. Punctuators

These characters are used as Punctuators in C++

Brackets [   ]Opening and closing brackets indicate single and multidimensional array subscript.
Parentheses (   )Opening and closing brackets indicate functions calls,; function parameters for grouping expressions etc.
Braces {   }Opening and closing braces indicate the start and end of a compound statement.
Comma ,It is used as a separator in a function argument list.
Semicolon ;It is used as a statement terminator.
Colon :It indicates a labeled statement or conditional operator symbol.
Asterisk *It is used in pointer declaration or as multiplication operator.
Equal sign =It is used as an assignment operator.
Pound sign #It is used as pre-processor directive.


5. Operators

Operators are the special symbols used for special purposes. There are many Operators available in C++ , but i have written the most common operators below :-

  1. Arithmetical Operator
  2. Relational Operator
  3. Logical Operators.
  4. Unary Operators
  5. Assignment Operators
  6. Conditional Operators
  7. Comma Operator







Friday 2 November 2012

Getting Started




As we all know computer is just a machine and it does not understand our language, but there would be something for Computer?? Well it's binary Language, which computer uses for it's different purposes. So if we need to ask computer something ,it should be in the language which computer can understand. Which is Binary Language.But we are humans who can we speak or write binary???

For this purposes software like C++ are made , which convert our Language into Binary Language and work as a medium between Humans and Computer.C++ use compiler for doing this.

In short Computer do not understand our language, it uses Binary Language. And C++ is the program which convert our Language into Binary Language.

What is C++ Compiler?

It is nothing just a computer program , whose job is to covert the C++ program from our form to the form which Computer can read and than think over it ( Execute ).

The original C++ program written is called the "source code" and the resulting code after compilation is called " object file "


Before compilation takes place  the preprocessor performs preliminary operations on C++ source files. When preprocessor does it's work the new form of source code ( preprocessed ) is sent to the compiler.


When compilation is completed the object files are combined with predefined libraries by a binder commonly known as Linker, to produce the final complete file which can be executed by the computer.

Library - A library is a collection of pre-compiled “object code” that provides operations that are done repeatedly by many computer programs.

See this chart to get better idea

Turbo C++ Compiler

You need to Download Turboo C++ Compiler to write a source code. Don't worry it's open source software and can be find easily on internet. Just Google  it. Download it and Install it.

If you find difficulty in installing Turboo C++ Compiler than see this full Tutorial

When you are done it should look like this.



For writing a new source code click on File Tab and Than New...

Now this was just Starting Click here to see the Next Post.








Thursday 1 November 2012

About US




Well, I am Vivek singh... I'm perusing my 12th from Saint Francis Senior Secondary School,Hathras......Next month i am going to have Board Exams...I thought many students are there who find difficulty in Computer Science. So i decided to make this site, it could be useful to you and even to me as my revision is going on by writing all this stuff for you..Now check the C++ from starting  :)