Friday 13 December 2013

ORACLE - SQL Query - 2

Query to display 1st highest,2nd highest salary earners.

This is the table i am using here.

Query to display 1st highest earners

Query to display 2nd highest earners

Sunday 6 October 2013

custom strcpy function in c to copy a string

#include<stdio.h>
#include<conio.h>
void mystrcpy(char *dest,const char*src)
{
while(*src)
{
 *dest=*src;
 src++;
 dest++;
}
*dest='\0';
}
int main()
{
  char str1[10]="Welcome";
  char str2[10];
  clrscr();
  printf("\nEnter a string: ");
  gets(str2);
  puts(str1);
  puts(str2);
  //stcpy(str1,str2); -- way to call library function
  mystrcpy(str1,str2);
  puts(str1);
  puts(str2);
  return 0;
}

custom strcmp function in c to compare two strings

#include<stdio.h>
#include<string.h>
#include<conio.h>
int mystrcmp(const char*s1,const char*s2)
{
int r=0;
while(*s1!='\0'||*s2!='\0')
{
 if(*s1!=*s2)
 {
 r=(int)(*s1-*s2);
 return r;
 }
 s1++;
 s2++;
}
return r;
}

int main()
{
  char s1[10]="Hello";
  char s2[10]="HeLlo";
  int d;
  clrscr();
  puts(s1);
  puts(s2);
  //d=strcmp(s1,s2);
  d=mystrcmp(s1,s2);
  printf("\nDiff is:%d",d);
  getch();
  return 0;
}

C program to find length of the string using custom strlen function


#include<stdio.h>
#include<conio.h>
int main()
{
  char src[10];
  int l;
  int mystrlen(const char*);
  clrscr();
  printf("\nEnter a string: ");
  gets(src);
  //l=strlen(src);
  l=mystrlen(src);
  printf("\nString lenght:%d",l);
  getch();
  return 0;
}
int mystrlen(const char*src)
{
int c=0;
while(*src!='\0')
{
++c;
src++;
}
return c;
}

C program to print fibonacci series upto a given number

#include<stdio.h>
#include<conio.h>
int main()
{
    int m=0,n=1,temp,a;
    printf("enter the value upto which you want to print fibonacci numbers\n");
    scanf("%d",&a);
    printf("%d\n%d\n",m,n);
    while(n<a)
    {
               temp=m;
               m=n;
               n=temp+n;
               while(n<a)
               {
               printf("%d\n",n);
               break;
               }
    }
    _getch();
    return 0;
}
              
    
    
    

C program to find gcd of N inputs

#include<stdio.h>
#include<conio.h>
#define size 100 
int Gcd(int *);
int n=0;
int main()
{
    int x[size],z=0;
    printf("\n for how many values do u want to find gcd????::::");
    scanf("%d",&n);
    printf("\n enter %d values to find gcd ...",n);
    while(z<n)
    {
            scanf("%d",&x[z]);
            z++;
    }
    x[n]=Gcd(x);
    if(x[n]==-1)
    {
                printf("\n gcd doesn't exist....");
    }
    else if(x[n]==-2)
    {
                
    }
    else
    {
                printf("gcd of given numbers is : %d",x[n]);
    }
    _getch();
    return 0;
}
int Gcd(int *ptr)
{
   int a[size],c,d,i=0,j=1,k=0,z=0;
   while(k<n)
   {
                   a[k]=*ptr;
                   ptr++;
                   k++;
   }
    while(z<n)
    {
              if(a[z]==0)
              {
                   printf("\n gcd of the given numbers is : 0");
                   goto a;
              }
              z++;
    }          
   while(i<n)
   {
           while(j<n)
           {
              if(a[i]>a[j])
              {
                           c=a[i];
                           a[i]=a[j];
                           a[j]=c;
              }
              j++;
           }
           i++;
   }
   c=a[0];
   d=n-1;     
   while(c>1)
   {
             while(a[d]%c==0)
             {
                             if(d>1)
                             {
                                    d--;
                                    continue;
                             }
                             else 
                             return c;
             }
             c--;
             if(c==1)
             return -1;
   }
   a:
         return -2;
}

C program to reverse given number

//reversing a number 
#include<stdio.h>
#include<conio.h>
int main()
{
    int sum=0,n;
    printf("enter a number to reverse : ");
    scanf("%d",&n);
    while(n)
    {
            sum=sum*10+n%10;
            n=n/10;
    }
    printf("%d",sum);
_getch();
return 0;
}


i/p : 1234
o/p : 4321

                  
    
    

C program to swap two integer numbers without using third variable

#include<stdio.h>
#include<conio.h>
int main()
{
    float a,b;
    printf("enter the values to swap \n");
    scanf("%f%f",&a,&b);
    printf("before swapping a:%f b:%f\n",a,b);
    /*a=a+b;
    b=a-b;
    a=a-b;*/
    a=a*b;
    b=a/b;
    a=a/b;
    printf("After swapping a:%f b:%f\n",a,b);
    _getch();
    return 0;
}

C++ code to implement stack using arrays

#include<iostream>
using namespace std;
#include<conio.h>
#include<process.h>
template<class T>
class Stack
{
      private:
              int top,maxtop;
              T *stack;
      public:
             Stack(int maxstacksize)
             {
                       top=-1;
                       maxtop=maxstacksize;
                       stack=new T[maxtop];
             }
             ~Stack()
             {
                     delete[] stack;
             }
             int isfull();
             int isempty();
             void insertion();
             void deletion();
             void display();
             void topmost();
};
template<class T>
int Stack<T>::isempty()
{
    if(top==-1)
    return 1;
    else
    return 0;
}
template<class T>
int Stack<T>::isfull()
{
    if(top==maxtop-1)
    return 1;
    else
    return 0;
}
template<class T>
void Stack<T>::topmost()
{
     if(isempty())
     cout<<"\nstack is underflow.......";
     else
     cout<<"\n topmost element of the stack is:"<<stack[top];
}
template<class T>
void Stack<T>::display()
{
     if(isempty())
     cout<<"\n stack is underflow..........";
     else
     cout<<"\nstack elements are :";
                    for(int i=0;i<=top;i++)
                    {
                            cout<<"\t";
                            cout<<stack[i];
                    }
}
template<class T>
void Stack<T>::insertion()
{
     T ele;
     if(isfull())
     cout<<"\n stack is overflow.........";
     else
     {
     cout<<"\n enter the element u want to insert :";
     cin>>ele;
     stack[++top]=ele;
     }
}
template<class T>
void Stack<T>::deletion()
{
     if(isempty())
     cout<<"\n stack is underflow..........";
     else
     cout<<"\n deleted element is:"<<stack[top--];
}
int main()
{
    int choice;
    Stack<int> s(10);
    do
    {
            cout<<"\n1.insertion\n2.deletion\n3.topmost\n4.display\n5.exit\n";
            cout<<"\n enter ur choice :";
            cin>>choice;
            switch(choice)
            {
                          case 1:
                               s.insertion();
                               break;
                          case 2:
                               s.deletion();
                               break;
                          case 3:
                               s.topmost();
                               break;
                          case 4:
                               s.display();
                               break;
                          case 5:
                               _exit(0);
            }
    }while(choice<=5);
    _getch();
    return 0;
}
     
     
                    
               

C++ code to implement stack using linked list

/*top was compared to null instead of initializing to null*/
#include<iostream>
using namespace std;

template<class T>
class node
{
      public:
              T data;
              node *link;
};
template<class T>
class stack
{
       node<T> *top;
      public:
             stack();
             ~stack();
             int isempty();
             void push(T x);
             T pop();
             T topmost();
             void display();
};
template<class T>
stack<T>::stack()
{
                 top=NULL;/*initially was top==null*/
}
template<class T>
int stack<T>::isempty()
{
    if(top==NULL)
    return 1;
    else
    return 0;
}
template<class T>
void stack<T>::push(T x)
{
     node<T> *temp;
     temp=new node<T>;
     temp->data=x;
     temp->link=top;
     top=temp;
}
template<class T>
T stack<T>::pop()
{
                 node<T> *temp;
                 T x;
                 if(isempty())
                 return -1;
                 else
                 {
                     temp=top;
                     top=top->link;
                     x=temp->data;
                     delete temp;
                     return x;
                 }
}
template<class T>
void stack<T>::display()
{
     node<T> *temp;
     if(isempty())
     {
                  cout<<"\n stack is empty ";
     }
     else
     {
         cout<<"\nthe contents are :";
         temp=top;
         while(temp!=NULL)
         {
                          cout<<temp->data;
                          temp=temp->link;
         }
     }
}
template<class T>
T stack<T>::topmost()
{
                     if(isempty())
                     return -1;
                     else
                     return(top->data);
}
template<class T>
stack<T>::~stack()
{
                  node<T> *temp=NULL;
                  while(top!=NULL)
                  {
                                  temp=top;
                                  top=top->link;
                                  delete temp;
                  }
}
int main()
{
    int ch,x;

    stack<int> a;
    do
    {
              cout<<"\n 1.insertion \n 2.deletion \n 3.display \n 4.topmost element \n 5.exit \n";
              cout<<"\n enter ur choice :";
              cin>>ch;
              switch(ch)
              {
                        case 1:
                             cout<<"enter data element :\n";
                             cin>>x;
                             a.push(x);
                             break;
                        case 2:
                             x=a.pop();
                             if(x==-1)
                             cout<<"\n stack is empty....";
                             else
                             cout<<"\n the deleted value is :"<<x<<"\n";
                             break;
                        case 3:
                             a.display();
                             break;
                        case 4:
                             x=a.topmost();
                             if(x==-1)
                             cout<<"\n stack is empty..";
                             else
                             cout<<"\n the topmost value is :"<<x<<"\n";
                             break;
                        case 5:
                             break;
              }
    }while(ch<5);
    return 0;

}

C++ code to implement single linked list

#include<iostream>

using namespace std;

#include<conio.h>

template<class T>

class node

{

     public:

              T data;

              node<T> *link;

};

template<class T>

class list

{

      private:

              node<T> *first;

      public:

      list();

      ~list();

      void createlist();

      void deletion();

      void insertion();

      void display();
      
      };

template<class T>

list<T>::list()

{

               first=NULL;

}

template<class T>

list<T>::~list()

{

                node<T> *next;

                while(first)

                {

                            next=first->link;

                            delete first;

                            first=next;

                }

}

template<class T>

void list<T>::createlist()

{

                     T a;
                     
                     node<T> *cur,*ptr;

                     cout<<"\nenter data for new node:";

                     cin>>a;

                     while(a)

                     {

                             cur=new node<T>;

                             cur->data=a;

                             cur->link=NULL;

                             if(first==NULL)

                             first=cur;

                             else

                             ptr->link=cur;

                             ptr=cur;

                             cout<<"\nenter data for new node :";

                             cin>>a;

                     }

}

template<class T>

void list<T>::insertion()

{
                node<T> *cur,*ptr;

                 T ele;
                   
                 char ch; 
          
                 ptr=first;

                 cur=new node<T>;

                 cout<<"\n enter data for new node :";

                 cin>>cur->data;

                 cur->link=NULL;

                 cout<<"\n do u want to insert first [y/n]:";

                 cin>>ch;

                 if(ch=='Y'||ch=='y')

                 {

                                     cur->link=first;

                                     first=cur;

                 }

                 else

                 {

                     cout<<"\n after which element do u want to insert :";

                     cin>>ele;

                     while(ptr!=NULL)

                     {

                                     if(ptr->data==ele)

                                     {

                                                      cur->link=ptr->link;

                                                      ptr->link=cur;

                                                      break;

                                     }

                                     else

                                     {

                                         ptr=ptr->link;

                                     }

                     }

                 }

}

template<class T>

void list<T>::deletion()

{

                 T ele;

                 char ch;

                 node<T> *ptr,*ptr1;

                 if(first==NULL)

                 {

                                cout<<"\nlist is empty .....";

                 }

                 else
                 
                 {

                     ptr=first;

                     cout<<"\ndo u want to delete first element [y/n]:";

                     cin>>ch;

                     if(ch=='y'||ch=='Y')

                     {

                                         first=first->link;

                                         delete ptr;

                     }

                     else

                     {

                         cout<<"\nwhich element do u want to delete :";

                         cin>>ele;

                         while(ptr!=NULL)

                         {

                                         if(ptr->link->data==ele)

                                         {

                                                                 ptr1=ptr->link;

                                                                 ptr->link=ptr1->link;

                                                                 delete ptr1;
                                                                 
                                                                 return;

                                         }
                                        
                                        else

                                         {

                                             ptr=ptr->link;

                                         }

                         }

                     }

                 }

}

template<class T>

void list<T>::display()

{

     node<T> *ptr;

     if(first==NULL)

     {

                    cout<<"\n list is empty..";

     }

     else

     {

         ptr=first;

         while(ptr!=NULL)

         {
                         
                         cout<<ptr->data<<"   ";

                         ptr=ptr->link;

         }

     }

}

int main()

{

    int n;

    list <int> l;

    l.createlist();

    do

    {

                   cout<<"\n 1.insertion \n2.deletion \n3.display \n4.exit \n";

                   cout<<"\n enter ur option : ";

                   cin>>n;

                   switch(n)

                   {

                            case 1: l.insertion();

                            break;

                            case 2: l.deletion();

                            break;

                            case 3: l.display();

                            break;

                            case 4:
                                 exit(0);

                            break;

                   }

    }while(n<=4);
    
    _getch();

    return 0;

}

Selection Sort code in C++

//selection sorting
#include<iostream.h>
using namespace std;
#include<conio.h>
template <class T>
void selectionsort(T a[],int n)
{
     int i,j,temp;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i])
                                       {
                                                    temp=a[i];
                                                    a[i]=a[j];
                                                    a[j]=temp;
                                       }
                     }
     }
}
int main()
{
    int a[50],i,n;
    cout<<"enter number of elements do u want to sort :";
    cin>>n;
    cout<<"enter elements to sort:";
    for(i=0;i<n;i++)
    {
                    cin>>a[i];
    }
    selectionsort(a,n);
    cout<<"after sorting the elements are....\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\n";
    }
    _getch();
    return 0;
}