Sunteți pe pagina 1din 7

ARRAYS

Q) Write a short notes on arrays in C?


Array is similar type of data collection referenced by unique name i.e. it
is a collection of same data type elements.or An array is a group of related
data items, which share common name. or It is the set of homogeneous data.
Types of Array:
The types of the array depends upon the given problem. Array are mainly
of two types:
(i) Linear Array
(ii) Non-Linear Array
(i). Linear Array:
This type of array is also called one Dimensional Array. This is also called
list array. Another name of linear array is called single-dimensional array. These
array are of ladder type. In the linear array only one subscript is used. It is
written either in row or in column form. The syntax to define or declare linear
array is
data-type array-name [size];
Where data types are integer (int), real(float), double and character(char)
Ex :
int x[50];
float salary[30];
char name[20];

Declaration of array which have fixed or constant value is defined as:


static data-type array-name [size] = {list of values};

For example, the above said different values can be assigned to the array as
static int numbers [5] = {55, 35, 40, 47, 33);
Now if we assign data as:
static int number[5]={55, 35, 40};
then the different values assigned to different array variables as:
number[0]=55, number[1]=35, number[2]=40, number[3]=0, number[4]=0.
Similarly we can declare and define values in float type of variables.
For example:
static float a[4] = {10. 3, 50, 7.2};

Here a[0] = 10.3,


a[l] = 50.0,
a[2] = 7.2,
a[3] = 0.0.

Also you can declare character type data in the array as:
static char name[ ]= { ‘D’, ‘H’, ‘E’, ‘E’, ‘R’, ‘A’, 'J'};
Here data to the character variable be assigned as: name[0] = 'D', name[1] =
'H', name[2] = 'E', name[3] ='E', name[4] = 'R', name[5] = 'A', name[6] = 'J',
name[7] = '\0',

(ii). Non-Linear Array:


Array of having different dimensions or n subscript is in the form of
Nonlinear array. Non-linear array are further of n different types as:

42
(a) Two Dimensional Array
(b) Three Dimensional Array

a). Two Dimensional Array:


These arrays are also called double dimensional array. Another
name of two-dimensional array is Tabular or Rectangular Array. These arrays are
in row and column form, so these are also called Row-Column array or square
Array. These arrays have two subscripts and also called double subscripted
array. We can write the double dimensional array in the matrix form as below
and so these are also known as matrix array. The syntax used for declaration of
two dimensional array is as:

data-type array-name [row size] [column size];

For example, some of the valid double dimensional arrays are written as below:
int a[10][10];
float b[50][50];
char name[10][20];

Also you can declare two-dimensional array in static form i.e. these type of array
declarations have fixed value or constant value.
The syntax used for static two dimensional array is as follows:
static data-type array-name [row size][column size = {list of value};
For example, suppose you want to assign some fixed value to a variable array
named table by using the static two-dimensional array as.
static int table [2][3] = {15, 7, 12, 2, 20, 8};

Here the values assigned to the tabular array-name is as:


table[0][0] = 15
table[0][1] = 7
table[0][2] = 12
table[l][0] = 2
table[l][1] = 20
table[l][2] = 8
Also we can write the above statement as:
static int table[ ][ ] = {15, 7, 12, 2, 20, 8};
Above statement fails in some cases.
Another way to write the above statement is as follows:
static int table[][]={ {5, 7, 12} , {2, 20, 8} };

char table[2][30]={“kumar”,”dheeraj”};

b)Multi Dimensional Array:

It is a collection of double dimension arrays. Each double dimension


array is identified by with ‘i’ each array is identified by ‘j’ and each
value is identified by with ‘k’

Syntax:

43
Datatype variable[no_of_dd_arrays][row_size][col_size];

Eg:
int a[2][3][3];

initialization of array

int a[2][3][3] ={
{
{1,2,3},
{4,5,6},
{7,8,9}
},

{
{1,2,3},
{4,5,6},
{7,8,9}
},
};

Int a[2][3][3]= {
{{1,2,3},{4,5,6},{7,8,9} }},

{{1,2,3},{4,5,6},{7,8,9} }},
};

Program to initialize multidimension array and display the array


#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k;
int a[2][3][3] =
{
{1,2,3}, {4,5,6}, {7,8,9},
{3,2,1}, {6,5,4}, {9,8,7}
};

clrscr();

printf(“Matrix is\n”);

for( i=0; i<2; i++ )


{
for(j=0; j<3; j++)

44
{
for(k=0; k<3; k++ )
{
printf(“ %d ”,a[i][j][k]);
}
printf(“\n”);
}
printf(“\n\n”);
}
getch();
}

Q) What is a sparse matrices?


A matrix in which number of zero entries are much higher than the
number of non zero entries is called sparse matrix.
A square matrix is called lower triangular if all the entries above the main diagonal
are zero. Similarly, a square matrix is called upper triangular if all the entries below
the main diagonal are zero

Q) Explain different String Handling Functions in C? or miscellaneous strings ?

C-language is rich in library functions, but to handle or operate some


operations with string, we use some powerful string-handling functions. All these
function are linked with the "string.h" header file stored in the include
sub-directory in the Turbo-C compiler. Here we explain the five commonly used
string handling function as:
(i) strcat ( )
(ii) strcmp ( )
(iii) strcpy ( )
(iv) strlen ( )
(v) strrev ( )
(i) strcat ( )

strcat( )
The purpose of this string handling function strcat( ) is to concatenate or combine two
different strings together.
The general syntax used for this is as:
strcat(string1, string2);
Here string1 is combined with string2 and result will be stored (combined
string) in string1. For example, suppose x is "HELLO" and y is "DEAR", then after
applying the strcat( ) function, the result will be "HELLO DEAR" and it will be
stored in the x.
The representation can be done by using the procedure as:

char x[80], y[30];


scanf("%s%s",x,y);

45
strcat (x, y);
printf ("%s", x);

Also we can concatenate more than one string by using nested concatenation:
strcat (strcat (string1, string2), string3);
For example, suppose if x variable has the data "SRI” the y variable has
the data "SAI" and the z variable has the data "RAM", then after applying the
below written nested strcat( ) function:

strcat (strcat (x, y), z);


the result be "SRI SAI RAM".

strcmp( ):
The purpose of this function is to compare two strings. It will check which
string is alphabetically above the others. For comparison ASCII (American
Standard Code for Information Interchange) values are used.
The general syntax is as:
strcmp (string1, string2);
If the ASCII difference of each character in the two different strings from
the first alphabet (till null character of any string occurs first) is zero, then both
the strings are equal. If difference is +ve, then string2 is larger than the string1,
but if the difference is -ve, then string1 is larger than the string2. For example,
suppose you have two character string variables having two different strings i.e.
"their" is stored in the string1 variable and "there" is stored in the string2
variable, then you can compare these two strings by using the strcmp function
as:
char string1[20] , string2[20];
strcmp (string1, string2);

Here the ASCII difference between string1 and string2 is negative and so
the string1 is alphabetically above than the string2. Similarly you can apply
some more operations using this function.

strcpy( ):
The purpose of this function is to copy one string into another string. Note
that target or destination field should be larger than the source field. In other
words size of the string1 should be larger to receive the contents of the string2.
The syntax is as follows:
strcpy (string1, string2);

It will copy the data from the string2 to the string1 variable. Also note
that the length of the string1 should be larger than string2. For example, if you
want to copy the data "BOMBAY" to the string variable city, then the procedure
is as follows:

char city[30];
strcpy(city, "BOMBAY");

46
Note that string1 always should be a character type variable and should
not be a data, whereas you can take string2 as data and string both. For
example,
below statement is an invalid statement:

strcpy("DELHI", "BOMBAY");
strcpy("DELHI", city1);

But following are the valid statements:

strcpy(city1,city2);
strcpy(city, "BOMBAY");

Here city 1 and city2 are the two string variable (character type).

strlen( ):
The purpose of this function is to count the number of character in a
string i.e. to find the length of the string. The general syntax is as:
n = strlen (string);
Where n should be of integer type and string variable should be of character
type. For example, suppose if you want to find the number of character (length
of the string) of the string "I am a good boy" assigned to the string variable ct,
then this can be done as

int n;
char ct[20];
n = strlen(ct);
printf ("length is = %d", n);

It will display the result 15 (space character is also included).


Note that counting ends at the NULL character i.e. Null character "\0" is
not included in the counting of characters.
strrev( ):
The purpose of this function is to reverse a string. This function takes
string variable as its single (only one) argument. Here the first character
becomes last and last character becomes first in a string. It is very useful to find
whether a string is palindrome or not. Here "madam" is a palindrome string,
because after reversing the string using this function, the resultant string
remains the same as original. The general syntax used for this function is as
follows:
strrev(st);
Here st is a string (character type) variable. For example, if you have a
string data "vinoothna” the string variable st, then after using the function as:

strrev(st);
printf("\n Reversed string is:%s",st);

The string variable st will display the result "anhtooniv”.

Q) Explain string taxonomy?


47
In c string can store either in fixed length format or in variable length format
Fixed length string : we need specify an appropriate size for string variable. If
the size is too small, then you will not be able to store all the elements in the
string. If the string is large, then unnecessarily memory space will be wasted.
Variable length string: in this string can be expanded or contracted to
accommodated the elements in it. This can be done either by using length
controlled string or a delimiter.
Length controlled string: in a length controlled string u need to specify the
number of characters in the string.
Delimited string:
The string is ended with a delimiter. This identify the end of the string.

Function Description
strcat() Appends a string to another string
strcmp() Compares two strings
strcmpi() Compares two strings ( case insensitive)
strcpy() Copies a string to another string
stricmp() Same as strcmpi()
strlen() Returns the length of a string
strlwr() String converted to lowercase
strncat() n characters of string appended
strncmp() n characters of two strings compared
strncpy() n character of a string are copied to another
strrev() Reverses characters in a string
strstr() Locates one string in another string
strupr() Converts string to uppercase

Strspn() It returns the index of the first character in string1 that


doesnot match any character in string2
Strtok() This function returns a pointer to the last token found in
the string. A null pointer is returned if there are no
tokens left to retrieve.
Atoi() This function returns the converted integral number as
an int value. If no valid conversion could be performed,
it returns zero.
Atof() This function returns the converted floating point
number as a double value. If no valid conversion could
be performed, it returns zero (0.0).
Atol() This function returns the converted integral number as a
long int. If no valid conversion could be performed, it
returns zero.

48

S-ar putea să vă placă și