Search This Blog

Friday, 15 March 2013

Single Dimensional Array



 Single/One Dimensional Array

An  array in which all the elements  are stored in a unidirectional order and using one subscript to access the elements are called as one Dimensional array.
Syntax
<Data type>  <array variable>[SIZE]        
Ex: int a[5];                                                    
Memory Diagram
In the above Example the array a is Declared as Integer Array  and it can store 5 integer values each of which occupies 2 Bytes .Since total Array occupies (2*5)10 Bytes of Memory as shown below
    a --------> Array name      






a[0]    a[1]     a[2]    a[3]   a[4]  --------> Array index
                                           
NOTE
1.Subscript is a non negative integer enclosed in [ ] placed immediately after 
 the array name without  any space.
2.In Arrays by default array Index starts with 0.
3.Total array size(in bytes)= Length of array * size of data type
Array Initialization
An array variable can be initialized with a element as given below.
Syntax
Data type  arrayname={value1,value2, value3,valu4……………..value n}
Example: 
 int a[5]={1,2,3,4,5};
In this example first value stored in First Block, second value stored in second block and so on ….. as shown below.
a[0]=1 ; a[1]=2 ; a[2]=3  ;a[3]=4;  a[4]=5.


1
2
3
4
5
                

 a[0]      a[1]      a[2]     a[3]        a[4]

Example
              \* sample array*\
 #include<stdio.h>
#include<conio.h>
Void main()
{
        int a[4]={1,2,3,4};
        clrscr();
        printf(“\n a[0]=%d”,a[0]);
        Printf(“\n a[1]=%d”,a[1]);
        Printf(“\n a[2]=%d”,a[2]);
        Printf(“\n a[3]=%d”,a[3]);
        Printf(“\n a[4]=%d”,a[4]);
  }

 
Explanation:

in the above  program , array a[4] is declared in the memory with 5 elements and the elements are finalized with values {1,2,3,4,.to access these elements and print on screen we use printf () function. First printf() prints a [0] value i.e 1,
a [1] is 2 and so on…..that is shown below.

OUTPUT
1
2
3
4
 
 

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code