GTU MATERIAL PROVIDE YOU ALL TYPE OF EDUCATION MATERIAL | DOWNLOAD FREE MATERIAL | EDUCATION SOFTWARES | EXAM ALERTS | EBOOKS | EXAM PAPERS | TIME TABLE | ALL TYPE OF SYLLABUS | MBA | MCA | ENGINEERING | BE | FREE MATERIAL PROVIDE FOR YOU ONLY
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 1ST SEMESTER. Show all posts
Showing posts with label GTU MCA 1ST SEMESTER. Show all posts
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.
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);
}
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);
}
// 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);
}
/* bubble.c */
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int array[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void main(void)
{
int values[30], i;
printf("\n Unsorted list is as follows\n");
for (i = 0; i < 10; i++)
{
values[i] = rand() % 100;
printf(" %d", rand()%100);
}
bubble_sort(values, 10);
printf("\n Sorted list is as follows\n");
for (i = 0; i < 10; i++)
printf("%d ", values[i]);
}
------------------------------------------------------------
C++
// BUBBLE SORT
# include<iostream.h>
# include<conio.h>
class bubble
{
private:
public:
void bubble_sort(int , int *); // prototype
void display(int *, int);
};
// definition of function
void bubble :: bubble_sort(int n, int l[])
{
int limit = n - 1 ;
int flag = 1 ;
for(int j = 0 ; j< n - 1; j++)
{
for(int k = 0 ; k< limit - j ; k++)
{
if(l[k] > l[k+1])
{
int temp = l[k];
l[k] = l[k+1];
l[k+1] = temp ;
flag = 0;
}
}
if(flag)
break ;
else
flag = 1;
}
}
void bubble :: display(int list[], int number)
{
for( int i = 0 ; i < number ; i++)
cout<<" "<< list[i];
}
void main()
{
bubble sort;
int number, key, list[200];
clrscr();
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number;
for(int i = 0 ; i < number; i++)
{
cout<<"\nInput the elements of the list : "<< i+1<<" : ";
cin >> list[i];
}
cout<<"\n Entered list is as follows:\n";
sort.display(list,number);
sort.bubble_sort(number, list);
cout<<"\n After sorting list is as follows:\n";
sort.display(list, number);
}