Code Generator | C/C++

2D Array Conversion: Row-Major to Column-Major in C

This project provides a method to convert a 2D array from row-major order to column-major order in C. It involves swapping the dimensions of the array and assigning values to the corresponding positions in the new array with swapped indices. The new...


Empty image or helper icon

Prompt

How can I manipulate a 2D array stored in row-major order to column-major order in C?

Answer

To manipulate a 2D array from row-major to column-major order in C, follow these steps:

  1. Declare a new 2D array with dimensions swapped (number of columns x number of rows).
  2. Iterate through the original array using nested loops:
    • The outer loop runs through the rows.
    • The inner loop runs through the columns.
  3. Assign the value of the original array to the corresponding position in the new array with swapped indices.
  4. Access the elements of the new array in column-major order.
// Function to convert a 2D array from row-major to column-major order
void rowToColumnMajor(int** inputArray, int numRows, int numCols, int** outputArray) {
    // Loop through rows
    for(int row = 0; row < numRows; row++) {
        // Loop through columns
        for(int col = 0; col < numCols; col++) {
            // Assign the value from the original array to the corresponding position
            // in the new array with swapped indices
            outputArray[col][row] = inputArray[row][col];
        }
    }
}

Example usage:

int main() {
    // Create the original 2D array in row-major order
    int numRows = 4;
    int numCols = 3;
    int** inputArray = malloc(numRows * sizeof(int*));
    for(int i = 0; i < numRows; i++) {
        inputArray[i] = malloc(numCols * sizeof(int));
    }
    
    // Populate the original array
    // [1, 2, 3]
    // [4, 5, 6]
    // [7, 8, 9]
    // [10, 11, 12]
    int count = 1;
    for(int row = 0; row < numRows; row++) {
        for(int col = 0; col < numCols; col++) {
            inputArray[row][col] = count;
            count++;
        }
    }
    
    // Create the new array with swapped dimensions
    int** outputArray = malloc(numCols * sizeof(int*));
    for(int i = 0; i < numCols; i++) {
        outputArray[i] = malloc(numRows * sizeof(int));
    }
    
    // Convert the original array to column-major order
    rowToColumnMajor(inputArray, numRows, numCols, outputArray);
    
    // Access elements of the new array in column-major order
    // [1, 4, 7, 10, 2, 5, 8, 11, 3, 6, 9, 12]
    for(int col = 0; col < numCols; col++) {
        for(int row = 0; row < numRows; row++) {
            printf("%d ", outputArray[col][row]);
        }
    }
    
    return 0;
}

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

To manipulate a 2D array from row-major to column-major order in C, follow these steps:

  1. Declare a new 2D array with dimensions swapped (number of columns x number of rows).
  2. Iterate through the original array using nested loops:
    • The outer loop runs through the rows.
    • The inner loop runs through the columns.
  3. Assign the value of the original array to the corresponding position in the new array with swapped indices.
  4. Access the elements of the new array in column-major order.

Example usage:

Declare the original 2D array in row-major order with a given number of rows and columns.

Populate the original array with values.

Create the new array with swapped dimensions.

Convert the original array to column-major order using the rowToColumnMajor function.

Access elements of the new array in column-major order.