Tuesday, July 10, 2018

Program 18


         
ARRAY IMPLEMENTATION OF QUEUE


Write a program to perform insertion and deletion in an array Queue

/* Program to create a Linear Queue using an array to perform insertion, deletion and display operations*/
#include < iostream . h >
#include  < process . h >
#include < conio . h >

# define MAX 10
class Queue
 {
          int queue[ MAX ] ;
          int front, rear ;
     public :
          Queue ( )
           {
                   front = rear = -1 ;  //denotes Q is empty
           }
          void add_to_Q ( )
           {
                   if ( rear == MAX - 1 )
                     {
                             cout<<"Queue is Full  !! " ;
                     }
                   else
                     {
                             int n ;
                             cout << "\nEnter the no to be added:: " ;
                             cin  >> n ;
                             queue [ ++rear ] = n ;
                             if( front == -1 )
                               front = rear;

                             cout<<"\nAdded to the Queue !! " ;
                     }
           }
          void del_from_Q ( )
           {
                   if ( front == -1 )
                    {
                             cout<< " Queue is empty !! Cannot delete !! " ;
                    }
                   else
                    {
                             cout<< "Element deleted :: " << queue [ front ++ ] ;
                    }
           }
          void display_Q ( )
           {
                   if ( front == -1 )
                             cout << "Queue is empty !! ";
                   else
                    {
                             cout<< "\nQueue is ::\t";
                             for ( int i = front ; i <=rear ; ++ i)
                                      cout << queue [ i ] << "\t" ;
                    }
           }
 } Q ;

void main ( )
 {
          int ch ;
          clrscr( ) ;
          while ( 1 )
           {
                   cout << "\n1. Add to Queue ";
                   cout << "\n2. Delete from Queue" ;
                   cout << "\n3. Display Queue" ;
                   cout << "\n4. Exit" ;
                   cout << "\nEnter your choice :: " ;
                   cin  >> ch ;

                   if ( ch == 1 )
                             Q.add_to_Q ( ) ;
                   if ( ch == 2 )
                             Q.del_from_Q ( ) ;
                   if ( ch == 3 )
                             Q. display_Q ( ) ;
                   if ( ch == 4 )
                             exit ( 0 ) ;
                   getch ( ) ;
           }
 }


Program 17


/* Transpose of matrix                Write a programe to read a matrix and display its transpose.   */ 

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
 int a[10][10],b[10][10],size;
 cout<<"\n Enter the size of matrix : ";
 cin>>size;
 cout<<"\n Enter the Elements of Matrix Row wise. \n";
 for(int i=0;i<size;i++)
  for(int j=0;j<size;j++)
   cin>>a[i][j];
 cout<<"\n The Entered Matrix is :\n";
 for(i=0;i<size;i++)
 {
  cout<<"\n";
  for(j=0;j<size;j++)
  {
   cout<<a[i][j]<<"  ";
   b[i][j]=a[j][i];
  }
 }
 cout<<"\n The Transpose of Matrix is : \n";
 for(i=0;i<size;i++)
 {
  cout<<"\n";
  for(j=0;j<size;j++)
  {
   cout<<b[i][j]<<"  ";
  }
 }
 getch();
}

Program 16


INSERTION IN 1-D ARRAY
Write a C++ program to insert an element into a given array in an appropriate position.

#include<iostream.h>
#include<process.h>

int findpos(int [],int,int);
void main()
{
 int ar[50],item,n,index;
 cout<<"How many elements do u want to create array with ?(max.50)...";
 cin>>n;
 cout<<"\nEnter array elements(must be sorted in ASC order)\n";
 for(int i=0;i<n;i++)
   cin>>ar[i];
 char ch = 'y';
 while(ch=='y'||ch=='Y')
 {
   cout<<"\nEnter element to be inserted..";
   cin>>item;
   if(n==50)
  {
    cout<<"Overflow!!\n";
    exit(1);
  }
  index=findpos(ar,n,item);
  for(i=n;i>index;i--)
 {
   ar[i] = ar[i-1];
 }
 ar[index]=item;
 n +=1;
 cout<<"\n Want to insert more elements ?(y/n)..";
 cin>>ch;
}
cout<<"The array now is as shown below..\n";
for(i=0;i<n;i++)
 cout<<ar[i]<<" ";
cout<<endl;
}
int findpos(int ar[], int size, int item)
{
  int pos;
  if(item < ar[0])
     pos = 0;
  else
  {
   for(int i=0; i<size-1; i++)
   {
     if(ar[i] <=item && item < ar[i+1])
     {
       pos = i+1;
       break;
    }
  }
  if(i == size-1)
    pos = size;
 }
 return pos;
}

Program 15


MERGE SORT
Write a C++ program to accept two single dimensional arrays, A in ascending order and B in descending order and Merge them into array C in ascending order.

#include<iostream.h>
void merge(int[],int,int[],int,int[]);
void main()
{
int A[50],B[50],C[50],MN=0,M,N;
cout<<"How many elements do u want to create first array with ?";
cin>>M;
cout<<"\nEnter first array elements [ascending]..\n";
for(int i=0;i<M;i++)
cin>>A[i];
cout<<"\nHow many elements you want to create second array with?";
cin>>N;
MN=M+N;
cout<<"\nEnter second array elements[decending]";
for(i=0;i<N;i++)
cin>>B[i];
merge(A,M,B,N,C);
cout<<"\n\nThe merged array is as shown below..\n";
for(i=0;i<MN;i++)
cout<<C[i];
cout<<endl;
}

void merge(int A[],int M,int B[],int N,int C[])
{
int a,b,c;
for(a=0,b=N-1,c=0;a<M && b>=0;)
{
if(A[a] <= B[b])
C[c++] = A[a++];
else
C[c++] = B[b--];
}
if(a<M)
{
while(a<M)
C[c++] = A[a++];
}
else
{
while(b>=0)
C[c++] = B[b--];
}
}

Program 14


INSERTION SORT
Write a C++ program to accept a single dimensional array and sort given array elements by Insertion Sort.

#include<iostream.h>
#include<conio.h>
#include<limits.h>

void inssort(int[],int);

void main()
{
 clrscr();
 int a[50],item,n,index;
 cout<<"Enter array size ";
 cin>>n;
 cout<<"\nEnter array elements :";
 for(int i=0;i<=n;i++)
cin>>a[i];
 inssort(a,n);
 cout<<"\n\nThe sorted array is as shown below \n";
 for(i=1;i<=n;i++)
cout<<a[i]<<" ";
 cout<<endl;
 getch();
}

void inssort(int a[],int size)
{
 int j,tmp;
 a[0]=INT_MIN;
 for(int i=1;i<=size;i++)
 {
  tmp=a[i];
  j=i-1;
  while(tmp<a[j])
  {
   a[j+1]=a[j];
   j--;
  }
  a[j+1]=tmp;
  cout<<"\nArray after pass - "<<i<<" - is:";
  for(int k=1;k<=size;k++)
         cout<<a[k]<<" ";
  cout<<endl;
 }
}

Program 13


BUBBLE SORT
Write a C++ program to accept a single dimensional array and sort given array elements by Bubble Sort.
                                               
#include<iostream.h>
#include<conio.h>
void bubblesort(int[],int);
void main()
{
clrscr();
int a[50],item,n,index;
cout<<"Enter array size ";
cin>>n;
cout<<"\nEnter array elements :";
for(int i=0;i<n;i++)
cin>>a[i];
bubblesort(a,n);
cout<<"\n\nThe sorted array is as shown below \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
getch();
}

void bubblesort(int a[],int size)
{
int tmp,ctr=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
cout<<"\nArray after iteration - "<<++ctr<<" - is:";
for(int k=0;k<size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}

Program 12


SELECTION SORT
Write a C++ program to accept a single dimensional array and sort given array elements by Selection Sort.

#include<iostream.h>
#include<conio.h>

void selsort(int[],int);
void main()
{
clrscr();
int a[50],item,n,index;
cout<<"Enter array size ";
cin>>n;
cout<<"\nEnter array elements :";
for(int i=0;i<n;i++)
cin>>a[i];
selsort(a,n);
cout<<"\n\nThe sorted array is as shown below \n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
getch();
}

void selsort(int a[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{
small=a[i];
for(int j=i+1;j<size;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
cout<<"\nArray after pass - "<<i+1<<" - is:";
for(j=0;j<size;j++)
cout<<a[j]<<" ";
}
}