Array in c programming language

An array can be defined as a finite ordered set of homogenous elements. By the term 'finite' , we mean that there is a fixed number of elements in an array. By first term 'ordered', we mean that elements of array are arranged in a sequence so that first, second, third, ...., nth element can be identified . The homogenous property of array means that all elements in the array must be of the same type. It is used to handle a large amount of data, without the need to declare many individual variables separately. The array elements are stored in a contiguous memory location(i.e. one after the other). All the array elements must be either of any simple data type like int, float, char, double etc. Or they can be any user defined data type like structures and unions

An array can be one dimensional , two dimensional or multidimensional depending upon the subscript used. A single subscript is required to access an element of one dimensional array and two subscripts are required to access an element of two dimensional array . All the arrays whose elements are accessed using two or more subscripts are called multidimensional arrays. It should be kept in mind that each subscripts is enclosed in square brackets and it should be expressed as a non-negative integer constant or expression.

The concept of array helps us to store group of values under a single name and refer directly to a particular value. This makes the programs very simple and efficient. This concept is present in almost all programming languages including C.

1.One - dimensional array in c programming language

Declaration of one dimensional arrays :Array must be declared before it can be store information. Arrays are declared in same manner as ordinary variables, except each array name must be accompanied by a size specification(i.e. number of elements) enclosed in square brackets following the array name.

The syntax of one dimensional array is,

data_type arrayname [size];

Here, the data_type refers to the data type of the array elements. It may be any of the primitive or derived types. The rule for naming an array are same as that identifiers. The size is a positive valued integer expression that specifies the maximum number of array elements that can be stored in array.

Let us take an example,

int x[5];

Here, int is data type , x is name of an array and 5 is size of array.

For example , consider the following program code:

#include<stdio.h>

#define n 20 /*preprocessor which initialized n to 20 */

main()

{

char x[n]; /*symbolic constant (n) for size specification */

....................

....................

}

Here we are using a symbolic constant n which is initialized a value 20 . By just changing value of n to different values, we can make alteration in the program to run for different set of values.





Storing values in one dimensional array: Array declaration only allocates memory for elements in array . No known value are stored in it yet. They only contain garbage value which is note useful for the program. So you must assign values to array elements before using them. If you want to store values in array, We must either,

1. Assign value to each individual element

2. Read values from the keyword

3. Initialize the elements





Initialization of one dimensional array: Declaration of an array does not automatically result in the initialization of the array elements. It only allocates contiguous memory to an array with each element assigned some garbage value . We can initialize an array by following the array definition an equal to (=) sign followed by comma separated list of values enclosed in braces . The order of assignment of values to array elements will be same as they appear in the initialization list. The syntax for initializing an array is

data_type array_name[size] = {value1, value2, ....... , valuen};

Here , value1 , value2 , ...... , valuen are assigned to first , second and nth element of an array.

For example , the statement

int x[5] = {10,20,30,40,50};

declares an array x of 5 elements and initialize its elements. As we know that individual values must appear in the order in which they should be assigned to individual array elements, therefore the element a[0] is initialize to 10, element a[1] is initialize to 20 and so on till element a[4] is initialize to 50.





Some more examples are :

int marks [20] = {0};

int days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

float percentage[] = {56.5, 67.8, 69.5};

char name [] = {'a','b', 'c', 'd', 'e' };





Points to remember:

1. If numbers of values specified in the initialization statement is less than the maximum size of the array then the remaining elements of the array are initialized to 0 automatically if the array type is numeric and null if the type is char.

2. If the number of values specified in the initialization statement are more than the maximum specified size of the array, then the compiler will be generate an error.

3. If you include an initialization list among with a declaration there must be atleast on initializing value in it, otherwise compiler generate an error message.

4. There is no method to initialize lateral elements without first specifying value for earlier elements.

5. Since whitespace is ignored in C, initializer list must be continued across multiple line.









Accessing the one dimensional array : Once the data has been stored in an array, you can access and use individual array elements in the same manner as you would access and use a single variable of same type. By changing an individual element of an array, You can perform various operations on them such as changing its value, printing its value, assigning value to individual variables and so on. To access a particular element in an array, we specify the array name followed by index number enclosed in square brackets. The syntax for accessing an array element is ,

array_name [index_expression]

The index_expression is simply an integer constant that explicitly specify the index of an element you want to access or any expression that evaluates to integer value during the execution of the program. These values should be correspond to one of the possible index value of an array.





A program of one dimensional to enter the value of five numbers and then print their value?

#include<stdio.h>

#include<conio.>

main()

{

int i, a[5];

clrscr();

printf("enter 5 numbers-------

");

for(i=0;i<5;i++); /reading array elements */

printf("enter number a[%d] : ",i);

scanf("%d",&a[i]);

}

printf("numbers entered are -----");

for(i=0; i<5;i++) /*displaying array elements "/

printf("

\t element [%d] is : %d", i,a[i]);

getch();

}





Output:

enter 5 numbers------

enter number[0] : 10

enter number[1] : 20

enter number[2] : 30

enter number[3] : 40

enter number[4] : 50

numbers entered are -----

elements a[0] is : 10

2 DIMENSIONAL ARRAY in C.



MULTIDIMENSIONAL ARRAY IN C.

THANKS





elements a[1] is : 20elements a[2] is : 30elements a[3] is : 40elements a[4] is : 50In the previous section, you have learned how to use one dimensional array which is used to represent items in a list or sequence of values. However in some situations, you need to store and manipulate data organized in the form of tables. These are two dimensional structures requiring a row and columns designators to identify specific data value.Like one dimensional arrays, two dimensional arrays must also be declared before it is used. The syntax for declaring two dimensional array isHererefers to the datatype of the array elements,is any valid array name that follows the same rules as that of identifiers. Theandare constant expressions yielding positive integer values. The expressionsandspecify the number of rows and columns respectively in the array.For example, the statementdeclares a two dimensional array x containing 2 rows and 3 columns where each element is of type int. As in case of one dimensional array, the rows are numbered from 0 to 1 and columns are numbered from 0 to 2. The product of the number of rows and number of columnsgives the totak number of elements which turns out to be 2 X 3 = 6 elements. As each type element requires 2 bytes of the storage so this two dimensional array x containing 6 elements will require 12 consecutive bytes for storing this elements.Two access a particular element of a two dimensional array we specify the array name followed by a pair of square bracket enclosing row number followed by a pair of another another square brackets enclosing column number. The syntax for accessing an array element isHereandare simply integers constant that explicitly specify the index of an element you want to access, or it can be any expression that evaluates to an integer value. The values should be one of the possible index values for the array. Note that row_index specfies row position and col_index specifies the column position. For example to access the elements of array x and second row and third column, you have to specify. for example the statementJust like one dimensional arrays, the two dimensional arrays can also be initialized . The elements are assigned in such a manner that elements of the first row are initialized first, then elements of the second row and so on. For exampleThe above statements declares x to be a two dimensional array containing 3 rows each of which contains 3 elements. Each set of values that initializes the elements in a row are placed in curly braces and all rows are further placed in another curly brace.In addition to one dimensional and two dimensional arrays, C also allows you to declare three dimensional or larger arrays. An arrays may consist of any number of dimensions subject to the restrictions put by a compiler depending upon the language. The general syntax of specifying a multidimensional array is ,Here thecorresponds to the data type of the array, theis the name of the array andare the consist expression that evaluate to positive value. The syntax to access n dimensional array is ,Whereare the expression that evaluate to positive gives the position of the array element in the ith dimension. For example, the statement,declare x to be 3-dimensional array. The size of the first dimension is 3 , the size of the second dimension is 4 and the size of the third dimension is 5, where each element is of type int.The following program illustrates the concept of three dimensional arrays: