Declaration and initialization of one dimensional and two-dimensional arrays
#Declaration _initialization_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:
Here we are explain the declaration and initialization of one and two-dimensional arrays with example in c.
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;
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;
#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;
}
#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;
}
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.