C Array Dimensions

What is an Array Dimensions?

Array Dimensions

The dimensions of an array refer to the number of indices or subscripts needed to access its elements. In C, arrays can be one-dimensional (1D), two-dimensional (2D), or multi-dimensional (more than two dimensions). Here's a brief explanation of each:

One-Dimensional Array:

A one-dimensional array is like a list or a sequence of elements, and It's defined with a single pair of square brackets '[]', for example:

Syntax:

int arr1D[5];

* Defines a 1D array of integers with 5 elements.

Example:

c Copy Code
#include <stdio.h>

                                int main() {
    // Declare an integer array of size 5
    int arr1D[5]={10,20,30,40,50};

    // Access and print the array elements
    printf("Array elements: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr1D[i]);
    }

    return 0;
}
Output:
Array elements: 10 20 30 40 50

Two-Dimensional Array:

A two-dimensional array can be thought of as a table with rows and columns, and It is defined with two pairs of square brackets '[][]'. Also, you need to specify the number of rows and columns when defining it, for example:

Syntax:

int arr2D[4][8];

* Defines a 2D array with 4 rows and 8 columns.

Example:

c Copy Code
#include <stdio.h>

int main() {
    // Declare a 2D integer array with 3 rows and 4 columns
    int arr2D[4][8];

    // Initialize the 2D array elements
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 8; j++) {
            arr2D[i][j] = i * 8 + j + 1;
        }
    }

    // Access and print the 2D array elements
    printf("2D Array elements:\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 8; j++) {
            printf("%d \t", arr2D[i][j]);
        }
        printf("\n"); // Move to the next row
    }

    return 0;
}
Output:
2D Array elements:
1       2       3       4       5       6       7       8 
9       10      11      12      13      14      15      16 
17      18      19      20      21      22      23      24 
25      26      27      28      29      30      31      32

Multi-Dimensional Array:

A multi-dimensional array has more than two dimensions and is essentially a nested array of arrays. You can extend the concept of a 2D array to create arrays with more dimensions, for example:

Syntax:

int arr3D[2][3][4];

* Defines a 3D array with 2 "sheets," each having 3 rows and 4 columns.

Example:

c Copy Code
#include <stdio.h>

int main() {
    // Declare a 3D integer array with dimensions 2x3x4
    int arr3D[2][3][4];

    // Initialize the 3D array elements
    int value = 1;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                arr3D[i][j][k] = value++;
            }
        }
    }

    // Access and print the 3D array elements
    printf("3D Array elements:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                printf("%d \t", arr3D[i][j][k]);
            }
            printf("\n"); // Move to the next row
        }
        printf("\n"); // Add a blank line to separate the 2D slices
    }

    return 0;
}
Output:
3D Array elements:
1       2       3       4  
5       6       7       8  
9       10      11      12 

13      14      15      16
17      18      19      20
21      22      23      24

* When you run this program, it will calculate and print the sum of all elements in the 'arr3D' array.

Matrix Addition

Matrices support various operations, such as addition, subtraction, multiplication, and transposition. These operations are typically implemented using nested loops to iterate through the elements of the matrices.

Here's a simple C program that takes user input for two matrices and then calculates their sum:

c Copy Code
#include <stdio.h>

int main() {
    int rows, cols;

    // Get the dimensions of the matrices from the user
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    // Declare two matrices of the given dimensions
    int matrix1[rows][cols];
    int matrix2[rows][cols];
    int result[rows][cols];

    // Input values for the first matrix
    printf("Enter values for the first matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Input values for the second matrix
    printf("Enter values for the second matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Calculate the sum of the two matrices
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    // Display the result matrix
    printf("Sum of the matrices:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d\t", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

This program first asks the user for the number of rows and columns for the matrices. Then, it takes input values for both matrices and calculates their sum. Finally, it displays the result matrix, which is the sum of the two input matrices.

Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter values for the first matrix:
10      20
-5      15
Enter values for the second matrix:
-4      30
10      -5
Sum of the matrices:
6       50
5       10

Overall, matrices implemented using arrays provide a convenient and efficient way to work with two-dimensional or multi-dimensional data, making them suitable for a wide range of applications, from graphics and image processing to scientific computing and machine learning.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.