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
    How can we declare and initialize 2D arrays? Explain with examples.? - EngineersHub
    Go Back
    Question
    Naseem Shaik
    3 years ago
    1 Answer(s) posted Write an answer 5324
    Answer
    Read Mode
    Answer posted by Vinod Raj
    3 years ago

    An array consisting of two subscripts is known as a two-dimensional array. These are often known as an array of the array. In two-dimensional arrays, the array is divided into rows and columns. These are well suited to handle a table of data. In a 2-D array, we can declare an array as :

    Declaration:-

    Syntax: data_type array_name[row_size][column_size];

    Ex:- int arr[3][3];

    where the first index value shows the number of the rows and the second index value shows the number

    of the columns in the array.

    Initializing two-dimensional arrays:

    Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

    Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row. 

    above statement can also be written as

    int a[2][3] = {{ 0,0,0},{1,1,1}};

    by surrounding the elements of each row by braces.

    We can also initialize a two-dimensional array in the form of a matrix as shown below

    int a[2][3]={

    {0,0,0},

    {1,1,1}

    };

    When the array is completely initialized with all values, explicitly we need not specify the size of the first dimension.

    Ex: int a[][3]={

    {0,2,3},

    {2,1,2}

    };

    If the values are missing in an initializer, they are automatically set to zero.

    Ex: int a[2][3]={

    {1,1},

    {2}

    };

    Will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero.

    Users Joined

    mahek thakur
    8 months ago
    sowmya
    8 months ago
    biraja
    9 months ago
    Diana R
    9 months ago
    P Annapoorani
    9 months ago
    X
    How can we declare and initialize 2D arrays? Explain with examples.?
    X
    How can we declare and initialize 2D arrays? Explain with examples.?

    An array consisting of two subscripts is known as a two-dimensional array. These are often known as an array of the array. In two-dimensional arrays, the array is divided into rows and columns. These are well suited to handle a table of data. In a 2-D array, we can declare an array as :

    Declaration:-

    Syntax: data_type array_name[row_size][column_size];

    Ex:- int arr[3][3];

    where the first index value shows the number of the rows and the second index value shows the number

    of the columns in the array.

    Initializing two-dimensional arrays:

    Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

    Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row. 

    above statement can also be written as

    int a[2][3] = {{ 0,0,0},{1,1,1}};

    by surrounding the elements of each row by braces.

    We can also initialize a two-dimensional array in the form of a matrix as shown below

    int a[2][3]={

    {0,0,0},

    {1,1,1}

    };

    When the array is completely initialized with all values, explicitly we need not specify the size of the first dimension.

    Ex: int a[][3]={

    {0,2,3},

    {2,1,2}

    };

    If the values are missing in an initializer, they are automatically set to zero.

    Ex: int a[2][3]={

    {1,1},

    {2}

    };

    Will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero.

    EngineersHub Logo
    x
    Loading...