Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Wednesday, 24 April 2013

Inheritance And Its Types In C++

The basic meaning of Inheritance is using a pre-defined code. This is the main advantage in Object oriented Programming (OOPS), where one can use a code that has been previously written and defined but with the condition that the code is used as-is or in other words, we are not changing the code.
All the pre-defined code is used resides into classes. To use those codes, we use a feature called inheritance, whereby, we "inherit" those classes.
The class from which other class inherits is called as Parent or Base Class and the class that inherits is called as Child or Derived Class.
 

Types Of Inheritances:
There are 5 types of Inheritances used in C++:
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchial Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

As Shown in Figure:

Types Of Inheritance C++

Accessibility Modes in Inheritance in C++:
We can use the following table to understand the accessibility of the members in different modes of Inheritance:

Accessibility Modes in Inheritance C++

Here, by 'X' we mean that, that object is not inheritable. Remember, Private objects and functions are never inherited. I'll be discussing about these types of inheritances individually in my next post.



Saturday, 6 April 2013

C Program To Generate Fibonacci Series

Hi Friends,
Today, we'll learn how to generate a Fibonacci Series using C. The program and concept are pretty simple. But before that, let me tell you what exactly is a Fibonacci series.  Fibonacci series is a special series in which the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. By going this way, the series generated will be:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and so on...

The program is very simple.

First input is taken from the user that tells how many terms of the Fibonacci series will be generated and then as described above, a function is called in a loop to add the previous two numbers and generate the next number in the series.


Here is the C Code for Fibonacci Series:


/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

int fib(int n)
{
 if(n==1||n==2)
 return 1;

 else
 return(fib(n-1)+fib(n-2));
}

void main()
{
 int i,n;
 clrscr();
 printf("Enter the number of terms: ");
 scanf("%d",&n);

 printf("\nThe Series is : \n\n0");

 for(i=1;i<n;i++)
 {
  printf("%5d",fib(i)); // fib Function Called
 }
 getch();
}


Output:



C Program To Generate Fibonacci Series



Please Comment If You Liked This Post !! 


Thursday, 28 March 2013

C Code To Check Whether A No. Is Palindrome Or Not

A Palindrome number is a number, which when reversed in order of digits, produces the same number a the original one. Some examples of Palindrome numbers are: 11, 121, 1331. 14641, 15101051, 989, 5225 etc. The C program for the same is very simple. First we will reverse the original number and then check it with the original number. If found same, number is palindrome, otherwise not.

A Simple Algorithm for the same will be:
  1. Input number from user, 'num'.
  2. Apply Logic on 'num', produce reversed number 'rev'.
  3. If num == rev, print 'num' is Palindrome.
  4. Otherwise print 'num' is not Palindrome.

C Program To Check Whether A Number Is Palindrome Or Not:


/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

void main()
{
 int a,b,c,d;
 clrscr();
 printf("Enter a number: ");
 scanf("%d",&a);
 b=a;
 d=0;

 while(a!=0)
 {
  d = d*10;
  c = a%10;
  a = a/10;
  d = d+c;
 }

 if(d==b)
 printf("\nThe number %d is plaindrome",b);

 else
 printf("\nThe number %d is not Palindrome",b);

 getch();
}


Program Output:


Palindrome Number C Code


Please Comment If You Liked This Post !! 


Sunday, 17 March 2013

Selection Sort Algorithm And C Code

In computer science, a selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar Insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.


Complexity:


Worst case performanceО(n2)
Best case performanceО(n2)
Average case performanceО(n2)



Step-by-step example:



Selection Sort Algorithm And C Code

Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11

11 25 12 22 64

11 12 25 22 64

11 12 22 25 64

11 12 22 25 64
(Nothing appears changed on this last line because the last 2 numbers were already in order.)
Selection sort can also be used on list structures that make add and remove efficient, such as a linked list. In this case it is more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far. For example:
64 25 12 22 11

11 64 25 12 22

11 12 64 25 22

11 12 22 64 25

11 12 22 25 64

Algorithm & Pseudo Code For Selection Sort:

/* Double-Click To Select Code */

/* a[0] to a[n-1] is the array to sort */
int i,j;
int iMin;
 
/* advance the position through the entire array */
/*   (could do j < n-1 because single element is also min element) */
for (j = 0; j < n-1; j++) {
    /* find the min element in the unsorted a[j .. n-1] */
 
    /* assume the min is the first element */
    iMin = j;
    /* test against elements after j to find the smallest */
    for ( i = j+1; i < n; i++) {
        /* if this element is less, then it is the new minimum */  
        if (a[i] < a[iMin]) {
            /* found new minimum; remember its index */
            iMin = i;
        }
    }
 
    /* iMin is the index of the minimum element. Swap it with the current position */
    if ( iMin != j ) {
        swap(a[j], a[iMin]);
    }
}

C Program For Selection Sort:

/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

int smallest(int arr[],int k,int n);
void sort(int arr[],int n);

void main()
{
 int arr[20],i,n,j,k;
 clrscr();
 printf("\nEnter the number of elements in the array: ");
 scanf("%d",&n);

 printf("\nEnter the elements of the array");
 for(i=0 ; i < n ; i++)
 {
  printf("\n arr[%d] = ",i);
  scanf("%d",&arr[i]);
 }

 sort(arr,n);
 printf("\nThe sorted array is: \n");
 for(i=0 ; i < n ;  i++)
 printf("%d\t",arr[i]);
 getch();
}

int smallest(int arr[],int k,int n)
{
 int pos=k,small=arr[k],i;
 for(i=k+1;i<n;i++)
 {
  if(arr[i]<small)
  {
   small=arr[i];
   pos=i;
  }
 }
 return pos;
}


void sort(int arr[],int n)
{
 int k,pos,temp;
 for(k=0 ; k < n ; k++)
  {
   pos=smallest(arr,k,n);
   temp=arr[k];
   arr[k]=arr[pos];
   arr[pos]=temp;
  }
}
 

Output Of Program:


Selection Sort Algorithm And C Code


Please Comment If You Liked This Post !! 

This article uses material from the Wikipedia article Selection_sort which is released under the Creative Commons Attribution-Share-Alike License 3.0


Insertion Sort Algorithm And C Code

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as Quicksort, heapsort, or Merge sort. However, insertion sort provides several advantages:
  • Simple implementation
  • Efficient for (quite) small data sets
  • Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
  • More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
  • Stable; i.e., does not change the relative order of elements with equal keys
  • In-place; i.e., only requires a constant amount O(1) of additional memory space
  • Online; i.e., can sort a list as it receives it


Step-by-step example:

The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the item under consideration is underlined. The item that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold.
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
7 4 9 5 2 6 1
4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9

Algorithm/Pseudo-Code:


 for i ← 1 to i ← length(A)-1
   {
     //The values in A[ i ] are checked in-order, starting at the second one
     // save A[i] to make a hole that will move as elements are shifted
     // the value being checked will be inserted into the hole's final position
     valueToInsert ← A[i]
     holePos ← i
     // keep moving the hole down until the value being checked is larger than 
     // what's just below the hole <!-- until A[holePos - 1] is <= item -->
     while holePos > 0 and valueToInsert < A[holePos - 1]
       { //value to insert doesn't belong where the hole currently is, so shift 
         A[holePos] ← A[holePos - 1] //shift the larger value up
         holePos ← holePos - 1       //move the hole position down
       }
     // hole is in the right position, so put value being checked into the hole
     A[holePos] ← valueToInsert 
   }

Here is the C Code for Insertion Sort:



/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

void sort(int arr[],int n); //Function Prototype

void main()
{
 int arr[20],i,n,j,k;
 clrscr();
 printf("\nEnter the number of elements in the array: ");
 scanf("%d",&n);

 printf("\nEnter the elements of the array");
 for(i=0 ; i < n ; i++)
 {
  printf("\n arr[%d] = ",i);
  scanf("%d",&arr[i]);
 }

 sort(arr,n);

 printf("\nThe sorted array is: \n");
 for(i=0;i<n;i++)
 printf("%d\t",arr[i]);
 getch();
 }

void sort(int arr[],int n)
{
 int k,j,temp;
 for(k=1 ; k < n ; k++)
 {
  temp=arr[k];
  j=k-1;
  while((temp < arr[j]) && (j>=0))
  {
   arr[j+1]=arr[j];
   j--;
  }
  arr[j+1]=temp;
 }
}


Output:


Insertion Sort Algorithm And C Code




This article uses material from the Wikipedia article Insertion_sort which is released under the Creative Commons Attribution-Share-Alike License 3.0


Saturday, 16 March 2013

Linear Search In An Array C Code

As I have already described what are Arrays and their basic creation C code, in this post, we'll learn how to perform Linear Search In An Array, its Algorithm And C Code.

In computer sciencelinear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. Linear search is the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for. Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) will be faster, but they also impose additional requirements.

Here is the C Code for Linear Search:


/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>
void main()
{
 int arr[10],num,i,n,found=0,pos=-1;
 clrscr();
 printf("Enter the number of elements in the array: ");
 scanf("%d",&n);

 printf("\nEnter the elements: \n");
 for(i=0;i<n;i++)
 {
  printf("arr[%d] = ",i);
  scanf("%d",&arr[i]);
 }

 printf("\nEnter the number that has to be searched: ");
 scanf("%d",&num);

 for(i=0;i<n;i++)
 {
  if(arr[i]==num)
  {
   found=1;
   pos=i;
   printf("\n %d is found in the array at position = %d",num,i);
   break;
  }
 }
 if(found==0)
 printf("\ %d does not exist in the array",num);
 getch();
}


Output:

Linear Search C Code

Please Comment If You Liked This Post !! 

This article uses material from the Wikipedia article Linear Search which is released under the Creative Commons Attribution-Share-Alike License 3.0