Showing posts with label One Dimensional Array. Show all posts
Showing posts with label One Dimensional Array. Show all posts

Tuesday 27 March 2012

One Dimensional Array



A list of items can be given a variable name using only one subscript and such a variable is called single dimensional array.

Array declaration

Datatype variable_name[size];

Where Datatype specifies the type of elements that will be contained in the array such as integer,float,character. The size indicates the maximum number of elements that can be stored inside the array.
Ex,
int no[10];

è    This declaration instruct the c compiler to associate 10 memory cells with the name no.
è    Individual elements of the array are accessed using the subscripted variables. A subscripted variable consist of array name and the position of the element in the array. for Ex, no[0],no[2].....no[9].The subscript of array starts from 0th element. so here no[0] is first element and no[9] is last element.
è    The computer reserves 10 storage location as given below:
100
no[0]
102
no[1]
104
no[2]
106
n0[3]
108
no[4]
110
no[5]
112
no[6]
114
no[7]
116
no[8]
118
no[9]




è    The values to the array elements are assigned as:
no[0]=10
no[1]=20
no[2]=30
no[3]=40
no[4]=50
no[5]=60
no[6]=70
no[7]=80
no[8]=90
no[9]=100

è    The elements of an array can be accessed as:
Ex, x=no[0]+2;
      sum=no[1]+no[2];
      no[5]=no[6]+no[7];
                  
è     The  c language treats character strings simply as arrays of characters. The size in a character string represents the maximum number of characters that the string can hold
Ex, char name[5];

Suppose the name read from keyboard is Shiv then  the each character of string is treated as an element of the array name and stored in memory as
S
h
i
v
\0
0
1
2
3
4
         


è    When compiler sees a character string , it terminates it with additional \0 (null) character. So when declaring an character array must need to keep extra one space for \0 (null character)

Initialization of one dimensional array

The array elements can be initialized same way as the ordinary variable. It is initialized at the time of its declaration itself. It can be initialized at either of the following stages:
-         At compile time
-         At run time

Compile time initialization
è    The general form of compile time initialization is as below;
                   type array-name[size] = { list of values};
Here, specifies commas separate the values in the list.
Ex,                   int id[3] = {0,0,0);
Here, It will declare the variable id as an array of 3 integer elements and assign 0 to each element.
è    If the size of the array is more than the elements in the braces then the rest elements are automatically initialized to zero.
Ex,                 int no[5]={1,2,3,4};
Here, it will initialize        the first four elements to 1,2,3,4 respectively and remaining one element to 0.
è    The size could be omitted in the initializations, in that case the compiler allocates required space to the array. For example
                 int count[] = {3,6,8.5};
è    The character arrays may be initialize in same manner
                 Ex,                 char name[] = {'K', 'R','I',’S’,’H’,’N’,’A’,’\0’};
                                      char name[]=”KRISHNA”;

Run time initialization

è    An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays.
Ex, int no[3];
     scanf(“%d %d %d”,&no[0],&no[1],&no[2]  );

Ex,
void main()
{
   int no[5],i;

   for(i=0;i<5;i++)
    {
        printf(“\n Enter value of number %d”,i+1);
   scanf(%d”,&no[i]);
         }
         printf(“\n output “);
    for(i=0;i<5;i++)
    {
        printf(“\n%d”,no[i]);
         }

     }      

Here it reads the five number  form keyboard and display on screen.

Posted By : Ruchita Pandya