• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Monday, April 11, 2016

    Declaration of one dimensional and two dimensional array in C


    Array :

    An array is a collection of two or more adjacent memory locations containing same types of data. These data are the array elements. The individual data items can be characters, integers, floating-point numbers, etc. However, they must all be of the same type and the same storage class.

    One dimensional array:

    The array can be declared as :
    int x[15];                                  x is an 15 element integer array
    char name[25];                        name is a 25 element character array

    For more-->

    int marks [5] ={ 85, 79, 60, 87, 70 };


              85            79             60                 87           70
    marks [0]      marks [1]   marks [2]   marks [3]    marks [4]

                         Fig 1.5 : the marks array after initialization


    Some more declarations of arrays with initial values are given below :
    char vowels [ ] = { ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ };
    int age [ ] = { 16, 21, 19, 5, 25 }
    In the above case, compiler assumes that the array size is equal to the number of elements enclosed in the curly braces. Thus, in the above statements, size of array would automatically assumed to be 5. If the number
    of elements in the initialized list is less than the size of the array, the rest of the elements of the array are initialized to zero.


    Declaration of two dimensional array:

    Two dimensional arrays are declared the same way that one dimensional arrays. For example,
    int matrix [ 3 ] [ 5 ]
    is a two dimensional array consisting of 3 rows and 5 column for a total of 20 elements. Two dimensional array can be initialized in a manner analogous to the one dimensional array :

    int matrix [ 3 ] [ 5 ] = {
                                        { 10, 5, -3, 9, 2 },
                                        { 1 , 0, 14, 5, 6 },
                                        { -1, 7, 4, 9, 2 }
                                     }





















    The above statement can be written as follows :
    int matrix [ 3 ] [ 5 ] = { 10,5,-3,9,2,1,0,14,5,6,-1,7,4,9,2 }

    For any further problem, please comment below...




    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel