Arrays

Arrays

                Array is a data structure, which provides the facility to store a collection of data of same type under single variable name. Just like the ordinary variable, the array should also be declared properly. The declaration of array includes the type of array that is the type of value we are going to store in it, the array name and maximum number of elements.
               The type may be any valid type supported by C. Array names, like other variable names, can contain only letter, digit and underscore characters. Array names cannot begin with a digit character. I.e., The rule for giving the array name is same as the ordinary variable. The size should be an individual constant. To refer to a particular location or element in the array, we specify the name of the array and the index of the particular element in the array. The index specifies the location of the element in the array. The array index starts from zero. The maximum index value will be equal to the size of the array minus one. The array index can be an integer variable for an integer constant.

One dimensional array in c programming language : ID array

                     The declaration form of one-dimensional array is
 Data_type array_name [size];
                    The following declares an array called ‘numbers’ to hold 5 integers and sets the first and last elements. C arrays are always indexed from 0. So the first integer in ‘numbers’ array is numbers[0] and the last is numbers[4].
int numbers [5];
numbers [0] = 1;       // set first element
numbers [4] = 5;       // set last element
                This array contains 5 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in square brackets ([]). The first element in every array is the zeroth element.Thus, the first element of array ‘numbers’is referred to as numbers[ 0 ], the second element of array ‘numbers’is referred to as numbers[ 1 ], the fifth element of array ‘numbers’is referred to as numbers[ 4 ], and, in general, the n-th element of array ‘numbers’is referred to as numbers[ n - 1 ].
Example:
One Dimensional l array
                  It's a very common error to try to refer to non-existent numbers[ 5], element. C does not do much hand holding. It is invariably up to the programmer to make sure that programs are free from errors. This is especially true with arrays. C does not complain if you try to write to elements of an array which do not exist!
For example: If you wrote:
numbers[ 5 ], = 6;
                   C would happily try to write 6 at the location which would have corresponded to the sixth element, had it been declared that way. Unfortunately this would probably be memory taken up by some other variable or perhaps even by the operating system. The result would be either:
  • The value in the incorrect memory location would be corrupted with unpredictable consequences.
  • The value would corrupt the memory and crash the program completely!
                      The second of these tends to be the result on operating systems with proper memory protection. Writing over the bounds of an array is a common source of error. Remember that the array limits run from zero to the size of the array minus one.
Out of bounds access
I would be doing well, that really shows the range of valid indices, please check often. 10 When the number of elements, all the indices of 10 or more is illegal. Also, whether any size of negative indices is also illegal.
These errors are detected at compile time is not. It is an error at run time. However, there is no guarantee that the error reliably. Sometimes works correctly (seems to be moving.) This is the "luck ran" instead of "bad luck worked" so, please, as no doubt.
                   The best way to see these principles is by use of an example, so load the program  and display it on your monitor.
#include <stdio.h>
#include <conio.h>
int main( )
       {
       char name[7]; /* define a string of characters */
       name[0] = 'A';
       name[1] = 's';
       name[2] = 'h';
       name[3] = 'r';
       name[4] = 'a';
       name[5] = 'f';
       name[6] = '\0'; /* Null character - end of text */
       name[7] = ‘X’;
       clrscr();
       printf("My name is %s\n",name);
       printf("First letter is %c\n",name[0]);
       printf("Fifth letter is %c\n",name[4]);
       printf("Sixth letter is %c\n",name[5]);
       printf("Seventh letter is %c\n",name[6]);
       printf("Eight letter is %c\n",name[7]);
       getch();
       return 0;
      }
                 The elements of an array can also be initialized in the array declaration by following the declaration with an equal sign and a comma-separated list (enclosed in braces) of initializers.
                The following program initializes an integer array with five values and prints the array.
#include <stdio.h>
#include <conio.h>
int main()
     {
     int numbers[]={1,2,3,4,5};
     int i;
     clrscr();
     printf("Array elements are\n");
     for(i=0;i<=4;i++)
              printf("%d\n",numbers[i]);
     getch();
     return 0;
    }
                    If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n could have been initialized to zero with the declaration
int n[ 10 ] = { 0 };
which explicitly initializes the first element to zero and initializes the remaining nine elements to zero because there are fewer initializers than there are elements in the array. It is important to remember that arrays are not automatically initialized to zero. The programmer must at least initialize the first element to zero for the remaining elements to be automatically zeroed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at run time for automatic arrays.
There are more complex uses of arrays, which I will address later along with pointers.

2 dimensional array

                      Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is
Data_type    Array_name [row size][column size];
                     The type may be any valid type supported by C. The rule for giving the array name is same as the ordinary variable. The row size and column size should be an individual constant.
                      The following declares a two-dimensional 3 by 3 array of integers and sets the first and last elements to be 10.
int matrix [3][3];
matrix[0][0] = 10;
matrix[2][2] = 10;
                    The following Figure illustrates a two dimensional array, matrix. The array contains three rows and tree columns, so it is said to be a 3-by-3 array. In general, an array with m rows and n columns is called an m-by-n array.

  [0] [1] [2]
[0] 10    
[1]      
[2]     10
                      Every element in array matrix is identified by an element name of the form matrix[ i ][ j ]; matrix is the name of the array, and i and j are the subscripts that uniquely identify each element in matrix . Notice that the names of the elements in the first row all have a first subscript of 0; the names of the elements in the third column all have a second subscript of 2.
                  In the case of Two-dimensional array, during declaration the maximum number of rows and maximum number of column should be specified for processing all array elements.       
                    The implementation of the array stores all the elements in a single contiguous block of memory. The other possible implementation would be a combination of several distinct one-dimensional arrays. That’s not how C does it. In memory, the array is arranged with the elements of the rightmost index next to each other. In other words, matrix[1][1] comes right before matrix[1][2] in memory.
The following array:

  [0] [1] [2]
[0] 1 2 3
[1] 4 5 6
[2] 7 8 9
would be stored:

1 2 3 4 5 6 7 8 9
Example:
2D array

No comments: