Arrays in C Language: Usage of One-Dimensional and Multi-Dimensional Arrays

Arrays in C Language: Usage of One-Dimensional and Multi-Dimensional Arrays

In the C language, arrays are an important data structure used to store multiple elements of the same type. They can be integers, characters, floating-point numbers, etc. This article will detail the usage of one-dimensional and multi-dimensional arrays, along with code examples for demonstration.

1. One-Dimensional Arrays

1.1 Definition and Initialization

A one-dimensional array is the simplest form, which can be viewed as a linear list. In C language, the definition 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 the number of elements to store

For example, to define a one-dimensional array containing 5 integers:

int numbers[5];

We can also initialize it at the time of declaration:

int numbers[5] = {1, 2, 3, 4, 5};

1.2 Using One-Dimensional Arrays

Accessing and modifying elements in a one-dimensional array can be done using indices. Note that indices start from 0. For example, to access the first element, you can use<span>numbers[0]</span>.

Here is a simple example of one-dimensional array operations, including input, output, and summation functionality:

#include <stdio.h>
int main() {
    int numbers[5]; // Declare a one-dimensional integer array of size 5
    int sum = 0;
    // Input data
    printf("Please enter 5 integers:\n");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &numbers[i]);
        sum += numbers[i]; // Accumulate sum
    }
    // Output results
    printf("The numbers you entered are: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\nThe total sum is: %d\n", sum);
    return 0;
}

Example Explanation:

  • The user inputs five integers and stores them in<span>numbers</span>.
  • The program calculates the total of these numbers and outputs it.

2. Multi-Dimensional Arrays

2.1 Definition and Initialization

A multi-dimensional array adds one or more indices to each element. The most common is the two-dimensional array, which can be viewed as a matrix. In C language, the definition format of a two-dimensional array is as follows:

type arrayName[size1][size2];

For example, to define a two-dimensional integer matrix with 3 rows and 4 columns:

int matrix[3][4];

We can also initialize it at the time of declaration:

int matrix[3][4] = {   {1, 2, 3, 4},   {5, 6, 7, 8},   {9,10,11,12}};

2.2 Using Two-Dimensional Arrays

Accessing elements in a two-dimensional array also requires two indices. For example, to access the element in the first row and second column, you can use<span>matrix[0][1]</span>.

Here is a simple example of two-dimensional array operations, including printing the matrix content and calculating the sum of all elements:

#include <stdio.h>
#define ROWS 3 // Macro definition for number of rows
#define COLS 4 // Macro definition for number of columns
int main() {
    int matrix[ROWS][COLS] = {
        {1,   -2 , -3 , -4 },
        {-5 , -6 , -7 , -8 },
        {-9 ,10 ,11 ,12 }     };
    int sum =0;
    // Print matrix content and calculate total
    printf("Matrix content:\n");
    for (int i =0 ;i<ROWS;i++) {
        for(int j=0;j<COLS;j++) {
            printf("%d ",matrix[i][j]);
            sum += matrix[i][j]; // Accumulate sum
        }
        printf("\n");
    }
    printf("The sum of all elements is: %d\n",sum);
    return 0;
}

Example Explanation:

  • The program first prints the entire matrix, then calculates the sum of all elements and outputs the result.

3. Conclusion

This article introduced the basic concepts and usage of one-dimensional and multi-dimensional arrays in the C language. One-dimensional arrays are important tools for handling linear data, while multi-dimensional arrays are suitable for more complex data structures, such as tables or image processing scenarios. Mastering these foundational concepts will help you better understand C programming and similar concepts in other programming languages. I hope this article helps you gain a deeper understanding of the application of arrays in C.

Leave a Comment