Array


  Arrays 

Arrays are a collection of items (i.e. ints, floats, chars) whose memory is allocated in a contiguous block of memory.  

Why need arrays???  
    As an example, suppose you want to read in five numbers and print them out in reverse order. You could do it the hard way as:   main() 
 int al,a2,a3,a4,a5;  scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);  
 printf("%d %d %d %d %d'',a5,a4,a3,a2,a1);  
}   
What happned if it is  numbers or more…? 

Array 
• a name like a[i] where i is a variable which specifies which particular value we are working with.  
• In the case of C you have to declare an array before you use it - in the same way you have to declare any sort of variable.     For example, int a[5];
• Declares an array called a with five elements.
• But, the first element is a[0] and the last a[4]  

Array Declaration
  
type array[size] 
• declares an array of the specified type and with size elements.  
• The first array element is array[0] and the last is array[size-1]. 
• Using an array, the problem of reading in and printing out a set of values in reverse order becomes simple: 
main()   
{  
 int a[5], i;   
for(i =0;i < 5; i++)    
scanf("%d",&a[i]);  
 for(i =4;i> =0;i--)   
 printf("%d",a[i]);   

One-dimensional arrays 

An array stores a set of data of the same type. 
Declaration: int a[size]; /* space for a[0].... a[size-1]           allocated. */ 

Usage: b=a[expr];  /* expr = integral expression    between 0 and size-1 */ 

Note:  indexing of elements always starts at zero, so the first entry is  a[0] not a[1].   The index is referred to as a subscript.  

It is sometimes useful to define the size of an array as a symbolic constant:  
#define NPOINTS 100 
 int a[NPOINTS]; 
The array is then typically processed with a for loop:  
for (i = 0; i < NPOINTS; i++)   
sum += a[i]; 
Changes in size are then taken care of all in one place. 

Arrays can be initialized: 
float a[5]  = { 0.0, 1.0, 2.0, 3.0, 4.0 }; 
If some but not all elements are initialized, the others are set to zero.  External and static arrays are initialized to zero by default anyway. Automatic arrays are not, although some compilers do so. 

 •Arrays can be declared without size! The size is then set to the number of initializers: 
 float a[]  = {0.0, 1.0, 2.0, 3.0, 4.0}; 
is equivalent to the above.   

•Strings are treated as arrays of character and can be initialized using string constants:
  char s[] = “abc”; 
is equivalent to (but a lot more convenient than) 
 char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; 
The \0 at the end here is the “end-of-character-string” symbol. 

Arrays :: Multidimensional  

• Multidimensional arrays are row major.  
• In other words, the first bracket specifies number of rows. Some examples of multidimensional array declarations:  
• int igrid[2][3] = { {0, 1, 2}, {3, 4, 5} }; 
• int igrid[2][3] = { 0, 1, 2, 3, 4, 5 }; 
• int igrid[ ][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9} }; 
int igrid[ ][2];
• The first three examples are valid, the last one is not. 

Limitations  

Some limitations or "problems." 
• No Array Out of Bounds Checking.   
For example:
int ia[2] = {0, 1}; printf("%d ", ia[2]);
The above code would default, because you are trying to look at an area of memory not inside the array memory allocation. 
• Array Size Must be Constant or Known at Compile-time.    

No comments

Powered by Blogger.