Array in c programming language.

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-------\n");
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("\n\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
elements a[1] is : 20
elements a[2] is : 30
elements a[3] is : 40
elements a[4] is : 50

2 DIMENSIONAL ARRAY in C.

In 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.

DECLARING TWO DIMENSIONAL ARRAYS
Like one dimensional arrays, two dimensional arrays must also be declared before it is used. The syntax for declaring two dimensional array is
                               data_type   arrayname[rowsize] [colsize] ;
                     Here data_type refers to the datatype of the array elements, arrayname is any valid array name that follows the same rules  as that of identifiers. The rowsize and colsize are constant expressions yielding positive integer values. The expressions rowsize and colsize specify the number of rows and columns respectively in the array.
                   For example, the statement
                   int  x[2] [3] ;
                  declares 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.

ACCESSING TWO DIMENSIONAL ARRAYS
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 is
                   arrayname[row_index] [col_index] ;
                   Here row_index  and col_index are 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 x[1] [2]. for example the statement
          x[1] [2]   = 60 

PROGRAM TO INPUT AND DISPLAY ELEMENTS OF A TWO DIMENSIONAL ARRAY
#include<stdio.h>
#include<conio.h>
main( )
{
int i,j,no_of_rows, n;
int a [10] [10];
clrscr ( );
primtf ("Entet no.of rows of the 2D array(<10) : ");
scanf ("%d",&no_of_rows);
printf( "Enter no. of columns of the 2D array (<10): ");
scanf ("%d",&n);
printf("------Enter row wise %d integers------/n",no_of_rows*n);
for (i=0;i<no_of_rows;i++)
     for(j=0;j<n;j ++)
     {
        scanf ("%d", &a [i] [j] );
}
printf("-------Elements you entered are-----/n");
for(i=0; i<no_of_rows;i++)
{  
   for(j=0; j<n; j++)
  {
      printf("%d\t",a[i] [j]);
   }
      printf("\n");
   }
      getch( );
}

OUTPUT:     Enter no. of  rows of the 2D array(<10)         :2
                    Enter no.of  columns of the 2D array(<10)    :3
                    ------Elements you entered are------
                    2    9    23    -4    55    98
                    ------Elements you entered are------
                    2         9       23
                   -4      55       98


INITIALIZATION OF TWO DIMENSIONAL ARRAY
Just 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 example
 int x[3] [3] ={
                         {11,12,13}
                          {14,16,17}
                          {20,21,22}
                       };
The 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.

Let us see a program to demonstrate how two dimensional arrays are initialized?
#include<stdipo.h>
#include<coni.h>
main()
{
int a[2] [3] = {{10,20,30},
                         {15,25,40}};
int i,j;
clrscr();
printf("------elements you initialized are ------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i] [j]);
}
printf("\n");
}
getch();
}

OUTPUT  :       ------elements you initialized are ------
                          10   20   30
                           15   25   40




MULTIDIMENSIONAL ARRAY IN C.

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 ,
  <datatype><arra_name>[size1][size2]..........[sizen];
Here the datatype corresponds to the data type of the array, the array_name is the name of the array and size1,size2,.......sizen are the consist expression that evaluate to positive value. The syntax to access n dimensional array is ,
   array_name [indexexp1][indexexp2].......[indexexpn];
Where indexexp1, indexexp2,......,indexexpn are the expression that evaluate to positive gives the position of the array element in the ith dimension. For example, the statement,
   int  x[3] [4] [5];
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:
Program to display all the elements of 3- dimensional array?
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
int 1[2] [2] [2] = {{{1,2},{3,4},{5,6},{7,8}}};
clrscr();
printf("------entered elements are ------\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
printf("%d\t", a[i] [j] [k]);
printf("\n");
}
printf("------\n");
}
getch();
}

OUTPUT:   ------entered elements are ------
                   1    2
                   3     4
                  ------
                   4     5 
                   6     7
                  ------
               


               
   
 


          THANKS



   

No comments

Powered by Blogger.