An array is a collection of similar datatypes of data items stored at contiguous memory locations.
Arrays are the derived data type and it can store the primitive type of data such as int, char, double, float, etc. But you can't store different types in a single array.
Arrays are a way to store a list of items. Each slot of the array holds an individual element, and you can place elements into or change the contents or those slots as you need to.
Each array element(i.e., each individual data item) is referred to by specifying the array name followed by its subscript enclosed in square brackets[].
The subscript indicates the position of the particular element with respect to the rest of the elements. The subscript must be a non negative integer.
We can declare an array in C using subscript operator.
data_type array_name[array_size];
An array is declared in the same manner as ordinary variables, except that each array name must be accompanied by a size specification. This is necessary because the compiler will have to know how much memory to reserve for this array.
An array can be initialized when declared by specifying the values of some or all of its elements. Arrays can be initialized at the time of declaration when their initial values are known in advance.
The values to initialize an array must be constants never variables or function calls. The array can be initialized as follows:
int array[5] = {4,5,7,2}; float x[6] = {0,2.5,0,0.50,0,0};
Code Optimization: It makes the code optimized; we can retrieve or sort the data easily.
Random access: We can get any data located at any index position.
Less amount of code: Using array we can aggregate N variables of same data type in a single data structure. Otherwise we have to declare N individual variables.
Easy access of elements: We can access any element of array using array name and index. We can access all elements serially by iterating from index 0 to size-1 using a loop.
value of arr[0] is 10 value of arr[1] is 20 value of arr[2] is 30 value of arr[3] is 40 value of arr[4] is 50
C as a language provides for arrays of arbitrary dimensions. A two dimensional array of size m rows by n columns is declared.
We can declare an array in C using subscript operator.
data_type array_name[rows][columns];
Elements in a two dimensional array can be accessed by means of a row and column. The row subscript generally is specified before the column subscript. Fore example, twodim[1][3]; will access the element in row number 1(the second row) and in a column number 3 (fourth) f the array.
//4 rows 3 column int arr[4][3]={{1,2,3},{3,4,5},{6,7,8},{9,0,1}}; //2 rows 3 column int arr[2][3]={{1,2,3},{4,5,6}};
The values can also be initialized by forming groups of initial values enclosed within braces. The values within an inner pair of braces will be assigned to the elements of a row.
int arr[3][4]={{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
Note: While Initializing a two dimensional array it is necessary to mention the second (column) dimension, wherease the first dimension (row) is optional. Thus the declarations given below are perfectly acceptable.
int arr[2][3]={12,13,24,23,41,14};
value of arr[0] [0] is 10 value of arr[0] [1] is 20 value of arr[1] [0] is 30 value of arr[1] [1] is 40
Enter number of rows of the matrix: 2 Enter number of columns of the matrix: 3 Enter 6 elements of the matrix: 1 2 3 4 5 6 Elements of the matrix you have entered are: 1 2 3 4 5 6