A R R A Y
What is an array?
An array is a collection of similar type of data items. This allocates contagious
memory allocations refereed by a single name.
Specifying the array name followed by subscript in brackets indicates the elements of array.
Ex : arr[ 10 ] i.e arr[0],arr[1]…………………………….arr[9]
The number of subscripts determines the dimension of array. arr[0] is the first element
and arr[n-1] is the last element of the array. Since C starts counting from 0.
The subscript of an array can be positive integer constant
Syntax : type variable_name[size];
The type specifies the type of element that will be contained in the array .(i.e int , float,
char). The size indicates the maximum number of elements that can be stored inside the array.
Types of array:
1. One dimensional array (liner array)
2. Two dimensional array
3. Multidimensional array
1. ONE DIMENSIONAL ARRAY
int no[5]={2,4,1,6,7};
float no[5]={2.3,5.6,7.8,2.7,8.9};
char name[5]=”MITM”;
C language treat’s character strings simply as array of
characters.
When the compiler sees a character string, it terminates
with an additional null character ‘\0’ holds at the end. So when
declaring character arrays we must always allow one extra
element space for the null terminator.
Rules to initialization of array:
· Array can be initialized only with the constants.
· Numeric arrays are automatically initialized with 0.
· The whole array can be referenced by the array name.
· Fewer initialize more then the specified size gives erroneous result.
· Array can be initialized without mentioning the number of elements. it is derived
automatically by the compiler with respect to the number of elements inside the braces.
· Array always stored in contagious memory locations.
Amiya Kumar Dash, KIIT 21
DRAWBACKS:
# There is no continent way to initialize only selected elements.
# There is no shortcut method for initializing a large number of array elements like other language.
# Boundary checking: In c language there is no check to see if the subscript used for an array
exceeds the size of the array. Do remember the
2. TWO DIMENSIONAL ARRAY (array of arrays)
As we know one-dimensional array use one bracket so two-dimensional array use n brackets.
Syn: data type array name [row size][column size];
Ex: int arr[3][4]={
{1,2,3,4},
{3,2,1,5},
{8,2,9,2}
};
Here 3 denote the row and 4 denotes the column i.e. in first bracket each element stores
the seconds brackets elements.
So the total no of element is 3*4=12
The two dimensional array is also called a matrix.
N.B (While initialization the values}:
Commas are required after each brace that closes off a row, except in case of the last
row. When all the elements are to be initialized to zero, in short cut method is
static arr[2][3]={{0},{0}};
Array elements are have been stored row wise and accessed row wise (you can store and
access column wise traditionally row wise)
It is necessary to mention the second (column) dimension, where as, the first dimension
(row) is optional.
The memory does not contain rows and columns the array elements are stored in one
contentious chain.
3. MULTI DIMENSIONAL ARRAY
Array can have more than one dimension and it can be represented by more than one
subscript. n dimensional array use n brackets.
Ex: A three dimensional array can be thought of as an array of
arrays. int arr[3][4][2]={
{ {2,4}, {7,8}, {3,4}, {5,6} },
{ {3,4}, {9,1}, {7,4}, {2,6} },
{ {8,9}, {7,2}, {2,3}, {5,1} } };
The outer array has three elements each of which is a two dimensional array of four rows,
each of which is a one-dimensional array of two elements.
N.B: [Link] if for three-dimensional array begins counting with zero.
Amiya Kumar Dash, KIIT 22
2. The no of elements in n dimensional array = multiplication of no of given each bracket.
SORTING: Sorting is the process of arranging the data or information in some logical
order. The logical order nay be ascending or descending order in case of numeric values or
dictionary order in case of alphanumeric value.
SEARCHING: Searching refers to the process of finding the location of given data
element from a collection of data.
MERGING: Merging refers to the process of combining the elements of two similar
subroutines such as linear array into a single structure.
S T R I N G
A string is an array of characters.
Note : Any group of characters defined between double quotation marks is a string constant.
Declaring and initializing string variables:
When the compiler assigns a character string to a character array, it
automatically supplies a NULL character (\0) at the end of the string. So the size should be
equal to the maximum of characters in the string plus one.
char name[5]=”MITM”;
When we initialize a character array by listing its elements, we must supply explicitly
the NULL terminator.
Char name[5]={‘M’,’I’,’T’,’M’,’\0’};
Common operations on character strings are :
1. Reading and writing strings.
2. Find the length of a string.
3. Reverse a string.
4. Combining (Concatenating) strings together.
5. Copying one string to another.
6. Comparing strings for equality.
7. Extracting a portion of a string.
Amiya Kumar Dash, KIIT 23
STRING LIBRARY FUNCTION :
String may be defined as a single character or combination of more than one character
and represented within double quotes.
strlen( ) : This function return the length of the string that is the number of characters in
the string. strlen(“tulika”) return the value 6.
If s1 is the array that contains the name barun then strlen(s1) return the value 5.
/* program for finding length of the string */
#include<stdio.h>
#include<string.h>
main( )
{
char str[20];
int length;
printf(“enter the string
:”); scanf(“%s”, str);
length=strlen(str);
printf(“the length of string is %d\n”,length);
}
strcmp( ) : This function is used for comparision of two string, if the two string match, strcmp(
) would return a value 0, otherwise it would return a non-zero value.
This function compares the string character by
character.strcmp(s1,s2) returns a value-
< 0 when s1<s2
= 0 when s1 = =
s2 >0 when s1>s2
/ * program for comparing two string */
#include<stdio.h>
#include<string.h>
main( )
{
char str1[10],str2[20];
printf(“enter the first string
:”); scanf(“%s”, str1);
printf(“enter the second string
:”); scanf(“%s”,str2);
if((strcmp(str1,str2))= = 0)
printf(“string are same \n”);
else
printf(“strings are not same \n”);
}
Amiya Kumar Dash, KIIT 24
strcpy( ): This function is used for copying of one string to another string.
strcpy(str1,str2) copy str2 to str1. Here str2 is the source string and str1 is
destination string. If str2 = “suresh” then this function copy “suresh” into str1.
/* program for copy a string */
#include<stdio.h>
#include<string.h>
main( )
{
char str1[10],str2[10];
printf(“enter the first string :”);
scanf(“%s”,str1);
printf(“enter the second
string”); scanf(“%s”,str2);
strcpy(str1,str2);
printf(“now the first string is same as the second string :%s\n”,str1);
}
strcat( ): This function is used for concentration of two strings. If first string is “sur” and second
string is “esh” then after using this function the resultant string is “suresh”.
strcat(str1,str2) concatenates str2 at the end of str1.
/* program for combining two string */
#include<stdio.h>
#include<string.h>
main( )
{
char str1[10],str2[10];
printf(“enter the first string :”);
scanf(“%s”,str1);
printf(“enter the second
string”); scanf(“%s”,str2);
strcat(str1,str2);
printf(“ the resultant string is : %s\n”,str1);
}
· strrev(str) reverse the string
· strlwr(str) Converts to lowercase
· strupr(str) Converts Uppercase.
---0---
Amiya Kumar Dash, KIIT 25