WELCOME TO GTU MATERIAL

GTU MATERIAL PROVIDE YOU ALL TYPE OF EDUCATION MATERIAL | DOWNLOAD FREE MATERIAL | ALL TYPE OF EDUCATION SOFTWARES | EXAM ALERTS | EBOOKS | EXAM PAPERS | TIME TABLE | ALL TYPE OF SYLLABUS | MBA | MCA | ENGINEERING | BE | FREE MATERIAL PROVEDE

SHARE YOUR MATERIAL ALSO.. PLEASE SEND ME YOUR MATERIAL WHO SHARE WITH PEOPLE WE PUBLISH IN GTU MATERIAL WITH YOU NAME.. PLEASE SEND US YOUR NAME, COLLEGE NAME, AND STREAM SO THAT WE CAN PUBLISH WITH YOUR NAME..

THANKS YOU SO MUCH.....

Showing posts with label GTU MCA MATERIAL. Show all posts
Showing posts with label GTU MCA MATERIAL. Show all posts

Wednesday, 17 August 2011

GTU MCA MATERIAL | C LANGUAGE | Preprocessor


Preprocessor:
vIt is a program that processes source program before it is passed to the complier.
vPreprocessor commands often known as directives.
vPreprocessor directives begin with a # symbol.
vThe directives can be placed anywhere in a program but generally it is beginning of a
     Program before main () or particular function.
vThese directives can be divided into 3 categories.
    1) Mecro substitution directive
    2) File inclusion directive
    3) Complier control directive
1) Macro substitution directive
   vMacro substitution is a process where an identifier in a program is replaced by a    
         predefined string composed of one or more token.
  vExample:
         #define a 25
Main ()
           {
                        Int i;
                        For (i=1;i<=a;i++)
                        {
                                    Printf(“%d”,i);
                        }
                        getch();
            }
   vThis # define a 25 statement is called “macro definition” or just a
       “macro”.
   va is often called “macro templates” and 5 is their “macro 
       expansion”.                             
  vWhen we compile the program it is check by the preprocessor for
       any macro definition before the source code passes to the    
        complier.
   vWe can use capital letter fot macro template this makes it easy for programmer to pick
       out all the macro template when reading through the program.
   vMacro template and its macro expansion are sepatated by blanks or tabs.
   vRemember that a macro definition is never to be terminated by a semicolon.
   vIt is not necessary that you can declare macro before the main function you can declare  
        anywhere in the program.
For Example:
main()
{
            #define pf printf
            pf(“Jay Swaminarayan”);
            getch();             
}

v #define directive is many a times used to define operators.
    #define AND &&
    #define    OR    ||

  2) File inclusion directive

v An external file containing functions or macro definitions can be included as a part of a program so that we need not rewrite those functions or macro definitions. This is achieved by the preprocessor directive.
For example:  #include “filename”

Where filename is the name of the file containing the required definitions or functions. At this point, the preprocessor inserts the entire contents of filename into the source code of the program. When the filename is included within the double quotation marks, the search for the file is made first in the current directory and then in the standard directories.
For example:-
                                                #include<filename>
Without double quotation marks. In this case, the file is searched only in the standard directories.
        
            Nesting of included files is allowed. That is, an include file can included file can include other files. However, a file cannot include itself.
             If an included file is not found, an error is reported and compilation is terminated.
             We can make use of a definition of function contained in any of these files by including them in the program as shown below:
                                                #include<stdio.h>
                                                #include<conio.h>

3) COMPILER CONTORL DIRECTIVES: -


1)      You have included a file containing some macro definitions. It is not known whether a particular macro (say, test) has been defined in that header file. However, you want to be certain that test is define (or not defined).
2)      Suppose a customer has two different type of computer and you are required to write a program that will run on both the system.
                                    One solution to these problems is to develop different programs to suit the needs of different situations. Another method is develop a single. Comprehensive program that includes all optional codes and then directs the compiler to skip over certain parts of source code when they are not required. Fortunately, the c preprocessor offers a feature known as conditional compilation. Which can be used to ‘switch’ on or off a particular line or group of lines in a program.

Situation 1

                     This situation refers to the conditional definition of a macro. We want ensure that the macro TEST is always defined. irrespective of whether it has been defined in the header file or not. This can be achieved as follows:
                                    #include           “DEFINE.H”
                                    #ifndef                       TEST
                                    #define                        TEST   1
                                    #endif
                                    ….
                                    ….
                     DEFINE.H is the header file that is supposed to contain the definition of TEST macro. The directive.
                                    #ifndef            TEST
                     Searches for the definition of TEST in the header file and if not defined, then all the lines between the #ifndef and the corresponding #endif directive are left ‘active’ in the program.
That is, the preprocessor directive
                                    #define     TEST
is processed.

Friday, 18 February 2011

GTU C PROGRAMS | HEAP SORT PROGRAM

C:
-------------------------------------------------------------------------
/* HEAP SORT */
/* HEAP.C */
# include<stdio.h>
void  heap_sort(int *, int );
void create_heap(int *, int);
void display(int *, int);
/*  Definition of the function */
void create_heap(int list[], int n )
{
 int k, j, i, temp;
 for(k = 2 ; k <= n;  ++k)
 {
  i = k ;
  temp = list[k];
  j = i / 2 ;
  while((i > 1) && (temp > list[j]))
  {
   list[i] = list[j];
   i = j ;
   j = i / 2 ;
   if ( j < 1 )
    j = 1 ;
  }
  list[i] = temp ;
 }
}
/* End of heap creation function */
/* Definition of the function */
void heap_sort(int list[], int n)
{
 int k, temp, value, j, i, p;
 int step = 1;
 for(k = n ; k >= 2; --k)
 {
  temp = list[1] ;
  list[1] = list[k];
  list[k] = temp ;
  i = 1 ;
  value = list[1];
  j = 2 ;
  if((j+1) < k)
   if(list[j+1] > list[j])
    j ++;
  while((j <= ( k-1)) && (list[j] > value))
  {
   list[i] = list[j];
   i = j ;
   j = 2*i ;
   if((j+1) < k)
    if(list[j+1] > list[j])
     j++;
    else
     if( j > n)
      j = n ;
   list[i] = value;
  } /* end of while statement */
  printf("\n Step = %d ", step);
  step++;
  for(p = 1; p <= n; p++)
   printf(" %d", list[p]);
 } /* end for loop */
}
/* Display function */
void display(int list[], int n)
{
 int i;
 for(i = 1 ; i <= n; ++ i)
 {
  printf("  %d", list[i]);
 }
}
/* Function main */
void main()
{
 int list[]={ 0,10,23,64,21,74,95,2,59,44,87,55};
 int i, size = 11 ;
 clrscr();
/* printf("\n Size of the list: %d", size);
 for(i = 1 ; i <= size ; ++i)
 {
  list[i] = rand() % 100;
 }*/
 printf("\n Entered list is as follows:\n");
 display(list, size);
 create_heap(list, size);
 printf("\n Heap\n");
 display(list, size);
 printf("\n\n");
 heap_sort(list,size);
 printf("\n\n Sorted list is as follows :\n\n");
 display(list,size);
 getch();
}

--------------------------------------------------------------------------
C++ :
--------------------------------------------------------------------------
  // HEAP SORT
  // HEAP.CPP
  # include<iostream.h>
  class heap_s
    {
 private:
 public:
    void  heap_sort(int *, int );
    void create_heap(int *, int);
    void display(int *, int);
    };

 //  definition of the function
  void heap_s :: create_heap(int list[], int n )
  {
    for( int k = 2 ; k <= n;  ++k)
       {
  int i = k ;
  int temp = list[k];
  int j = i / 2 ;
  while((i > 1) && (temp > list[j]))
     {
      list[i] = list[j];
      i = j ;
      j = i / 2 ;
      if ( j < 1 )
        j = 1 ;
     }
     list[i] = temp ;
   }
       }
// end of heap creation function
// definition of the function
 void  heap_s :: heap_sort(int list[], int n)
 {
    for( int k = n ; k >= 2; --k)
      {
  int temp = list[1] ;
  list[1] = list[k];
  list[k] = temp ;
  int i = 1 ;
  int value = list[1];
  int j = 2 ;
  if((j+1) < k)
     if(list[j+1] > list[j])
        j ++;
         while((j <= ( k-1)) && (list[j] > value))
   {
    list[i] = list[j];
    i = j ;
    j = 2*i ;
     if((j+1) < k)
       if(list[j+1] > list[j])
         j++;
         else
         if( j > n)
         j = n ;
         list[i] = value;
         } // end of while statement
       cout<<"\n";
       for(int p=1; p<=n; p++)
       cout<<"  "<<list[p];
    } //end for loop
        }
  void heap_s :: display(int list[], int n)
      {
 for( int i = 1 ; i <= n; ++ i)
     {
       cout<<"  "<<list[i];
     }
       }
    void main()
  {
    heap_s sort;
    int list[100];
    int size ;
    cout<<"\n Input the size of the list :";
    cin>>size;

    for(int i = 1 ; i <= size ; ++i)
     {
       cout<<"\n Input values for :" <<i<< " : ";
       cin>>list[i];
     }
     cout<<"\n Entered list is as follows:\n";
     sort.display(list, size);
     sort.create_heap(list, size);
     cout<<"\n Heap\n";
     sort.display(list, size);
     sort.heap_sort(list,size);
     cout<<"\n Sorted list is as follows :\n";
     sort.display(list,size);
  }



GTU C PROGRAMS | SHELL SORT PROGRAM

C :
-------------------------------------------------------------------------
/* shell.c */
/* shell sort */
#include <stdio.h>
#include <stdlib.h>
void shell_sort(int array[], int size)
{
 int temp, gap, i, exchange_occurred;
 gap = size / 2;
 do {
  do {
   exchange_occurred = 0;
   for (i = 0; i < size - gap; i++)
    if (array[i] > array[i + gap])
    {
     temp = array[i];
     array[i] = array[i + gap];
     array[i + gap] = temp;
     exchange_occurred = 1;
    }
  } while (exchange_occurred);
 } while (gap == gap / 2);
}
void main(void)
{
 int values[50], i;
 printf("\n Unsorted list is as follows \n");
 for (i = 0; i < 50; i++)
 {
  values[i] = rand() % 100;
  printf(" %d", rand() %100);
 }
 shell_sort(values, 50);
 printf("\n Sorted list is as follows \n");
 for (i = 0; i < 50; i++)
  printf("%d ", values[i]);
}
-------------------------------------------------------------------------
C++
-------------------------------------------------------------------------
// SHELL SORTING
// SHELL.CPP
# include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
   class shell
       {
 private:
  int temp, gap, i, swap;
 public:
  void shell_sort(int *, int );
  void display(int *, int);
       };
void shell :: shell_sort(int array[], int size)
   {
     gap = size / 2;
     int k =0;
   do {
    do {
      swap = 0;
      k++;
      for (i = 0; i < size - gap; i++)
       if (array[i] > array[i + gap])
        { 
          temp = array[i];
          array[i] = array[i + gap];
          array[i + gap] = temp;
   swap = 1;
 }
 for(int t=0;t<size; t++)
  cout<<" "<<array[t];
 cout<<"  Swap="<<swap;
 cout<<"\n";
    } while (swap);
   } while (gap = gap / 2);
  }
  void shell :: display(int list[], int n)
     {
       cout<<"\n Sorted list is as follows:\n";
       for( int i = 0; i < n; i++)
 cout<<"  " << list[i];
     }
   void main(void)
    {
    shell sort;
    int list[50];
    int number;
   cout<<"\n Input the number of elements in the list:";
   cin>>number;
   for (int i = 0; i < number; i++)
   {
      cout<<"\n Input the value for the "<< i+1<<" : ";
      cin>>list[i];
   }
   sort.shell_sort(list, number);
   sort.display(list,number);
 }

GTU C PROGRAMS | RADIX SORT PROGRAM

C :
--------------------------------------------------------------------------

/* RADIX SORT */
/* RADIX.C*/
# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>
struct node
{
 int data ;
 struct node *next;
};
typedef struct node node1;
node1 *first;
node1 *pocket[100], *pocket1[100];
void create_node(node1 *, int);
void display(node1 *);
node1 *radix_sort(node1 *);
int large(node1 * );
int numdig(int );
int digit(int , int);
void update(int, node1 *);
node1 *Make_link(int, node1 *);
/* This function create nodes and take input data */
void  create_node(node1 *rec, int n)
{
 int i, j, k;
 for(i = 0 ; i< n; i++)
 {
  rec->next = (node1 *) malloc(sizeof(node1));
  printf("\n First node value: %d: ", i);
  scanf("%d", &rec->data);
  rec = rec->next;
 }
 rec->data = NULL;
 rec->next = NULL;
}
/* Output Function */
void  display(node1 *rec)
{
 while(rec != NULL)
 {
  printf(" %d", rec->data);
  rec= rec->next;
 }
}
/* This radix sort function */
node1 *radix_sort(node1 *rec)
{
 node1 *r, *nex;
 int poc = 0 ;
 int i, j, k;
 int larg = large(rec);
 int m = numdig(larg);
 /* These statements create pockets */
 for(k = 0 ; k < 10; k++)
 {
  pocket[k] = (node1 *)malloc(sizeof(node1));
  pocket1[k] = (node1 *)malloc(9*sizeof(node1));
 }
 /* These statements initialize pockets */
 for(j = 1; j <= m ; j++)
 {
  for(i = 0 ; i < 10 ; i++)
  {
   pocket[i] = NULL;
   pocket1[i] = NULL ;
  }
  r = rec ;
  while(r != NULL)
  {
   int dig = digit(r->data, j);
   nex = r->next ;
   update(dig,r);
   r = nex;
  }
  if(r!= NULL)
  {
   int dig = digit(r->data,j);
   update(dig,r);
  }
  while(pocket1[poc] == NULL)
   poc ++;
  rec = Make_link(poc, rec);
 }
 return(rec);
}
/* This function finds largest number in the list */
int large(node1 *rec)
{
 node1 *save ;
 int p = 0;
 save = rec ;
 while(save != NULL)
 {
  if(save ->data > p)
  {
   p = save->data;
  }
  save = save->next ;
 }
 printf("\n Largest element: %d", p);
 return(p);
}
/* This Function finds number digits in a number */
int numdig(int large)
{
 int temp = large ;
 int num = 0 ;
 while(temp != 0)
 {
  ++num ;
  temp = temp/10 ;
 }
 printf("\n  Number of digits of the number %d is %d\n", large, num);
 return(num);
}
/* This function scarve a number into digits */
int digit(int num, int j)
{
 int dig, i, k;
 int temp = num ;
 for(i = 0 ; i < j ; i++)
 {
  dig = temp % 10 ;
  temp = temp / 10 ;
 }
 printf("\n  %d digit of number  %d is %d", j, num, dig);
 return(dig);
}
/* This function updates the pockets value */
void  update(int dig, node1 *r)
{
 if(pocket[dig] == NULL)
 {
  pocket[dig] = r ;
  pocket1[dig] = r ;
 }
 else
 {
  pocket[dig]->next = r ;
  pocket[dig] = r ;
 }
 r->next = NULL;
}
/* This function create links between the nodes */
node1* Make_link(int poc , node1 *rec)
{
 int i, j, k;
 node1 *pointer;
 rec = pocket1[poc];
 for(i = poc +1 ; i< 10 ; i++)
 {
  pointer = pocket[i-1];
  if(pocket[i] != NULL)
   pointer->next= pocket1[i];
  else
   pocket[i] = pointer ;
 }
 return(rec);
}
/* Main function */
void  main()
{
 node1 *start, *pointer;
 int number;
 printf("\n Input the number of elements in the list:");
 scanf("%d", &number);
 start = (node1 *)malloc(sizeof(node1));
 create_node(start, number);
 printf("\n Given list is as follows \n");
 display(start);
 start = radix_sort(start);
 printf("\n Sorted list is as follows:\n");
 display (start);
}
-------------------------------------------------------------------------
C++

-------------------------------------------------------------------------
 // RADIX SORT
 // RADIX.CPP
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;
};
typedef struct node node1;
class radix
{
 public:
  node1 *first;
  node1 *pocket[100], *pocket1[100];
 public:
  void create_node(node1 *, int);
  void display(node1 *);
  node1 *radix_sort(node1 *);
  int large(node1 * );
  int numdig(int );
  int digit(int , int);
  void update(int, node1 *);
  node1 *Make_link(int, node1 *);
};
// This function create nodes and take input data
void radix :: create_node(node1 *rec, int n)
{
 for( int i = 1 ; i<= n; i++)
 {
  rec->next = (node1 *) malloc(sizeof(node1));
  cout<<"\n First node value:"<<i<<":";
  cin>>rec->data;
  rec = rec->next;
 }
 rec->next = NULL;
}
// Output Function
void radix ::  display(node1 * rec)
{
 while(rec)
 {
  cout<<"  "<<rec->data;
  rec= rec->next;
 }
 getch();
}
// This radix sort function
node1 * radix ::  radix_sort(node1 *rec)
{
 int larg = large(rec);
 int m = numdig(larg);
// These statements create pockets
 for(int k = 0 ; k < 10; k++)
 {
  pocket[k] = (node1 *)malloc(sizeof(node1));
  pocket1[k] = (node1 *)malloc(9*sizeof(node1));
 }
// These statements initialize pockets
 for(int j = 1; j <= m ; j++)
 {
  for(int i = 0 ; i < 10 ; i++)
  {
   pocket[i] = NULL;
   pocket1[i] = NULL ;
  }
  node1 *r = rec ;
  while(r != NULL)
  {
   int dig = digit(r->data, j);
   node1 *nex = r->next ;
   update(dig,r);
   r = nex;
  }
  if(r!= NULL)
  {
   int dig = digit(r->data,j);
   update(dig,r);
  }
  int poc = 0 ;
  while(pocket1[poc] == NULL)
   poc ++;
  rec = Make_link(poc, rec);
  cout<<"\n Newly ordered list:\n";
  display(rec);
 }
 return(rec);
}
// This function finds largest number in the list
int radix :: large(node1 *rec)
{
 node1 *save ;
 int p = 0;
 save = rec ;
 while(save != NULL)
 {
  if(save ->data > p)
   p = save->data;
  save = save->next ;
 }
 cout <<"\n Largest element:"<<p;
 return(p);
}
// This Function finds number digits in a number
int radix :: numdig(int large)
{
 int temp = large ;
 int num = 0 ;
 while(temp != 0)
 {
  ++num ;
  temp = temp/10 ;
 }
 cout <<"\n Number of digits of the number "<<large<<" is "<<num ;
 return(num);
}
// This function scarve a number into digits
int radix :: digit(int num, int j)
{
 int dig ;
 int temp = num ;
 for( int i = 0 ; i < j ; i++)
 {
  dig = temp % 10 ;
  temp = temp / 10 ;
 }
 cout<<"\n";
 cout <<j <<" digit of number "<<num <<" is "<<dig;
 getch();
 return(dig);
}
// This function updates the pockets value
void radix :: update(int dig, node1 *r)
{
 if(pocket[dig] == NULL)
 {
  pocket[dig] = r ;
  pocket1[dig] = r ;
 }
 else
 {
  pocket[dig]->next = r ;
  pocket[dig] = r ;
 }
 r->next = NULL;
}
// This function create links between the nodes
node1* radix :: Make_link(int poc , node1 *rec)
{
 node1 *pointer;
 rec = pocket1[poc];
 for(int i = poc +1 ; i< 10 ; i++)
 {
  pointer = pocket[i-1];
  if(pocket[i] != NULL)
   pointer->next= pocket1[i];
  else
   pocket[i] = pointer ;
 }
 return(rec);
}
// Main function
void  main()
{
 radix rad;
 node1 *start, *pointer;
 int number;
 cout<<"\n Input the elements of the list :\n";
 cout<<"\n Input the number of elements in the list:";
 cin>>number;
 start = (node1 *)malloc(sizeof(node1));
 rad.create_node(start, number);
 cout<<"\n Given list is as follows \n";
 rad.display(start);
 start = rad.radix_sort(start);
 cout<<"\n Sorted list is as follows:\n";
 rad.display (start);
}

GTU C PROGRAMS | QUICK SORT PROGRAM

C :

/* quick.c */
#include <stdio.h>
#include <stdlib.h>
void quick_sort(int array[], int first, int last)
{
 int temp, low, high, list_separator,i;
 low = first;
 high = last;
 list_separator = array[(first + last) / 2];
 do {
  while (array[low] < list_separator)
   low++;
  while (array[high] > list_separator)
   high--;
  if (low <= high)
  {
   temp = array[low];
   array[low++] = array[high];
   array[high--] = temp;
  }
 } while (low <= high);
 for (i = 0; i < 11; i++)
  printf("%d ", array[i]);
 printf("\n");
 getch();
 if (first < high)
  quick_sort(array, first, high);
 if (low < last)
  quick_sort(array, low, last);
}
void main(void)
{
 int values[]={10,23,64,21,74,95,2,59,44,87,55}, i;
 clrscr();
 for (i = 0; i < 11; i++)
  printf("%d ", values[i]);
 printf("\n");
/* printf("\n Unsorted list is as follows \n");
 for (i = 0; i < 20; i++)
 {
  values[i] = rand() % 100;
  printf(" %d", rand() %100);
 }*/
 quick_sort(values, 0, 10);
 printf("\n Sorted list as follows\n");
 for (i = 0; i < 11; i++)
  printf("%d ", values[i]);
 getch();
}

------------------------------------------------------------------

C++

  // QUICK SORT
  # include<iostream.h>
  # include <stdlib.h>
    class quick
       {
 private: int temp, low, high, pivot;
 public:
 void Q_sort(int *, int , int );
 void display(int *, int );
      };
// sorting function
void quick :: Q_sort(int array[], int first, int last)
 {
   low = first;
   high = last;
   pivot = array[(first + last) / 2];
   do {
     while (array[low] < pivot )
       low++;
     while (array[high] > pivot)
       high--;
     if (low <= high)
      {
 temp = array[low];
 array[low++] = array[high];
 array[high--] = temp;
      }
   } while (low <= high);
  if (first < high)
    Q_sort(array, first, high);
  if (low < last)
    Q_sort(array, low, last);
 }
   void quick :: display(int list[], int n)
     {
       cout<<"\n List after sorting the elements:\n";
       for( int i = 1 ; i <= n ; i++)
       {
  cout<<"  "<<list[i];
       }
     }
 void main(void)
  {
   quick sort;
   int list[100];
   int number ;
   cout<< "\n Input the number of elements in the list:";
   cin>> number;
   for ( int i = 1; i <= number; i++)
    {
     cout<<" Input the value for : "<< i <<" : ";
     cin>>list[i];
    }
  sort.Q_sort(list, 1, number);
  sort.display(list, number);
   }

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms