04 June, 2013

C++ Program To Perform Insertion Sort In Ascending Order.

This Post Contains A C++ Program To Perform Insertion Sort In Ascending Order. With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Insertion Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Insertion Sort In Ascending Order.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, dim, val;
int a[20];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter The Value : ";
cin>>val;

/* Source Code For Computing Insertion Sort In Ascending Order */
i=0;
while(i<dim)
{
   j=i-1;
   while(val<a[j] && j>=0)
   {
       a[j+1]=a[j];
       --j;
   }
   a[j+1]=val;
   if(i != dim-1)
   {
      cout<<" \n Enter The Value : ";
      cin>>val;
   }
}
getch();
}
/* End Of Main Program */




Mini Project :~ Sorting Techniques Using Functions #02.

This Post Contains A Mini Project :~ Sorting Techniques Using Functions #02 With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' & 'Merge Sort' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

In The Following Program, We Would Perform Following Sorting Techniques Such As :
1. Quick Sort.
2. Merge Sort.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Program To Perform 'Quick' And 'Merge' Types Of Sorting Techniques Using Functions.

/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  a[100],b[100],l,k,No2=0,j,t,m,i;
int  Scan(int a[100]);
int  QuickS(int a[100],int ,int);
void Disp1();
void Split(int,int);
void MergeS(int ,int ,int);
void Disp2();

/* Start Of Main Program */
void main()
{
    int No1 = 0;
    clrscr();
    printf("\n\t\t :: Welcome To 'Quick' And 'Merge' Types Of Sorting Program ::");
    do                                                                               // Displaying Menu Onto Screen.
    {
        printf("\n\nPlease Select One Of The Following Options ::");
        printf("\n1. Quick Sorting");
        printf("\n2. Merge Sorting");
        printf("\n3. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                l = 0;
                QuickS(a , l , No2);                 // Call To The Function.
                Disp1();
                break;
            case 2: No2 = (Scan(a));
                Split(0,No2-1);
                Disp2();
                break;
            case 3: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 3);
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
QuickS(int a[],int l,int No2)
{
    if(l >= No2)
        return(0);
    k=l;
    i=l;
    j=No2;
    m=a[i];
    do
    {
        do
        {
            i++;
        }
        while(a[i]<=m && i<No2);
        do
        {
            j--;
        }
        while(a[j]>=m && j>l);
        if(i<j)
        {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    while(i<j);

    t=a[j];
    a[j]=m;
    a[k]=t;
    QuickS(a,l,j);
    QuickS(a,j+1,No2);
    return(0);
}
void Disp1()
{
    printf("\nQuick Sorted Array  A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void MergeS(int low,int mid,int high)
{
    int i,h,j,k;
    i=low;
    h=low;
    j=mid+1;
    while(h <= mid && j <= high)
    {
        if(a[h] < a[j])
        {
            b[i] = a[h];
            h++;
        }
        else
        {
            b[i] =  a[j];
            j++;
        }
        i++;
    }
    if( h > mid )
    {
        for(k=j ; k <= high ; k++)
        {
            b[i] = a[k];
            i++;
        }
    }
    else
    {
        for(k=h ; k<= high ; k++)
        {
            b[i] =a[k];
            i++;
        }
    }
    for(k=low ; k <= high ; k++)
        a[k] = b[k];
}

void Split(int low,int high)
{
    int mid;
    if(low<high)
    {
        mid = (low+high)/2;
        Split(low,mid);
        Split(mid+1,high);
        MergeS(low,mid,high);                // Call To The Function.
    }
}
void Disp2()
{
    printf("\nMerge Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
/* End Of Main Program */

 
Output :~
           
Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 8
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5

Quick Sorted Array Is As Follows ::

0  1  2  4  5  6  7  8

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 10
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5  9  3

Merge Sorted Array Is As Follows ::

0  1  2  3  4  5  6  7  8  9

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 3




03 June, 2013

C Program To Perform Character Design : #02

This Post Contains A C Program To Perform Character Design With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Nested For-Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Perform Character Design : #02
/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

/* Declaration Of Variables */
int num=0 ,col=0;

clrscr();


 /* Asking For The Input From User */
 printf(" \n Enter Number Of Rows : ");
 scanf("%d",&num);

 /* Source Code For Character Design */
 for(; num>=1; num--)
 {
        for(col=1; col<=num; col++)
        {     
              printf("@");
        }
        printf("\n");
 }

 getch();

 return 0;

}
/* End Of Main Program */

Output:
@@@@@
@@@@   
@@@
@@
@

C++ Program To Perform Character Design : #02
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

/* Declaration Of Variables */
int num=0 ,col=0;

clrscr();


 /* Asking For The Input From User */
 cout  <<  " \n Enter Number Of Rows : ";
 cin  >>  num;

 /* Source Code For Character Design */
 for(; num>=1; num--)
 {
        for(col=1; col<=num; col++)
        {     
              cout  <<  "@";
        }
        cout  <<  "\n";
 }

 getch();

 return 0;

}
/* End Of Main Program */

Output:
@@@@@
@@@@   
@@@
@@
@




C/C++ Program To Perform Character Design : #01

This Post Contains A C & C++ Program To Perform Character Design With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Nested For-Loops' From C & C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
 
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.

  

C Program To Perform Character Design : #01
/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

 /* Declaration Of Variables */
 int num=0, row=0, col=0;

 /* Asking For The Input From User */
 printf(" \n Enter The Number Of Rows : ");
 scanf("%d",&num);

 /* Source Code For Character Design */
 for(row=1; num>=row; row++)
 {

      for(col=1; col<=num; col++)

            printf("@");

      printf("\n");
 }

 getch();

 return 0;
}

/* End Of Main Program */
Output:
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@

C++ Program To Perform Character Design : #01
/* Declaration Of Header Files */
#include <iostream..h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

 /* Declaration Of Variables */
 int num=0, row=0, col=0;

 /* Asking For The Input From User */
 cout  <<  " \n Enter The Number Of Rows : ";
 cin  >>  num;

 /* Source Code For Character Design */
 for(row=1; num>=row; row++)
 {
      for(col=1; col <= num; col++)
            cout  <<  "@";
      cout  <<  "\n";
 }

 getch();

 return 0;

}
/* End Of Main Program */
Output:
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@




C++ Program To Perform Binary Search.

This Post Contains A C++ Program To Perform Binary Search. With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Binary Search' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Binary Search.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, x, dim, temp;
int low=0, high=0, mid=0;
int a[20];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// For Searching Values, They Should Be Sorted First.
// Sorting Values In Ascending Order.

for(i=0;i<dim;i++)
{
  for(j=i+1;j<dim;j++)
  {
    if(a[i]>a[j])
    {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

/* Source Code For Computing Binary Search */
cout<<" \n Enter Value To Be Searched : ";
cin>>x;
low=0;
high=dim;
while(low<=high)
{
  mid=(low+high)/2;
  if(x<a[mid])
  {
     high=mid-1;
  }
  else
  {
     if(x>a[mid])
     {
        low=mid+1;
     }
     else
     {
        if(x==a[mid])
        {
           cout<<x<<" Is Located At Location "<<mid+1;
           break;
        }
     }
  }
}
getch();
}

Output :

Enter Dimension : 5

Enter 5 Values :
44  22  11  55  33

Sorted Array Is :
11  22  33  44  55

Enter Value To Be Searched : 33
33 Is Located At Location 3




C++ Program To Perform Linear Search.

This Post Contains A C++ Program To Perform Linear Search. With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Linear Search' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Linear Search.
 
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, x, dim, t;
int a[20];
clrscr();

// Accepting Range Of Array From User.
cout<<" \n Enter Dimension For Array : ";
cin>>dim;

// Accepting Value For Array From User.
cout<<" \n Enter The Values For Array A : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// For Searching Values, They Should Be Sorted First.
// Sorting Values In Ascending Order.

for(i=0;i<dim;i++)
{
  for(j=i+1;j<dim;j++)
  {
    if(a[i]>a[j])
    {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

/* Source Code For Computing Linear Search */
cout<<" \n Enter Value To Be Searched : ";
cin>>x;
for(i=0;i<dim;i++)
{
  if(x==a[i])
  {
     cout<<"\n"<<x<<" Is Located At Location "<<i+1;
  }
}
getch();
}
/* End Of Main Program */

 
Output :

Enter Dimension :5

Enter 5 Values For Array 'a' :
4  2  5  1  3

Sorted Array 'a' Is :
1  2  3  4  5

Enter Value To Be Searched : 2
2 Is Located At Location 2.




C++ Program To Perform Bubble Sort In Descending Order.

This Post Contains A C++ Program To Perform Bubble Sort In Descending Order. With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Bubble Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language
In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Bubble Sort In Descending Order.


/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{
int i, j, dim=0, temp=0;
int a[20];
clrscr();

// Accepting Range Of Array From User.
cout<<" \n Enter Dimension : ";
cin>>dim;

// Accepting Values Of Array From User.
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// Printing Values Of Array.
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

// Sorting Values In Descending Order.
for(i=1;i<=dim;i++)
{
  for(j=1; j<=(dim-i); j++)
  {
    if(a[j]<a[j+1])
    {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=1;i<=dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */

 
Output :

Enter Dimension : 5

Enter 5 Values :
44  22  11  55  33

Sorted Array Is :
55  44  33  22  11




C++ Program To Perform Selection Sort In Ascending Order.

This Post Contains A C++ Program To Perform Selection Sort In Ascending Order With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Selection Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Selection Sort In Ascending Order.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, dim=0, temp=0;
int a[20];

clrscr();

// Accepting Range Of Array From User.
cout<<" \n Enter Dimension : ";
cin>>dim;

// Accepting Values Of Array From User.
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// Printing Values Of Array.
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

// Sorting Values In Ascending Order.
for(i=0;i<dim;i++)
{
  for(j=i+1;j<dim;j++)
  {
    if(a[i]>a[j])
    {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */

 
Output :

Enter Dimension : 5

Enter 5 Values :
44  22  11  55  33

Sorted Array Is :
11  22  33  44  55




26 May, 2013

Mini Project :~ Sorting Techniques Using Functions #01.

This Post Contains A Mini Project :~ Sorting Techniques Using Functions. With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Sorting Techniques' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

In The Following Program, We Would Perform Following Sorting Techniques Such As :
1. Bubble Sort.
2. Insertion Sort.
3. Selection Sort.
4. Shell Sort.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.




Program To Perform Different Types Of Sorting Techniques Using Functions.


/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  No2 = 0;
int  Scan(int a[100]);
void BubbleS(int a[100],int);
void InsertionS(int a[100],int);
void SelectionS(int a[100],int);
void ShellS(int a[100],int);

/* Start Of Main Program */
void main()
{
    int No1 = 0;
    int a[100];
    clrscr();
    printf("\n\t\t :: Welcome To 'Sorting' Program ::");    
    do                                                                        // Displaying 'Menu' On Screen.
    {
        printf("\nPlease Select One Of The Following Options ::");
        printf("\n1. Bubble Sorting");
        printf("\n2. Insertion Sorting");
        printf("\n3. Selection Sorting");
        printf("\n4. Shell Sorting");
        printf("\n5. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");     // Accepting User's Choice For Sorting.
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                        // Call To Function 'Bubble' Sort.
                        BubbleS(a , No2);
                        break;
            case 2: No2 = (Scan(a));
                        // Call To Function 'Insertion' Sort.
                        InsertionS(a , No2);
                        break;
            case 3: No2 = (Scan(a));
                        // Call To Function 'Slection' Sort.
                        SelectionS(a , No2);
                        break;
            case 4: No2 = (Scan(a));
                        // Call To Function 'Shell' Sort.
                       ShellS(a , No2);
                       break;
            case 5: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 5);     // End Of 'do...while' Loop.
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
void BubbleS(int a[100], int No2)
{
    int i , Ptr , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr < (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp = a[Ptr];
                a[Ptr] = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nBubble Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void InsertionS(int a[100],int No2)
{
    int i , j , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Temp = a[i];
        for(j=(i-1); j>=0 && Temp<a[j] ; j--)
        {
            a[j+1] = a[j];
        }
        a[j+1] = Temp;
    }
    printf("\nInsertion Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void SelectionS(int a[100],int No2)
{
    int i , j , k , Temp = 0;

    for(i=0;i<(No2-1);i++)
    {
        k = i;
        for(j=(i+1);j<No2;j++)
        {
            if(a[j] < a[k])
            {
                k = j;
            }
        }
        if(k != i)
        {
            Temp = a[i];
            a[i] = a[k];
            a[k] = Temp;
        }
    }
    printf("\nSelection Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void ShellS(int a[100],int No2)
{
    int i, k, Stop, Swap, Limit, Temp;
    int x = (int)(No2/2)-1;
    while(x > 0)
    {
        Stop  = 0;
        Limit = (No2 - x);
        while(Stop == 0)
        {
            Swap = 0;
            for(k=0;k<Limit;k++)
            {
                if(a[k] > a[k+x])
                {
                    Temp   = a[k];
                    a[k]   = a[k+x];
                    a[k+x] = Temp;
                    Swap   = k;
                }
            }
        Limit = (Swap - x);
        if(Swap == 0)
            Stop = 1;
        }
        x=(int)(x/2);
    }
    printf("\nShell Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}

Output :~

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Bubble Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Insertion Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 3

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Selection Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 4

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Shell Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 5




02 May, 2013

Mini Project :~ Searching Techniques Using Functions.

This Post Contains A Mini Project :~ Searching Techniques Using Functions. With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Searching Techniques' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



/* Program To Perform Different Types Of Searching Techniques Using Functions */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  No2 = 0;
int  Scan(int a[100]);
void Lsearch(int a[100],int);
void Bsearch(int a[100],int);
void Fsearch();
int  FibSearch(int a[100],int ,int ,int ,int );                         
int  fib(int );
void main()
{
    int No1 = 0;
    int a[100];
    clrscr();
    printf("\n\t\t :: Welcome To 'Searching & Sorting' Program ::");
    do
    {
        printf("\n\nPlease Select One Of The Following Options ::");
        printf("\n1. Linear Searching");
        printf("\n2. Binary Searching");
        printf("\n3. Fibbonanci Searching");
        printf("\n4. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                    Lsearch(a , No2);
                    break;
            case 2: No2 = (Scan(a));
                    Bsearch(a , No2);
                    break;
            case 3: Fsearch();
                    break;
            case 4: exit (0);
                    break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 4);
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
void Lsearch(int a[100],int No2)
{
    int i , No3 = 0;
    int flag = 0;
    printf("\nPlease Enter The Element Which You Want Search Into The Array A ::\t");
    scanf("%d",&No3);
    for(i=0;i<No2;i++)
    {
        if(No3 == a[i])
        {
            flag = 1;
            break;
        }
        else
        {
            flag = 0;
        }
    }
    if(flag == 1)
    {
        printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :- (%u)",a[i],(i+1),&a[i]);
    }
    else
    {
        printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    }
}
void Bsearch(int a[100],int No2)
{
    int i , Ptr , Temp = 0 , No4 = 0;
    int Lb = 0 , Mid = 0 , Ub = No2;
    int flag = 0;
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr <= (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp = a[Ptr];
                a[Ptr] = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nYour Entered Elements Have Been Sorted Into The Array A ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
    Mid = ((Lb + Ub) / 2);
    printf("\nPlease Enter The Element Which You Want Search Into The Array A ::\t");
    scanf("%d",&No4);
    if(No4 == a[Mid])
    {
        flag = 1;
    }
    else
    {
        if(No4 < a[Mid])
        {
            for(i=Lb;i<Mid;i++)
            {
                if(No4 == a[i])
                {
                    flag = 1;
                    break;
                }
            }
        }
        else
        {
            for(i=(Mid+1);i<Ub;i++)
            {
                if(No4 == a[i])
                {
                    flag = 1;
                    break;
                }
            }
        }
    }
    if(flag == 1)
    {
        printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :- (%u)",a[i],(i+1),&a[i]);
    }
    else
    {
        printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    }
}
void Fsearch()
{
    int No2 = 0 , a[100] , i , k , Ptr , Temp = 0 , key = 0 , p , q , Mid = 0 , Result = 0;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr < (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp     = a[Ptr];
                a[Ptr]   = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nYour Entered Elements Have Been Sorted Into The Array A ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
    printf("\nEnter The Element To Be Searched ::\t ");
    scanf("%d",&key);
    for(k=1 ; fib(k) <= No2 ; k++);
    p   = fib(k-2);
    q   = fib(k-3);
    Mid = No2-p+1;
    Result = FibSearch(a,key,Mid,p,q);
    if(Result == -1)
           printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    else
           printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location",key,Result);
}
int FibSearch(int a[],int key,int Mid ,int p ,int q)
   {
       int Temp;
       if(key==a[Mid])
            return(Mid);
       if(key> a[Mid])
       {
            if(p == 1)
                return(-1);

            Mid = (Mid + q);  // new mid
            p   = (p - q);      //p=fib(k-4)
            q   = (q - p);      //q=fib(k-5)
            return(FibSearch(a,key,Mid,p,q));
      }
      else    //key < a[mid]
      {
            if(q==0)
                return(-1);

            Mid  = (Mid - q);    // new mid
            Temp = (p - q);
            p    = q;         //p=fib(k-3)
            q    = Temp;      //q=fib(k-4)
            return(FibSearch(a,key,Mid,p,q));
      }
}
int fib(int n)
{
    if(n==0)
    {
        return(0);
    }
    if(n==1)
    {
        return(1);
    }
    return(fib(n-1)+fib(n-2));
}

Output :~

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Please Enter The Element Which You Want To Search Into Array A :: 3

The Entered Element (3) Is Found Into Array A At (3) Location With Memory Location :- 62230

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Your Entered Elements Have Been Sorted Into The Array A ::
1
2
3
4
5

Please Enter The Element Which You Want To Search Into Array A :: 5

The Entered Element (5) Is Found Into Array A At (5) Location With Memory Location :- 62234

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 3

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Your Entered Elements Have Been Sorted Into The Array A ::
1
2
3
4
5

Please Enter The Element Which You Want To Search Into Array A :: 3

The Entered Element (3) Is Found Into Array A At (2) Location

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 4




Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive