Create FREE 'HowTo' Videos with MyGuide

Array



Pass Quiz and Get a Badge of Learning



Content "filtered", Please subscribe for FULL access.


Chapter 2 : Array


Array arrow_upward


  • An array is a simplest memory data structure.
  • An Array is a systematic arrangement of elements, usually in rows and columns.
  • Each element is identified by a specific index.
  • Contents of an array are stored in contiguous memory cells.
  • All the elements must be of the same type.
  • Array elements can be accessed directly.

  • Sample Program Using C to Display Array Elements with their Addresses arrow_upward

    PROGRAM:
    #include<stdio.h>
    #include<conio.h>
    void main ()
    {
     int Num[6]={1,2,3,2,5,6};
     printf("\n Num[0] = %d Address: %u",Num[0],&Num[0]);
     printf("\n Num[1] = %d Address: %u",Num[1],&Num[1]);
     printf("\n Num[2] = %d Address: %u",Num[2],&Num[2]);
     printf("\n Num[3] = %d Address: %u",Num[3],&Num[3]);
     printf("\n Num[4] = %d Address: %u",Num[4],&Num[4]);
     printf("\n Num[5] = %d Address: %u",Num[5],&Num[5]);
     getch();
    }
    
    OUTPUT:
    Num[0] = 1 Address: 65514
    Num[1] = 2 Address: 65516
    Num[2] = 3 Address: 65518
    Num[3] = 2 Address: 65520
    Num[4] = 5 Address: 65522
    Num[5] = 6 Address: 65524
    
    DESCRIPTION:
  • In the output of the program, elements and their addresses are displayed.
  • Recall that integer requires two bytes in memory.
  • The memory locations displayed have a difference of two.
  • From the above program, it is clear that array elements are stored in successive memory locations.
  • Below figure shows the memory location and the stored values.

  • Sample Program Using C to Initialize an Array arrow_upward

    PROGRAM:
    #include<stdio.h>
    #include<conio.h>
    void main ()
    {
    int Num[6]={0},j;
    printf("The elements in array after initialization ");
    for(j=0;j<6;j++)
    printf("\n Num[%d]=%d",j,Num[j]);
    getch();
    }
    
    OUTPUT:
    The elements in array after initialization
    Num[0]=0
    Num[1]=0
    Num[2]=0
    Num[3]=0
    Num[4]=0
    Num[5]=0
    

    DESCRIPTION:

  • In this program the first element is initialized with zero and an array, Num [6] is declared.
  • The compiler automatically initializes all the elements with zero. Using the ‘for loop’ the contents of an array are displayed and we can see that all are zeroes.

  • One-Dimensional array arrow_upward


  • A One-dimensional array is a linear array.
  • Accessing its elements involves a single subscript which can either represent a row or column index.

  • Two-Dimensional Arrays arrow_upward


  • A Two-dimensional array is array of arrays.
  • Arrays that have two indices for representing rows and columns.
  • By convention, the first subscript is understood to be for rows and the second for columns.

  • Example:

  • Consider an integer array with 3 rows and 3 columns.
  • This array can hold up to 9 integer values, the product of the values used to declare the array.
  • The following table illustrates how the subscripts are specified for this array.

  • Column

     Row

    0

    1

    2

    0

    [0][0]

    [0][1]

    [0][2]

    1

    [1][0]

    [1][1]

    [1][2]

    2

    [2][0]

    [2][1]

    [2][2]


    Multi-Dimensional Array arrow_upward


  • A Multi-dimensional Array of dimension n is a collection of items which is accessed via n subscript expressions.

  • Array Advantages arrow_upward


  • Allows sequential access of memory cells.
  • Retrieve or store data with names.
  • Easy to implement.
  • Simplifies program writing and reading.

  • Array Disadvantages arrow_upward


  • Cannot store heterogeneous items.
  • Arrays lack ability to dynamically allocate memory.
  • Searching unsorted arrays is not efficient.

  • Thank You from Kimavi arrow_upward


  • Please email us at Admin@Kimavi.com and help us improve this tutorial.


  • Mark as Complete => Receive a Certificate in Data-Structure


    Kimavi Logo

    Terms and conditions, privacy and cookie policy | Facebook | YouTube | TheCodex.Me | Email Kimavi


    Kimavi - A Video Learning Library { Learning is Earning }

    Get Ad Free Learning with Progress Report, Tutor Help, and Certificate of Learning for only $10 a month



    All videos on this site created using MyGuide.

    Create FREE HowTo videos with MyGuide.