Book Your slot
X
ONLINE BOOKING
BOOK NOW
OFFLINE BOOKING
Call or WhatsApp 7993732682 (WhatsApp Now), 9177341827 (WhatsApp Now)
search
Menu Login home
  • Questions

  • Library

  • University Updates

  • Informatives

  • Technology Lines

  • Training & Internships

  • X
    Menu
  • Home
  • Privacy Policy
  • Legal Disclaimer
  • Terms & Conditions
  • Return Policy
  • About Us
  • Need any help?? write to us at

    support@engineershub.co

    Follow Us

    X
    LOGIN
    Login to access posts, links, updates, question papers, materials, one liners!
    Use Your Email Address/Mobile and Password to Login
    Forgot Password?
    Not a member? Sign Up
    LOGIN WITH EMAIL/MOBILE
    Forgot Password?
    Go Back
    FORGOT PASSWORD
    Go Back
    RESET PASSWORD
    Go Back
    Continue with LinkedIn
    OR
    Fill Up a Simple Form
    Already a Member? Login
    SIGN UP
    Fill all the below details correctly and click on Next
    Go Back
    What is an array? How to declare and initialize arrays? Explain with examples? - EngineersHub
    Go Back
    Question
    Ruchitha
    4 years ago
    1 Answer(s) posted Write an answer 8908
    Answer
    Read Mode
    Answer posted by Vinod Raj
    4 years ago

    Array:-

    An array is defined as an ordered set of similar data items. All the data items of an array are stored in consecutive memory locations in RAM. The elements of an array are of same data type and each item can be accessed using the same name.

    Declaration of an array:-

    We know that all the variables are declared before they are used in the program. Similarly, an array must be declared before it is used. During declaration, the size of the array has to be specified. The size used during declaration of the array informs the compiler to allocate and reserve the specified memory locations.

    Syntax:- data_type array_name[n];

    where, n is the number of data items (or) index(or) dimension.

    0 to (n-1) is the range of array.

    Ex: int a[5];

              float x[10];

    Initialization of Arrays:-

    The different types of initializing arrays:

    1. At Compile time

    (i) Initializing all specified memory locations.

    (ii) Partial array initialization

    (iii) Initialization without size.

    (iv) String initialization.

    2. At Run Time

    1. Compile Time Initialization:  We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. The general form of initialization of arrays is 

    type array-name[size]={ list of values};

    (i) Initializing all specified memory locations:- Arrays can be initialized at the time of declaration when their initial values are known in advance. Array elements can be initialized with data items of type int, char etc.

    Ex:- int a[5]={10,15,1,3,20};

    During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and all these locations are initialized as shown in figure.

    Fig: Initialization of int Arrays

    Ex:- int a[3]={9,2,4,5,6}; //error: no. of initial vales are more than the size of array.

    (ii) Partial array initialization:- Partial array initialization is possible in c language. If the number of values to be initialized is less than the size of the array, then the elements will be initialized to zero automatically. 

    Ex:- int a[5]={10,15};

    Even though compiler allocates 5 memory locations, using this declaration statement; the compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically initialized to 0's by compiler as shown in figure.

    Fig: Partial Array Initialization

    Initialization with all zeros:-

    Ex:- int a[5]={0};

    (iii) Initialization without size:-

    Consider the declaration along with the initialization.

    Ex:- char b[]={'C','O','M','P','U','T','E','R'};

    In this declaration, eventhough we have not specified exact number of elements to be used in array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8 automatically. The array b is initialized as shown in figure.

    Fig: Initialization without size

    Ex:- int ch[]={1,0,3,5} // array size is 4

    (iv) Array initialization with a string: -Consider the declaration with string initialization.

    Ex:- char b[]="COMPUTER";

    The array b is initialized as shown in figure.

    Fig: Array Initialized with a String

    Even though the string "COMPUTER" contains 8 characters, because it is a string, it always ends with null character. So, the array size is 9 bytes (i.e., string length 1 byte for null character).

    Ex:-                                                                              char b[9]="COMPUTER"; // correct

    char b[8]="COMPUTER"; // wrong

    2. Run Time Initialization

    An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays.

    Ex:- scanf can be used to initialize an array.

    int x[3];

    scanf(%u201C%d%d%d%u201D,&x[0],&x[1],&x[2]);

    The above statements will initialize array elements with the values entered through the key board.

    (Or)

    for(i=0;i<100;i=i 1)

    {

    if(i<50)

    sum[i]=0.0;

    else

    sum[i]=1.0;

    }

    The first 50 elements of the array sum are initialized to 0 while the remaining 50 are initialized to 1.0 at run time.

    261
    X
    What is an array? How to declare and initialize arrays? Explain with examples?
    X
    What is an array? How to declare and initialize arrays? Explain with examples?

    Array:-

    An array is defined as an ordered set of similar data items. All the data items of an array are stored in consecutive memory locations in RAM. The elements of an array are of same data type and each item can be accessed using the same name.

    Declaration of an array:-

    We know that all the variables are declared before they are used in the program. Similarly, an array must be declared before it is used. During declaration, the size of the array has to be specified. The size used during declaration of the array informs the compiler to allocate and reserve the specified memory locations.

    Syntax:- data_type array_name[n];

    where, n is the number of data items (or) index(or) dimension.

    0 to (n-1) is the range of array.

    Ex: int a[5];

              float x[10];

    Initialization of Arrays:-

    The different types of initializing arrays:

    1. At Compile time

    (i) Initializing all specified memory locations.

    (ii) Partial array initialization

    (iii) Initialization without size.

    (iv) String initialization.

    2. At Run Time

    1. Compile Time Initialization:  We can initialize the elements of arrays in the same way as the ordinary variables when they are declared. The general form of initialization of arrays is 

    type array-name[size]={ list of values};

    (i) Initializing all specified memory locations:- Arrays can be initialized at the time of declaration when their initial values are known in advance. Array elements can be initialized with data items of type int, char etc.

    Ex:- int a[5]={10,15,1,3,20};

    During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and all these locations are initialized as shown in figure.

    Fig: Initialization of int Arrays

    Ex:- int a[3]={9,2,4,5,6}; //error: no. of initial vales are more than the size of array.

    (ii) Partial array initialization:- Partial array initialization is possible in c language. If the number of values to be initialized is less than the size of the array, then the elements will be initialized to zero automatically. 

    Ex:- int a[5]={10,15};

    Even though compiler allocates 5 memory locations, using this declaration statement; the compiler initializes first two locations with 10 and 15, the next set of memory locations are automatically initialized to 0's by compiler as shown in figure.

    Fig: Partial Array Initialization

    Initialization with all zeros:-

    Ex:- int a[5]={0};

    (iii) Initialization without size:-

    Consider the declaration along with the initialization.

    Ex:- char b[]={'C','O','M','P','U','T','E','R'};

    In this declaration, eventhough we have not specified exact number of elements to be used in array b, the array size will be set of the total number of initial values specified. So, the array size will be set to 8 automatically. The array b is initialized as shown in figure.

    Fig: Initialization without size

    Ex:- int ch[]={1,0,3,5} // array size is 4

    (iv) Array initialization with a string: -Consider the declaration with string initialization.

    Ex:- char b[]="COMPUTER";

    The array b is initialized as shown in figure.

    Fig: Array Initialized with a String

    Even though the string "COMPUTER" contains 8 characters, because it is a string, it always ends with null character. So, the array size is 9 bytes (i.e., string length 1 byte for null character).

    Ex:-                                                                              char b[9]="COMPUTER"; // correct

    char b[8]="COMPUTER"; // wrong

    2. Run Time Initialization

    An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays.

    Ex:- scanf can be used to initialize an array.

    int x[3];

    scanf(%u201C%d%d%d%u201D,&x[0],&x[1],&x[2]);

    The above statements will initialize array elements with the values entered through the key board.

    (Or)

    for(i=0;i<100;i=i 1)

    {

    if(i<50)

    sum[i]=0.0;

    else

    sum[i]=1.0;

    }

    The first 50 elements of the array sum are initialized to 0 while the remaining 50 are initialized to 1.0 at run time.

    EngineersHub Logo
    x
    Loading...