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](https://fs.enterprisedna.co/library/data-mentor/cover-images/l5I6lwGDb85ir0bSWOO5ff6OkR0ujk1InnYVW3JN.webp)
Prompt
Answer
To manipulate a 2D array from row-major to column-major order in C, follow these steps:
- Declare a new 2D array with dimensions swapped (number of columns x number of rows).
- Iterate through the original array using nested loops:
- The outer loop runs through the rows.
- The inner loop runs through the columns.
- Assign the value of the original array to the corresponding position in the new array with swapped indices.
- 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;
}
Description
To manipulate a 2D array from row-major to column-major order in C, follow these steps:
- Declare a new 2D array with dimensions swapped (number of columns x number of rows).
- Iterate through the original array using nested loops:
- The outer loop runs through the rows.
- The inner loop runs through the columns.
- Assign the value of the original array to the corresponding position in the new array with swapped indices.
- 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.
More Code Generators
![Apache Flink Code Generator](https://fs.enterprisedna.co/library/icons/flink.png)
![Apache Pig Code Generator](https://fs.enterprisedna.co/library/icons/apache_pig.png)
![Azure Data Factory Code Generator](https://fs.enterprisedna.co/library/icons/azure-data-factory.png)
![C/C++ Code Generator](https://fs.enterprisedna.co/library/icons/c_c_plus.png)
![CouchDB Code Generator](https://fs.enterprisedna.co/library/icons/couchdb.png)
![DAX Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Excel Code Generator](https://fs.enterprisedna.co/library/icons/excel.png)
![Firebase Code Generator](https://fs.enterprisedna.co/library/icons/firebase.png)
![Google BigQuery Code Generator](https://fs.enterprisedna.co/library/icons/google_bigquery.png)
![Google Sheets Code Generator](https://fs.enterprisedna.co/library/icons/google_sheets.png)
![GraphQL Code Generator](https://fs.enterprisedna.co/library/icons/graphql.png)
![Hive Code Generator](https://fs.enterprisedna.co/library/icons/hive.png)
![Java Code Generator](https://fs.enterprisedna.co/library/icons/java.png)
![JavaScript Code Generator](https://fs.enterprisedna.co/library/icons/javascript.png)
![Julia Code Generator](https://fs.enterprisedna.co/library/icons/julia.png)
![Lua Code Generator](https://fs.enterprisedna.co/library/icons/lua.png)
![M (Power Query) Code Generator](https://fs.enterprisedna.co/library/icons/m.png)
![MATLAB Code Generator](https://fs.enterprisedna.co/library/icons/matlab.png)
![MongoDB Code Generator](https://fs.enterprisedna.co/library/icons/mongodb.png)
![Oracle Code Generator](https://fs.enterprisedna.co/library/icons/oracle.png)
![PostgreSQL Code Generator](https://fs.enterprisedna.co/library/icons/postgresql.png)
![Power BI Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Python Code Generator](https://fs.enterprisedna.co/library/icons/python.png)
![R Code Generator](https://fs.enterprisedna.co/library/icons/r.png)
![Redis Code Generator](https://fs.enterprisedna.co/library/icons/redis.png)
![Regex Code Generator](https://fs.enterprisedna.co/library/icons/regex.png)
![Ruby Code Generator](https://fs.enterprisedna.co/library/icons/ruby.png)
![SAS Code Generator](https://fs.enterprisedna.co/library/icons/sas.png)
![Scala Code Generator](https://fs.enterprisedna.co/library/icons/scala.png)
![Shell Code Generator](https://fs.enterprisedna.co/library/icons/shell.png)
![SPSS Code Generator](https://fs.enterprisedna.co/library/icons/spss.png)
![SQL Code Generator](https://fs.enterprisedna.co/library/icons/sql.png)
![SQLite Code Generator](https://fs.enterprisedna.co/library/icons/sqlite.png)
![Stata Code Generator](https://fs.enterprisedna.co/library/icons/stata.png)
![Tableau Code Generator](https://fs.enterprisedna.co/library/icons/tableau.png)
![VBA Code Generator](https://fs.enterprisedna.co/library/icons/vba.png)