Monday 29 April 2013

History of C++

History of C++

C++ was developed by Bjarne Stroustrup at AT & T Bell Laboratories in Muarry Hill, New Jersy (USA) in 1983. C++ is an OOP i.e. Object Oriented Programming, which allows programmers to develop large and complex applications. The OOP languages existed before C++ were slow and inefficient.

So, Bjarne Stroustrup who was a great admirer and master of C and SIMULA 67, combined the features of both the languages into more powerful language. This combination of features of SIMULA 67 and C resulted in a new language called C with Classes by Bjarne in 1979. C with Classes lacked some OOP features. Therefore some features and ideas were taken from ALGOL 68 (Algorithmic Language). Thus it resulted in C++. The name C++ is credited to Rick Mascitti who suggested this name and was first used in 1983. It is named C++ not C+ or ++C because C+ has been used as the name of an earlier unrelated language and C++ is more commonly used than ++C. Moreover, it is not named D language because it is an extension of C.

Version                year
1.0                    1983
2.0                    1989
3.0                    1992

Creator of C++: Bjarne Stroustrup.


Brief History of C++

  1. During 1970 Dennis Ritchie created C Programming language.
  2.  In the early 1980′s, also at Bell Laboratories, another programming language was created which was based upon the C language.
  3. New language was developed by Bjarne Stroustrup and was calledC++.
  4. Stroustrup states that the purpose of C++ is to make writing good programs easier and more pleasant for the individual programmer.
  5. C++ programming language is extension to C Language.
  6. In C we have already used increment operator (++) . Therefor we called C++ as “Incremented C” means Extension to C.

Tuesday 16 April 2013

Simply Explained Iterative (Looping):


Iterative (Looping): Iterative is also known as looping. It is used when we want to execute a statement again and again. The process of executing a statement again and again is known as loop. Such type of statement is known as iterative or looping statement. Loop statements are categorized in two controls: entry control loop and exit control loop. Entry control loop is the loop where condition is checked at entry point. If it is true then body of loop is executed. Otherwise the control is moved out of the loop when condition becomes false. Entry control loop is also called base loop. The while-loop and for-loop are example of entry control loop. Exit control loop is the loop when condition is checked at exit point. If condition is true then body of loop is executed otherwise not. The example of exit control loop is do-while loop statement. Each loop has the same purpose i.e. repeating the task again and again till condition becomes false. The basic loop statements are:
while loop
do while loop
for loop


1) while loop: While loop is an entry control loop. First of all, the condition is checked at entry point. If it is true then body of loop is executed otherwise loop is terminated. This loop will execute again and again till condition becomes false.
General syntax of while loop:
initialization;
while(condition)
Notes By:
Vijay Kumar Bhargav (98728-71471)
{
   statements;
   incre/decre;
}
Example of while loop:
#include<stdio.h>
#include<conio.h>
void main()
{
     int w;
     w=0;                                                            //initialization
     while(w<5)
     {
         printf("\n my name is while loop");       // executable statement
         w++;                                                       // increment
     }
       printf("while loop is terminated");
       getch();
}
2) do-while loop: The do while loop is an exit control loop. In this loop the condition is checked at exit point. First of all body of loop is executed then condition is checked. If condition is true then body is executed again, otherwise not. It is also known as do-loop. Body of loop is executed till the condition becomes false.
General syntax of do loop:
initialization;
do
{
    statements;
    incre/decre;
} while(condition);
Note that in while loop there is no semi colon at the end but in do while there is semi colon at the end when while (condition) is written. Example:
#include<stdio.h>
#include<conio.h>
void main()
{
    int d;
    d=0;                                                             // initialization
    do
    {       printf("i am do while loop");
       d++;                                                         // incre/decre
    } while(d<5);                                              // condition
   printf("exited from do while");
   getch();
}
3) for loop: The for loop is an entry control loop. In this loop, condition is checked at entry point of loop body. Every loop has three parts: initialization, condition check and increment/decrement. In other loops all three parts initialization, condition check and increment/decrement are written at separate places. But in for loop, all three parts are written at same place in single line.
General syntax of for loop:
for(initialization; condition check; incre/decre)
{
   body of loop;
Notes By:
Vijay Kumar Bhargav (98728-71471)
}
Example of for loop:
#include<stdio.h>
#include<conio.h>
void main()
{
    int i;
    for(i=0;i<5;i++)
    {
       printf("i am for loop");
    }
   getch();
}
Each loop has the same purpose i.e. repeating the task again and again till condition becomes false.


4) nested for loop: When a for loop occurs within another for loop then it is known as nested for loop. We can apply number of nested for loops in C language.
General syntax of nested loop:
for(initialization; condition check; incre/decre)
{   for(initialization; condition check; incre/decre)
            {          body of inner loop;
            }
       body of outer loop;
}
In nested loop, inner loop will be executed first then outer loop will be executed. Example:
#include<stdio.h>
#include<conio.h>
void main()
{    int i,j;
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
       {  printf("this is inner loop");
       }
          printf("this is outer loop");
     }
    printf("exit from all loops");
    getch();
}

Blog Author: Vijay Kumar


Friday 12 April 2013

What is the far pointer in c?


What is the far pointer in c?  
Explanation:
The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer.


Size of far pointer is 4 byte or 32 bit. Examples:

(1) What will be output of following c program?

int main(){
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}

Output: 4

(2)What will be output of following c program?

int main(){
int far *near*ptr;
printf("%d %d",sizeof(ptr) ,sizeof(*ptr));
return 0;
}

Output: 4 2
Explanation: ptr is far pointer while *ptr is near pointer.

(3)What will be output of following c program?

int main(){
int far *p,far *q;
printf("%d %d",sizeof(p) ,sizeof(q));
}

Output: 4 4

First 16 bit stores: Segment number
Next 16 bit stores: Offset address

Example:

int main(){
int x=100;
int far *ptr;
ptr=&x;
printf("%Fp",ptr);
return 0;
}

Output: 8FD8:FFF4
Here 8FD8 is segment address and FFF4 is offset address in hexadecimal number format.

Note: %Fp is used for print offset and segment address of pointer in printf function in hexadecimal number format.
In the header file dos.h there are three macro functions to get the offset address and segment address from far pointer and vice versa.

1. FP_OFF(): To get offset address from far address.
2. FP_SEG(): To get segment address from far address.
3. MK_FP(): To make far address from segment and offset address.

Examples:
(1)What will be output of following c program?

#include "dos.h"
int main(){
int i=25;
int far*ptr=&i;
printf("%X %X",FP_SEG(ptr),FP_OFF(ptr));
}

Output: Any segment and offset address in hexadecimal number format respectively.

(2)What will be output of following c program?

#include "dos.h"
int main(){
int i=25;
int far*ptr=&i;
unsigned int s,o;
s=FP_SEG(ptr);
o=FP_OFF(ptr);
printf("%Fp",MK_FP(s,o));
return 0;
}

Output: 8FD9:FFF4 (Assume)
Note: We cannot guess what will be offset address; segment address and far address of any far pointer .These address are decided by operating system.

Limitation of far pointer:

We cannot change or modify the segment address of given far address by applying any arithmetic operation on it. That is by using arithmetic operator we cannot jump from one segment to other segment. If you will increment the far address beyond the maximum value of its offset address instead of incrementing segment address it will repeat its offset address in cyclic order.

Example:

(q)What will be output of following c program?

int main(){
int i;
char far *ptr=(char *)0xB800FFFA;
for(i=0;i<=10;i++){
printf("%Fp \n",ptr);
ptr++;
}
return 0;
}

Output:

B800:FFFA
B800:FFFB
B800:FFFC
B800:FFFD
B800:FFFE
B800:FFFF
B800:0000
B800:0001
B800:0002
B800:0003
B800:0004

This property of far pointer is called cyclic nature of far pointer within same segment.

Important points about far pointer:

1. Far pointer compares both offset address and segment address with relational operators.

Examples:

(1)What will be output of following c program?

int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
if(p==q)
printf("Both pointers are equal");
else
printf("Both pointers are not equal");
return 0;
}

Output: Both pointers are not equal

(2)What will be output of following c program?

int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
int near *x,near*y;
x=(int near *)p;
y=(int near *)q;
if(x==y)
printf("Both pointer are equal");
else
printf("Both pointer are not equal");
return 0;
}

Output: Both pointers are equal

2. Far pointer doesn’t normalize.
20
What is a cyclic property of data type in c? Explain with any example. 
Explanation:
#include<stdio.h>
int main(){
    signed char c1=130;
    signed char c2=-130;
    printf("%d  %d",c1,c2);
    return 0;
}

Output: -126   126 (why?)
This situation is known as overflow of signed char. 
Range of unsigned char is -128 to 127. If we will assign a value greater than 127 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If we will assign a number which is less than -128 then we have to move in anti-clockwise direction.

Tuesday 9 April 2013

Recursion V\S Iteration

Recursion V\S Iteration

1) Recursive function – is a function that is partially defined by itself whereas Iterative functions are loop based imperative repetitions of a process

2) Recursion Uses selection structure whereas Iteration uses repitation structure

3) An infinite loop occurs with iteration if the loop-continuation test never becomes false whereas infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case.

4) Iteration terminates when the loop-continuation condition fails whereas recursion terminates when a base case is recognized

5) When using recursion multiple activation records are created on stack for each call when in iteration everything is done in one activation record

6) Recursion is usually slower then iteration due to overhead of maintaining stack whereas iteration does not use stack so it's faster than recursion

7) Recursion uses more memory than iteration
8) Infinite recursion can crash the system whereas infinite looping uses cpu cycles repeatedly

9) Recursion makes code smaller and iteration makes code longer.




Blog Author: Vijay Kumar

Recursion Completely Explained


Recursion: 
The programming language allows you to divide your program into the functions. Using functions, your program becomes easier to understand and to debug. Also the functions can be shared among various programs. We can call a function inside a function, it is known as nesting of functions. The programming language C even allows calling a function itself. When a function calls itself then it is known as recursive function. The process of a function calling itself is known as Recursion. Here is the example to calculate the factorial of a number:
 int factorial(int a)
 {
   if (a == 1)
   return 1;
   else
    {
      a *= factorial(a-1);
      return a;
   }
 }
let a=4
4 != 1 so the if portion is skipped for now.
4*=factorial(3)
3 != 1
3*=factorial(2)
2 != 1
2*=factorial(1)
1==1 so 1 is returned
2*1=2 so 2 is returned
2*3=6 so 6 is returned
6*4=24 so 24 is returned
The function doesn't get to the 'return a' until the factorial(a-1) returns a number which doesn't happen until a==1.
Recursive functions should be avoided at all costs because they get very complex. Large numbers would consume a lot of memory to do something as simple as a factorial.


Blog Author: Vijay Kumar