Array


Defination:

array is collection of data which have same data type

array use to store multiple value widh same data type

types of array:

One-dimensional array: Also called a vector, it is the simplest form of an array where elements are stored in a single row or column. Elements are accessed using a single index.

#include
int main() {
int arr[5]; // Declaration of a one-dimensional array of integers with size 5
// Assigning values to array elements
arr[0] = 10;
arr[1] = 20;
// Accessing and printing array elements
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0; }

Two-dimensional array: Also known as a matrix, it is an array of arrays where elements are arranged in rows and columns. Elements are accessed using two indices: one for the row and one for the column.

#include br int main() {
int matrix[3][3]; // Declaration of a two-dimensional array of integers with size 3x3
// Assigning values to array elements for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = i + j;
}
} // Accessing and printing array elements for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++ {
printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);
}
}
return 0; }

Multi-dimensional array: This refers to arrays with more than two dimensions. Each additional dimension adds another level of depth. Accessing elements in multi-dimensional arrays requires specifying multiple indices.

#include int main() {
int threeDArray[2][3][4]; // Declaration of a three-dimensional array of integers with size 2x3x4
// Assigning values to array elements for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
threeDArray[i][j][k] = i + j + k;
}
}
}
// Accessing and printing array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("threeDArray[%d][%d][%d] = %d\n", i, j, k, threeDArray[i][j][k]);
}
}
}
return 0; }