arrays in C Language:
Arrays in C
Arrays in C are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value. Arrays can be one-dimensional (1D), two-dimensional (2D), or even multi-dimensional (3D).
1D Arrays
A one-dimensional array stores a list of elements of the same type in a single row.
#include
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Dynamic Initialization of 1D Arrays
#include
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2D Arrays
A two-dimensional array stores elements in a matrix format, consisting of rows and columns.
#include
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Dynamic Initialization of 2D Arrays
#include
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter elements for %d x %d array:\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("You entered:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
3D Arrays
A three-dimensional array stores elements in a 3D space, consisting of multiple 2D arrays.
#include
int main() {
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Dynamic Initialization of 3D Arrays
#include
int main() {
int x, y, z;
printf("Enter the dimensions (x y z): ");
scanf("%d %d %d", &x, &y, &z);
int arr[x][y][z];
printf("Enter elements for %d x %d x %d array:\n", x, y, z);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
scanf("%d", &arr[i][j][k]);
}
}
}
printf("You entered:\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Common Use Cases of Arrays
Arrays are commonly used in various scenarios in programming, such as:
- Storing a collection of data items (e.g., list of integers, list of names)
- Implementing mathematical matrices and performing matrix operations
- Representing multi-dimensional data (e.g., tables, grids)
- Handling data in scientific computations and simulations
Examples
Here are some additional examples demonstrating the usage of arrays in C:
Example: Summing Elements of an Array
#include
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of elements: %d\n", sum);
return 0;
}
Example: Multiplying Matrices
#include
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{2, 0}, {1, 2}};
int c[2][2] = {0};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}