Disciplined storage: Arrays

Arrays in C are collections of elements of the same type, stored in contiguous memory locations. Let's go through various aspects of arrays with sample code and comments for explanation:

#include <stdio.h>

int main() {
    // Declaring an array
    int numbers[5];

    // Initializing an array
    numbers[0] = 1;
    numbers[1] = 2;
    numbers[2] = 3;
    numbers[3] = 4;
    numbers[4] = 5;

    // Alternatively, declaring and initializing in one step
    // int numbers[] = {1, 2, 3, 4, 5};

    // Finding the size of the array
    int size = sizeof(numbers) / sizeof(numbers[0]);
    printf("Size of the array: %d\n", size);

    // Accessing elements of the array
    printf("First element: %d\n", numbers[0]);
    printf("Third element: %d\n", numbers[2]);

    // Iterating through the array using a loop
    printf("Array elements: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    // Initializing an array with a specific size and value
    int zeros[3] = {0}; // Initializes all elements to 0
    printf("Array with zeros: %d %d %d\n", zeros[0], zeros[1], zeros[2]);

    // Multi-dimensional arrays
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Accessing elements in a multi-dimensional array
    printf("Matrix element at (1, 2): %d\n", matrix[1][2]);

    return 0;
}

Explanation with comments:

  1. Array Declaration:

     int numbers[5];
    

    This declares an integer array named numbers with a size of 5.

  2. Array Initialization:

     numbers[0] = 1;
     // ...
     numbers[4] = 5;
    

    Initializes each element of the array individually.

  3. Array Declaration and Initialization in One Step:

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

    This declares and initializes the array in a single line. The size is automatically determined based on the number of elements provided.

  4. Finding Array Size:

     int size = sizeof(numbers) / sizeof(numbers[0]);
    

    Calculates the size of the array by dividing the total size of the array by the size of one element.

  5. Accessing Array Elements:

     printf("First element: %d\n", numbers[0]);
     printf("Third element: %d\n", numbers[2]);
    

    Accessing individual elements of the array using their indices.

  6. Iterating Through an Array:

     for (int i = 0; i < size; i++) {
         printf("%d ", numbers[i]);
     }
    

    Using a loop to iterate through the array and print its elements.

  7. Initializing Array with a Specific Size and Value:

     int zeros[3] = {0};
    

    Initializes all elements of the array to 0. The size is specified, and if there are fewer values provided than the size, the remaining elements are initialized to 0.

  8. Multi-dimensional Arrays:

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

    Declares a 2x3 matrix.

  9. Accessing Elements in a Multi-dimensional Array:

     printf("Matrix element at (1, 2): %d\n", matrix[1][2]);
    

    Accessing individual elements in a multi-dimensional array using indices.

Let's use the matrix array defined in the previous example and demonstrate how to access its elements using nested loops:

#include <stdio.h>

int main() {
    // Multi-dimensional array (2x3)
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    // Accessing elements using nested loops
    printf("Matrix elements:\n");
    for (int row = 0; row < 2; row++) {
        for (int col = 0; col < 3; col++) {
            printf("%d ", matrix[row][col]);
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation with comments:

  1. Nested Loops:

     for (int row = 0; row < 2; row++) {
         for (int col = 0; col < 3; col++) {
             printf("%d ", matrix[row][col]);
         }
         printf("\n"); // Move to the next line after each row
     }
    

    Nested loops are used to iterate through each element of the two-dimensional array. The outer loop (row) iterates over the rows, and the inner loop (col) iterates over the columns.

  2. Accessing Matrix Elements:

     printf("%d ", matrix[row][col]);
    

    This line prints the value of the current element at the specified row and column of the matrix.

  3. Newline After Each Row:

     printf("\n");
    

    After printing all the elements in a row, a newline character is printed to move to the next line for the next row.

When you run this program, it will output:

Matrix elements:
1 2 3
4 5 6

These examples cover various aspects of working with arrays in C, including declaration, initialization, accessing elements, iterating, and multi-dimensional arrays.