Declaration and initialization of one dimensional and two-dimensional arrays

11/15/2023
All Articles

#Declaration _initialization_one_dimensional_and_two-dimensional_arrays

Declaration and initialization of one dimensional and two-dimensional arrays

Declaration and initialization of one dimensional and two-dimensional arrays

In C, an array is a way to combine several objects of the same kind into a single, bigger group. These components or entities may be of the user-defined data kinds, such as structures, or they may be of the int, float, char, or double data types.Below is some point to be noted:
 

  1. A straightforward list of elements on a single line or column is called a 1D array.
  2. A group of objects arranged in rows and columns to create a table or matrix is called a 2D array.
  3. A single index is needed to access elements in a 1D array.
  4. Two indices are needed to access elements in a 2D array: one for each row and column.

Here we are explain the declaration and initialization of one and two-dimensional arrays with example in c.


One-Dimensional Array:

In given example, oneDimArray is a one-dimensional array of integers with a size of 5.
 The array is initialized with values {1, 2, 3, 4, 5}.


int numbers[5];

int numbers[5] = {1, 2, 3, 4, 5};
// Initialization after declaration
numbers[0] = 1;
numbers[1] = 2;


 Two-Dimensional Array:

In given example, twoDimArray is a one-dimensional array of integers with a size of [3,3].
 The array is initialized with values {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}.

int matrix[3][3];
// Declaration and initialization of a 2D integer array
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

// Initialization after declaration
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;

Here  you can see example of  one dimension array :

#include <stdio.h>

int main() {
    // Declaration and initialization of a one-dimensional array
    int oneDimArray[5] = {1, 2, 3, 4, 5};

    // Accessing and printing elements of the one-dimensional array
    printf("One-Dimensional Array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", oneDimArray[i]);
    }

    return 0;
}

 

 

Here you can see example of  two dimension array :

#include <stdio.h>

int main() {
    // Declaration and initialization of a two-dimensional array
    int twoDArray[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Accessing and printing elements of the two-dimensional array
    printf("Two-Dimensional Array: Developer Indian\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d ", twoDimArray[i][j]);
        }
        printf("\n");
    }

    return 0;
}


Conclusion

Here we learn about array and various type of dimension .These above programs to see the output demonstrating the initialization and printing data in array.Example is initialise data in one dimension  and two dimension ,finally print it on console using loop.

Article