Arrays in C Language: Definitions and Applications of One-Dimensional and Multi-Dimensional Arrays
In the C language, arrays are an important data structure used to store multiple data of the same type. This article will detail the definitions, usage methods, and code examples of one-dimensional and multi-dimensional arrays, helping basic users better understand and apply these concepts.
1. One-Dimensional Arrays
1.1 Definition
A one-dimensional array is the simplest form of an array, which can be viewed as a linear list. We can access each element using an index. One-dimensional arrays are stored contiguously in memory.
1.2 Declaration and Initialization
In C language, the declaration format of a one-dimensional array is as follows:
type arrayName[arraySize];
<span>type</span>: Data type (e.g., int, float, char, etc.)<span>arrayName</span>: Custom variable name<span>arraySize</span>: Specifies how many elements the array can hold
For example, we can declare a one-dimensional array containing 5 integers:
int numbers[5];
We can also initialize it at the time of declaration:
int numbers[5] = {10, 20, 30, 40, 50};
1.3 Using One-Dimensional Arrays
To access or modify elements in a one-dimensional array, we can use the index. Note that the index starts from 0. For example, to access the first element, we can write:
printf("%d\n", numbers[0]); // Outputs 10
numbers[0] = 100; // Modifies the first element to 100
Example Code: Calculating the Average of a Set of Numbers
The following is a complete example demonstrating how to calculate the average of input numbers using a one-dimensional array.
#include <stdio.h>
int main() {
int numbers[5]; // Declare a one-dimensional array containing 5 integers
int sum = 0;
// Input numbers and calculate the total
printf("Please enter 5 integers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i]; // Accumulate to sum
}
float average = sum / 5.0; // Calculate average
printf("The average of these five numbers is: %.2f\n", average);
return 0;
}
2. Multi-Dimensional Arrays
2.1 Definition
A multi-dimensional array is a data structure composed of multiple linear lists, with the most commonly used being the two-dimensional array. A two-dimensional array can be viewed as a matrix, consisting of rows and columns.
2.2 Declaration and Initialization
The declaration format of a two-dimensional array is as follows:
type arrayName[rowSize][columnSize];
For example, we can declare a two-dimensional array of integers with 3 rows and 4 columns:
int matrix[3][4];
It can also be initialized at the time of declaration:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Example Code: Printing a Two-Dimensional Matrix and Its Transpose
The following example demonstrates how to print a given matrix and its transposed matrix.
#include <stdio.h>
#define ROWS (3)
#define COLS (4)
void printMatrix(int matrix[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void transposeMatrix(int matrix[ROWS][COLS], int transposed[COLS][ROWS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
transposed[j][i] = matrix[i][j];
}
}
}
int main() {
int matrix[ROWS][COLS] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int transposed[COLS][ROWS];
printf("Original Matrix:\n");
printMatrix(matrix);
transposeMatrix(matrix, transposed);
printf("\nTransposed Matrix:\n");
printMatrix(transposed);
return(0);
}
3. Conclusion
This article introduced the basic concepts of one-dimensional and multi-dimensional arrays in the C language, including how they are defined, initialized, and used. In practical programming, effectively utilizing arrays can manage data efficiently and improve program performance. It is hoped that through this article, you will gain a deeper understanding of arrays in C language and be able to apply them flexibly in your own projects.