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..
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);
}
The first line of a function definition contains the byte specification of the value returned byte the function, followed by the function name and (optionally) a set of parameters separated by comas and enclosed in parenthesis. The type specification can be omitted if the function returns an integer or a character, in entry pair of parenthesis must follow the function name if the function definition does not include any arguments. In general terms, the first line can be
written as :
Data-type name(formal arg1, arg2, argn)
Where data type represents the data type of the value, which is returned, and name represents
the function name.
The formal argument allows information to be transferred from the calling portion of the program to the function. They are also known as parameter on formal parameters.(the corresponding arguments in the functions reference are called actual arguments since they define the information actually being transferred. The identifiers used as formal argument are local in the sense that they are not recognized outside of the function. Hence, the names of the formal arguments may be same as the names of the other identifiers that appear outside the function definition.
The argument declaration follows the first time. All format arguments must be declared at the point in the function. Each format argument must have the same data type as its corresponding actual arguments. That is each formal argument must be of the same data type as the data items it receives from the calling portion of the program.
The remainder of the function definition is a compound statement that defines the action to be taken by the function. This command statement is sometimes referred to as the body of the function. It must follow the formal argument declarations. Like any other compound statements, the statement can contain expression statements. Other compound statement, control statements, and so on. As a result it can even access itself. This process is known as
recursion.
For example,
Low-to-up(Char el)
{
char c2
c2 = c1>=’a’ && c1<=’z’) ? (‘A’+cl-‘a’) : cl;
return c2;
}
Where data type represents the data type of the quantity returned by the function, name represents the function name and argument type 1 argument type 2 argument type n refers to argument data types of the first argument, second argument and so on. Remember that the argument data type are optional even in the situations that requires a function declaration. Most C compilers supports the use of the keyword void in function definition as a return data type indicating that the function does not return anything. Function declaration may also include void for the same purpose. In addition void may appear in an argument list, in both function definition and function declarations, to indicate that a function does not require arguments. In the later case, void appears by itself in the area normally used for argument specifications.
CATEGORY OF FUNCTION :
A function, depending and where argument are present or not and where a value is returned or
not, may belong to one of the following categories.
No arguments and not return values:
When a function has no arguments, it does not receive a data from the calling function.
Similarly, when it does not return a value, the calling function does not receive any data from
calling function. In fact, there is not data transfer between the calling function and the called
function.
We would ensure that the function call has matching arguments. In case, the actual arguments
are more than the formal arguments, extra actual arguments are discarded. On the hand, if the
actual arguments are less than the formal arguments, the unmatched formal arguments are
initialized to some garbage values. Any mismatch in data type may also result in passing of
garbage value. Remember no error message will be generated.
While the formal arguments must be void variable names; the actual arguments may be
variable name expression or constants. The variable used in actual arguments may be assigned
values before the function call is made.
Arguments with return values :
When function is called with arguments, then called function will receive the arguments. Then
the statement included within the body at the called function will be executed.
RECURSION :
Recursion is a process by which a function calls itself repeatedly, until some specified condition has been satisfied. The process is used for repetitive computation in which each action is stated on forms of previous result. The process is used for repetitive computation in which each action in terms of a previous result. Many iterative problems can be written in this form.
In order to solve a problem recursively, two condition must be satisfied. First, the problem must be written in recursive form and second the problem example, we wish to calculate the factorial of a positive integer quantity. We would normally express this problem as n1=1 x 2 x 3 x 4 .. x n where n is the specified positive integer. However, we can also express in another way, by writing n=1 = n*(n-1). This is a recursive statement of the problem, in which last expression provides a stopping condition for the recursion.
When a recursive program is executed the recursive function calls are not executed
mmediately. Rather, they are placed on a stack until the condition that terminates the recursion
s encountered. The function calls are then executed in reverse order, as they are popped off the
tack.
f a recursive function contains local variables, a different set of local variables will be created
during each call. The name of the local variables, will be cause always be the same, as declared
within the function. However, the variables will represent a different set of values each time
he function is executed. Each set of values will be stored on the stack, so that they will be
vailable as the recursive process unwinds i.e. as the various function calls are popped off
he stack and executed.
THE SCPOE AND LIFETIME OF VARIABLES IN FUNCTION :
A variable in C can have any one of the four storage classes :
Automatic variables :
Automatic variables are declared inside a function in which they are to be utilized. They are
reated when the function is called and destroyed automatically when the function is exited,
hence the name automatic. Automatic variables are therefore private to the function in which
hey are declared. Because of this property, automatic variables are also referred to as local or
internal variable. A variable declared inside a function without storage class specifications, by default, an
automatic number in the example below is automatic.
One important feature of automatic variable is that their value cannot be changed accidentally. This assures that we may declare and use the same variable name in different function in the ame program without causing any confusion to the compiler. There two consequences of the cope and longevity of auto variables. First any variable local to main will normally live hroughout the whole program, although, it is active only in main. Secondly, during recursion, he nested variables are unique auto variables, a situation similar to function, nested auto variable with identical names.
Automatic, variable can also be defined within a set of braces known as blocks they are
meaningful only inside the block where they are defined.
External variables :
Variables that are both alive and active throughout the entire program are known as external
variables. They are also known as global variables. Unlike function in the program external
variables are declared outside a function. For example the external declaration of integer
number and float length might appear is :
Int number;
Float length = 7.5;
Main()
{
---
---
}
function1()
{
---
---
}
function2()
{
---
---
}
the variables number and length are available for use in all the three functions. In case a local
variable and a global variable have the same name, the local variable will have preceded over
the global variable in the function where it is declared.
Once a variable has been declared as global, any function can use it and change its value. Then
subsequent function can reference only that new value. Because of this property, we should try
to use global variables only for tables of for variables shard between functions when it is
inconvenient to pass them as parameters.
One other aspect of a global variable is that it is visible only form the point of declaration to
the end of the program.
Note that the extern declaration does not allocate storage space for variables. In case of arrays,
the definition should include their size as well.
An extern within a function provides the type information to just that one function. We can
provide type information to all function within a file by placing external declaration before any
of them, as shown below.
Extern float height []
Main ()
{
int t;
void printout();
---
---
printout();
}
void printout ()
{
int t;
---
---
}
float height(size);
MULTIFILE PROGRAMS :
Multiple source files can share a variable provided it is declared as an external variable
appropriately variable that are shared by two or more files are global variables and therefore
must declared them according in one file and then explicitly define them with extern in other
files. The extern specifies tells the compiler that the following variables types and names have
already been declared elsewhere and now need to create storage space for them. For example,
File 1.c
Main()
{
extern int m;
int l;
---
---
}
function1()
{
int j;
---
---
}
file 2.c
int m;
function2()
{
int il;
---
---
}
function3()
{
int count;
---
---
}
the extern, declaration in place where secondary reference are made, if we declare a variable as
global in two different files used by a single, program then the linker will have a conflict as to
which variable to use and therefore, is use a warning. When a function is defined in one file
and accessed in other, the later file must include a function declaration. The declaration
identifies the function we usually place such declarations at the beginning of the file, before all
function. Although all functions are assumed to be external, it would be good practice to
explicitly declare such functions with the storage class extern.
Static variables :
As the name suggest, the value of static exist until the end of the program. A variable can be
declared static using the keyword static like:
Static int x;
Static float y;
A static variable may be either an internal type or an external type depending on the piece of
declaration. Internal static variables are those which are declared inside a function. The scope
of internal static variable extend up to the end of the function in which they are defined.
Therefore, internal static variable are similar to auto variables, except that they remain in
existence throughout the remainder of the program. Therefore, internal static variables can be
used to retain values between function calls.
A static variable is initialized only once, when the program is compiled. It is never initialized
again. An external static variable is declared outside that program. The difference between a
static external variable and simple external variable is that the static external variable is
available only within the file where it is defined while the simple external variables can be
accessed by other files.
It is possible to control the scope of the function. For example, we would like a particular
function accessible only to the function in the file in which it is defined. And not to function in
other files. This can be accomplished by defining that function with the storage class static.
Register variable:
We can tell the compiler that a variable should be kept in one of the machine’s register instead
of keeping in the memory where normal variables are stored. Since a register access is much
faster than a memory access. Keeping frequently accessed variables in the register will lead to
faster execution of program. This is done as follows :
Register int count;
Since only a few variables can be placed in the register, it is important to carefully select the
variable for the purpose.
PASSING ARRAYS TO A FUNCTION:
An array name can be used as an argument to a function, thus permitting the entire array to be
passed to the function. The manner in which the array is passed differs mainly. However, from
that of an ordinary variable.
To pass an array to a function, the array name must appear by itself without brackets or
subscripts as an actual argument within the function call. The corresponding format argument
is written in the same manner, though it must be declared as an array within the formal
argument declaration. When declaring array as a format argument, the array name is written
with a pair of empty square brackets. The size of the array is not specified within the format
argument declaration.
The following program outline illustrates the passing of an array from the main portion of the
program to the function :
Main()
{
int n;
float avg;
float list[100];
float average(int a, float x[]);
---
---
avg = average(n,list);
---
}
float average(n,x)
{
int a;
float x[];
---
---
}
within main we see a call to the function average. This function call contains two actual
argument the integer variable n and the one-dimensional floating-point array list. Notice that
list appears as an ordinary variable within the function call.
In the first line of the function definition we see two formal arguments, called a and x. the
formal argument declarations establish a as an integer variable and x as an one-dimensional
floating point array. Thus there is a correspondence between the actual argument list and the
formal argument x. note that the size of x is not specified within the formal argument.
If the first line of a function definition includes the formal argument declaration, each array
name appearing as a formal argument must be followed by an empty pair of square braces. We
can pass the elements of array to the function in two ways :
• pass by value approach
• pass by reference approach
PASSING BY VALUE APPROACH
When each element of array is passed one by one to the functions as an actual argument, at that
time element behaves as an ordinary variable. This manner of passing array value is called pass
by value approach.
In such cases, formal argument need not be an array. It may be any ordinary variable. That
element then be accessed throughout the function and then control returns to the calling
function, that alteration can not be recognized in the calling portion of the program because
passing element process will make a copy of original element to the formal argument.
PASS BY REFERENCE APPROACH
When an array is passed to a function, however the values of the array elements are not passed
to the function. Rather the array name is interpreted as address of the first array element (i.e.
the address of the memory location containing the first array element). This address is assigned
to the corresponding format argument when function is called. The formal argument therefore
becomes a pointer to the first array element. Argument passed in this manner are said to be
passed by reference.
When a reference is mad to an array element within the function, the value of the element
subscript is added to the value of the pointer to indicate the address of the specified array
element. Therefore any array element can be accessed from within the function, alteration will
be recognized in the calling portion of the program.
The return statement cannot be used to return an array. Therefore, if the elements of an array
are to be passed back to the calling portion of the program, the array must either be defined as
an external array whose scope includes both the function and the calling portion of the
program, or it must be passed to the function as a formal arguments.
Array is a collection or group of similar data type elements stored in contiguous memory. The individual data items can be characters, integers, floating points numbers and so on . Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory.
DEFINING AN ARRAY :
Arrays are defined in much the same manner as ordinary variables. Except that each array name must be accompanied by a size specification number of elements for a one-dimensional array. The size specified by positive integer expression enclosed in square brackets. The expression is usually written as a positive integer constants. For example if we want to declare an array of 10 integer values then we can define it as following. The general form is Storage class data type array[expression] = {vlaue1, value2,…valuen}
Single Dimension Array
Int a[10];
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
2 bytes.
2
bytes.
2
bytes.
2
bytes.
2
bytes.
2
bytes.
2
bytes.
2
bytes.
2
bytes.
2 bytes.
2001 2003 2005 2007 2009 2011 2013 2015 2017 2019
Each array elements is referred to by specifying the array name followed by one or more subscripts. With each subscript enclosed in square brackets. Each subscript must be expressed as a non-negative integer : thus in the n- element array x, the array elements arr[0], arr[1], arr[2]..arr[n-1]. The value of each subscript can be expressed as an integer constant, an integer variable or a more complex integer expression.
The number of subscripts determines the dimensionality of the array. For example, x[I] refers to an element in the one dimensional array x. Similarly y[I][j] refers to an element in the two dimensional array..
MULTIDIMENTIONAL ARRAY
Multidimensional, array is defined in much the same manner as one-dimensional arrays, except that a separate pair of square brackets is required for each subscript. Thus a two dimensional array will require two pairs of square brackets, three dimensional array will require three pairs of square brackets and so on.
In general terms, a multidimensional array definition can be written as
Storage_class data_type array[exp1][exp2]…[expn];
Where storage class refers to the storage class of the array, data type is its data type. Array is the array name and exp1, exp2,.. expn are positive valued expressions that indicate the number of array element associated with each subscript. The storage class is optional, the default values are automatic for arrays
that are defined inside of a function, and external for arrays defined outside of a function.
Two Dimension Array
Int a[5][5];
0 1 2 3 4
A[0][0]
A[0][1]
A[0][2]
A[0][3]
A[0][4]
A[1][0]
A[1][1]
A[1][2]
A[1][3]
A[1][4]
A[2][0]
A[2][1]
A[2][2]
A[2][3]
A[2[4]
A[3][0]
A[3][1]
A[3][2]
A[3][3]
A[3][4]
A[4][0]
A[4][1]
A[4][2]
A[4][3]
A[4][4]
The first line defines table as a floating point array having 50 rows and 50 columns (hence 50 x
50 = 2500 elements) and the second line establishes page as a character array with 24 rows and 80
columns (24 x 80 =1920 elements), the third array can be thought of as a set of double precision tables, each having 66 lines and 266 columns (hence 100 x 66 x 255 = 1,683,000 elements).
The last definition is similar to the preceding definition except that the symbolic constant L,M,N defines the array size. Thus the values assigned to these symbolic constant will determine the actual size of the array.
If a multidimensional array definition includes the assignment of initial values, then care must be given to the order in which the initial values are assigned to the array elements(remember only external and static arrays can be initialized). The rule is that the last (right most) subscript increases most rapidly and the first(left most) subscript increases least rapidly. Thus the elements of a two-dimensional array will be
assigned by rows, that is the element of the first row will be assigned, then the element of the second row and so on.
For example, consider following two-dimensional array definition :
Int values[3][4] = {1,2,3,4,5,6,7,8,9,10,11.13};
Note that values can be thought of a table having three rows and for columns (four element per
row.) Since the initial values are assigned by rows (i.e. last subscript including most rapidly,
the results this initial assignment are as follows:
Remembers that the first subscript ranges from 0 to 2 and the second ranges from 0 to 3. This example can be written as :
The natural order in which the initial values are assigned can be altered by forming groups of
initial values enclosed in braces. The values within each innermost pair of braces will be assigned to those array element whose last subscript changes most rapidly. In a two dimensional array, for example, the
value within the inner pair of braces will be assigned to the element of row, since the second subscript increase most rapidly. If there array two few values within a pair of braces, the remaining elements of that row will be assigned zeros. On the other hand, the number of values within each pair of braces cannot be exceed the defined row size.
Multidimensional arrays are processed in the same manners as one-dimensional arrays, an element-by-element basis. However, some care is required when passing multidimensional arrays to a function. In
particular, the formal argument declarations within a function definition must include explicit size specifications in all of the subscript positions except the first. These size specification must be consistent with the corresponding size specifications in the calling program. The first subscript position may be written as an empty pair of square brackets as with a one-dimensional array.
At individual array element that are not assigned explicit values will automatically to set to zero. This includes the remaining elements of an array in which certain elements have been assigned non zero values. The array size need not be specified explicitly when initial values are included as a part of an
array definition, with a numerical array, the size will automatically be set equal to the number of initial values included within the definition.
ARRAY AND STRINGS
The gets and puts functions:
The gets and puts functions facilitate the transfer of strings between the computer and the standard input/output devices. Each of these functions accepts a single argument. The argument must be a data item that represents a string(e.g. character array). The string may include white space characters. In the case of gets, the string will enter from the keyboard and will terminate with a new line character(i.e. the string will end when the user presses the RETURN key). The gets and puts functions offer simple alternatives to the use of scan f and printf for reading and displaying strings, as illustrated in the following example. Here is a program that reads a line of text into the computer and then writes it back out in its original form :
This program utilizes gets and puts to transfer the line of text into and out of the computer. Most C compilers include library functions that allow strings to be compared. Copied or concatenated. Other functions permit operations on individual character within strings. For example, they allow individual characters to be found within strings and so on. The following example illustrate the use of some of these library functions :
Strlen()
This function counts a number of characters present in a string while giving a call to the function we are to pass the base address of the string. Total numbers of characters of the stings are counted without counting null character, returning length of the string.
Static char msg[] = “Lord Krishna”;
Int n;
N=strlen(msg);
Printf(“length of string =%d”, n);
Strcpy()
This function copies the contents of one string to another. The base address of source and target strings
are supplied to the function.
Static char source[] = “Lord Krishna”;
Static char target[15];
Strcpy(target, source);
Printf(“Source string is : %s”, source);
Printf(“target string is :%s”,target);
Strcat()
This function concatenates the source string at the end of target sting.
Static char s[] = “Lord”;
Static char t[] = “Krishna”;
Strcat(t,s);
Printf(“source string is %s\n”, s);
Printf(“target stirng is %s\n”, t);
This function compare two strings to find out whether they are same of different. The process of character wise comparison continues till a mismatch is attained or- end of string. If two strings are identical, it returns a values zero, if they are not, it returns numeric difference between ASCII values of non-matching characters.
Strcmp() :
This function compares two strings and returns some integer value. If both the strings are equal then it returns 0 otherwise it returns some other integer value.
Main()
{
static char s1[]=”Jerry”;
static char s2[]=”Ferry”;
static char s3[]=”jerry boy”
int I,j,k;
I = strcmp(s1, “jerry”);
J= strcmp(s1, s2);
J=strcmp(s1, s3);
Printf(“%d %d %d”, I,j,k);
}
PROCESSING AN ARRAY :
Single operations involving entire arrays are not permitted in C. thus if a and b are similar
arrays, assignment operations. Comparison operations, and so on must be carried out on an
element-by-element basis. This is usually accomplished within a loop where each pass of loop
will therefore equal the number of array elements to be passed.