ALL SIR NOTES HAVE BEEN UPLOADED HERE.
An array is defined as the collection of similar type of data items stored at contiguous memory locations. It is simply a grouping of like-type data. Arrays are the derived data type in C language which can store the primitive type of data such as int, float, char, double etc. In its simplest form, an array can be used to represent a list of numbers, or a list of names.
Each element of an array is of same data type and carries the same size.
Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.
Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location (base address).
Name of the array is also a constant pointer to the first element of the array.
Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
Code Optimization: Less code to the access the data.
Ease of traversing: By using the loop, we can retrieve the elements of an array easily.
Ease of sorting: To sort the elements of the array, we need a few lines of code only.
Random Access: We can access any element randomly using the array.
We can use arrays to represent not only simple lists of values but also tables of data in two, or more dimensions. Following are the types of an array:
One-dimensional arrays
Two-dimensional arrays
Multidimensional arrays
A list in which the elements are accessible by the variable name assigned to the list and its subscript is known as a one-dimensional array.
Like any other variable, arrays must be declared before they are used so that the compiler can allocate space for them in memory. The general form of array declaration is:
Type array_name [ size ];
The Type specifies the type of element that will be contained in the array and size indicates the maximum number of elements that can be stored inside the array. For example:
int list [10] ;
declares the list as an array to contain a maximum of 10 integer.
The C language treats character string simply as arrays of characters. For example:
char name [15];
declares the name as a character array (string) variable that can hold a maximum of 15 characters.
We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. For example:
int list [10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; or
int list [ ] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
char name [15] = {‘B’, ‘h’, ‘a’, ‘g’, ‘I’, ‘r’, ‘a’, ‘t’, ‘h’, ‘\0’ }; or
char name [ ] = “Bhagirath”;
The values to the array elements can also be assigned as follows:
list[ 0] = 10;
list [1] = 20;
list [2] = 30;
The values to the array elements can also be initialized at run time as follows:
for( i = 0; i < 5; i++)
{
scanf(%d”, &list[i] );
}
Example:
#include <stdio.h>
int main()
{
int arr[10], i;
printf(“Enter 10 numbers: “);
for( i = 0; i < 9; i++)
scanf(“%d”, &arr[i]);
printf(“\nElements of an Array are: \n”);
for( i = 0; i < 9; i++)
printf(“%d\t”, arr[i]);
return 0;
}
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, flat = 0, item, i;
printf(“Enter the size of an array: “);
scanf(“%d”,&n);
printf(“\nEnter %d elements: ”, n);
for( i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
printf(“\nEnter item to be search: ”);
scanf(“%d”,&item);
for( i = 0 ; i < n; i++)
{
if( arr[i] == item)
{
flag = 1;
break;
}
}
if( flag == 1)
printf(“\nitem is present at %d location”, i);
else
printf(“\nitem is not present in the array”);
return 0;
}
#include <stdio.h>
int main()
{
int i, j, temp;
int arr[5] = {10, 32, 30, 12, 28};
printf("\nBefore sorting array elements are:\n");
for (i = 0; i < 5; i++)
printf("%4d", arr[i]);
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for (i = 0; i < 5; i++)
printf("%4d", arr[i]);
return 0;
}
Output:
Before sorting array elements are:
10 32 30 12 28
After sorting array elements are:
10 12 28 30 32
The 2D array can be defined as an array of arrays. The two-dimensional (2-D) array is also called a matrix. C allows to define table (matrix) of items by using two-dimensional arrays. Each dimension of the array is indexed from zero to its maximum size minus one. Two-dimensional arrays are declared as follows:
Type arr_name [rows] [columns];
Example: int table[2][3];
We think of this table as a matrix consisting of two rows and three columns.
As like the one-dimensional array, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. For example:
int table[2][3] = { 2, 4, 6, 8, 10, 12}; or
int table[2][3] = { {2, 4, 6}, {8, 10, 12} };
Example:
#include <stdio.h>
int main()
{
int i, j;
int arr[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
printf(“\nArray elements are:\n”);
for ( i =0; i < 3; i++)
{
printf(“\n”);
for (j = 0; j < 3; j++)
{
printf(“%4d”, arr[i][j]);
}//end of inner loop
}//end of outer loop
return 0;
}
The subscripts in the definition of a two-dimensional array represent rows and columns. This format maps the way that data elements are laid out in the memory. The elements of all arrays are stored contiguously in increasing memory location, essentially in a single list. If we consider the memory as a row by bytes, with the lowest address on the left and the highest address on the right, a simple array will be stored in memory with the first element at the left end and the last element at the right end. Similarly, a two-dimensional array is stored “row-wise”, starting from the first row and ending with the last row, treating each row like a simple array.
C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The general form of a multi-dimensional array is:
Type arr_name[s1][s2][s3];
Where s1, s2 and s3 are the size of the dimension. For example:
int report[2][2][3];
An array created at compile time by specifying size in the source code has a fixed size and cannot be modified at run time. The process of allocating memory at compile time is known as static memory allocation and the arrays that receive static memory allocation are called static arrays.
In C it is possible to allocate memory to arrays at run time. This feature is known as dynamic memory allocation and the array created at run time are called dynamic arrays. Dynamic arrays are created using pointer variables and memory management functions malloc, calloc and realloc.
The C library supports a large number of string-handling functions that can be used to carry out many of the string manipulations. Following are the most commonly used string-handling functions:
This function counts and returns the number of characters in a string. It takes the form:
len = strlen(str);
Where len is an integer variable, which receives the value of the length of the string str. The argument may be a string constant. For example:
printf( “Total characters = %d”, strlen(“Bhagirath”) );
will print “Total characters = 9”.
The strcpy function works almost like a string-assignment operator. It takes the following form:
strcpy( str1, str2);
and assigns the contents of str2 to str1. str2 may be a character array variable or a string constant. For example, the statement
strcpy(name, “Bhagirath”);
will assign the string “Bhagirath” to the string variable name.
The strcat function joins two strings together. It takes the following form:
strcat (str1, str2);
str1 and str2 are character arrays. When the function strcat is executed, str2 is appended to str1. The string at str2 remains unchanged. For example:
strcpy(name, “Bhagirath”);
strcat(name, “Singh”);
The strcmp function compares two strings identified by the arguments and has a value 0 if they are equal. It they are not, it has the numeric difference between the first nonmatching characters in the strings. It takes the form:
strcmp (str1, str2 );
str1 and str2 may be string variables or string constants. Examples are:
strcmp(name1, name2);
strcmp(name, “Bhagirath”);