About Me

My photo
Posting here few basic programs. I prefer using C++. Trying to keep it error free. Feel free to point any of them. Thanks for visiting. You can copy paste code directly from here.

Thursday 17 November 2011

/*PROGRAM TO IMPLEMENT BINARY SEARCH IN 1-DIMENSIONAL ARRAY.
THE UNSORTED ARRAY IS SORTED IS SORTED WHICH IS THE REQUIREMENT OF BINARY SEARCH.



************************************************************/


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

#define size 20


int searchBinary(int arr[], int number, int item)
{

    //SORTING


    int min, low, i, j, temp;
   
    for(i= 0; i < number; ++i)
    {
            min = arr[i]; low =i;
            for(j = i+1;  j < number; ++j)
            {
                  if(min >= arr[j])
                  {
                         low = j;
                         min = arr[j];
                  }
            }
                 
                  temp = arr[i];
                  arr[i] = arr[low];
                  arr[low] = temp;
    }
   
    printf("\n\nSORTED Numbers...\n");
    for(i= 0; i < number; ++i)
    {
           printf("%d ",arr[i]);
    }
                        
   //SORTING DONE              
   
   
    int first=0, end = number-1, mid = int((first+end)/2);
    while(first <= end)
    {
            mid = int((first+end)/2); 
             
            if(arr[mid] == item)
            return(2);
           
            else if(arr[mid] > item)
            end = mid-1;
           
            else
            first = mid+1;
           
    }
    return -1;
}

int main()
{
    int array[size], num, data;
   
    printf("\nHow many elements...?");
    scanf("%d",&num);
   
    if(num>20)
    num = 20;
    for(int i = 0 ; i < num ; ++ i)
    {
            printf("\nElement number %2d : ", i+1);
            scanf("%d",&array[i]);
    }
   
    printf("\n\nEnter element to be searched... :");
    scanf("%d", &data);
   
   
   
    int index;
    index = searchBinary(array, num, data);
   
    if(index == 2)
             printf("\n\nElement found");
    else
        printf("\n\nElement  not found");

       
       
    getch();   
    return 0;       
}
/*Program to implement Linear search in 1-Dimensional array

*****************************************************/

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

#define size 20


int searchLinear(int arr[], int number, int item)
{
    for(int t = 0; t<number; ++t)
    {
            if(arr[t] == item)
            return(t+1);
    }
    return -1;
}

int main()
{
    int array[size], num, data;
  
    printf("\nHow many elements...?");
    scanf("%d",&num);
  
    if(num>20)
    num = 20;
    for(int i = 0 ; i < num ; ++ i)
    {
            printf("\nElement number %d : ", i+1);
            scanf("%d",&array[i]);
    }
  
    printf("\n\nEnter element to be searched... :");
    scanf("%d", &data);
  
    int index;
    index = searchLinear(array, num, data);
  
    if(index == -1)
             printf("\n\nElement not found");
    else
        printf("\n\nElement found  at %d", index);

      
      
    getch();  
    return 0;      
}

Friday 23 September 2011

/*Linked List


  Program to Create Linked List (node implementation) and do common operations in it.
Creation, Traversal, Deletion of nodes

 ****************************************************************************************************************************/



                                                            //preprocessor directives

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

                                                                                   
                                                             //prototype declaration
                                                             //return type is void i.e. they do return any values
    void create();
    void travel();
    void nodeDelete();
       
                                                                                                 //variable declaration
    int nodeNumber, delInfo=0;

    struct node                                                                            //Structure of node defined
    {
        int data;
        node *ptr;
    } *start, *temp, *trvl;



int main( )
{
   
   
    int choice;
    do{
       clrscr();
                                                                                                //Menu
       cout<<"\n\tWelcome to Node Menu...";
       cout<<"\nPress 1 for node creation process..";
       cout<<"\nPress 2 for Traversal...";
       cout<<"\nPress 3 to delete a node...";
       cout<<"\nPress 4 to view value of last deleted node...";
       cout<<"\nPress 5 to exit\n\t";
       cout<<"\nEnter your choice...";
       cin>>choice;

       switch(choice)
       {
        case 1: create();
              break;
        case 2: travel();
              break;
        case 3: nodeDelete();
              break;
        case 4: cout<<"\nLast data part: "<<delInfo;
              break;  
        case 5:
              cout<<"\nPreparing to exit...";
              cout<<"\nPress Enter...";
              getch();
              exit(0);                                 //exit() is defined in process.h, 0 is usually way for exiting
              break;
        default: cout<<"\nWrong choice!!  Remember (1-5)";
              break;


    }
    }while(choice != 4);


    getch();

    return 0;
}



                                                                        //First two are required for list creation
 void create()
 {
    start = new node;                                       //First node


/* actually right after creation of every node, you should/can check whether it is actually created or not. i.e. did the system had sufficient memory to allocate resources

It can be done using a null check;  NULL is a state of a pointer if it doesn't points to some location.
NULL is defined in a lot of header files including our own "stdio.h" which as can be seen is included earlier. Next I'll show how to use NULL.


if (start == NULL)
{
     cout<<"\nInsufficient memory";
}

and maybe you can add exit(0) in the condition to quit the program. Because if the system fails to allocate memory then it's of no use*/

    cout<<"\nNew node created...!";
    cout<<"\nEnter data: ";
    cin>>start->data;

    temp = new node;                                      //Second node
    cout<<"\nSecond node....";
    cout<<"\nEnter data: ";
    cin>>temp->data;

    start->ptr = temp;


    cout<<"\nHow many more nodes you want...?";
    cin>>nodeNumber;

    int i=0;
    for(i=0; i<(nodeNumber); ++i)     //Further nodes created
    {
        trvl = new node;
        cout<<"\nEnter data in node number "<<i+3<<": ";
        cin>>trvl->data;
        temp->ptr = trvl;
        temp = temp->ptr;
    }


    cout<<"\nNodes created...";
   
   
  }




void travel()
{
    cout<<"\nTraversing";                                               //Traversal
    cout<<"\n    Node number \t   Data";
    trvl = start;

    for(int i=0; i<(nodeNumber+2); ++i)
    {
        cout<<endl<<"\t"<<i+1<<"\t\t"<<trvl->data;
        trvl = trvl->ptr;
    }
}






void nodeDelete()
{
     int del;
    // clrscr();
     travel();
     cout<<"\nWhich node you want to delete...?";
     cin>>del;
     if( del > nodeNumber+2)
     cout<<"\nSuch node doesn't exists..., try again";

     else
     {
       temp = trvl = start;



       for(int i=0; i<del ; ++i)
           trvl = trvl-> ptr;

           for( int j=0; j< (del-1) ; ++j)
           temp = temp-> ptr;

       temp->ptr = trvl->ptr;
       delInfo= trvl->data;
       nodeNumber--;
    }

    cout<<"\nNode deleted...";
    cout<<"\nYou can view data of last deleted node";
}
/*ASSIGNMENT - 2 DISCRETE STRUCTURES

Sorting


  program for sorting in C using M A and S functions

  M is used to find maximum amongst a list of numbers
  A is used to find greater of 2 numbers and put the bigger number to the Right Hand Side in the list
  S is the sorting function which is used recursively
********************************************************************************************************************/

                                           
                              

                                                                             /* pre-processor directives */ 
#include<stdio.h>
#include<conio.h>

#define size 50                                                 /* helps in changing array size */




int array[size], numberOfValues = 0, temp = 0, n=0, j=0;
                                                               /* variable declaration , these are defined Globally*/

void a()
{
             if(array[j] > array[j+1])               /* here swapping the smaller with bigger number and moving it right*/
             {
                          temp = array[j];
                          array[j] = array [j+1];
                          array[j+1] = temp;
             }
}


void m()                                                      /* m() calls a() */
{
     for(j = 0; j< (n-1); ++j)
     a();
}



void s()                                                             /* and here s calls m() */
{
    if(n != 0)
    {
       
        m();
        n--;
        s();                                                          /* see here s() is called recursively i.e. s() calls s()*/
    }
}




int main()
{
   
    clrscr();
    printf("\n\tNumber of elements...: ");
    scanf("%d", &numberOfValues);
   
    int i;                                                                        
                                                                                                       /* inputs */
   
    for(i = 0; i < numberOfValues; ++i)
    {
            printf("Element no. %3d : ", i+1);
            scanf("%d", &array[i]);
    }
   
    n = numberOfValues;
    s();
   
   
    printf{"\nOUTPUT");
    for(i = 0; i < numberOfValues; ++i)                                              /*output*/
    {
            printf("%d  ", array[i]);
    }
   
   
    getch();
   
    return 0;
}








Sample Output: