Row Major And Column Major Program In C

A multi-dimensional array stores its elements consecutively row by row in row-major order. Initially, the elements of the first row are stored in a 2D array, followed by the elements of the subsequent rows. This sequential arrangement of memory addresses in the row direction facilitates efficient access to array elements.

Example: Row_major.c

Example

#include <stdio.h>



int main() {

 //row-major array

 int row_major_array[3][4] = {{5, 2, 0, 14},

 {12, 6, 7, 3},

 {7, 15, 11, 12}};

 // the size of rows and columns

 int rows_count = 3;

 int cols_count = 4;

 

 //printing the matrix according to row-major order

 for (int i = 0; i < rows_count; i++) {

 for (int j = 0; j < cols_count; j++) {

 printf("%d ", row_major_array[i][j]);

 }

 printf("\n");

 }



 return 0;

}

Output:

Output

5 2 0 14 

12 6 7 3 

7 15 11 12

Explanation:

An array named rowmajorarray with a dimension of 3 rows and 4 columns is declared and initialized with specific values in a row-major order.

In this example, this line declares and initializes the rowmajorarray 2D integer array. It has three rows and four columns, and the elements are pre-populated with predefined values.

  • rowmajorarray0 has 5\
  • 0 has 2
  • rowmajorarray2 is made up of 12
  1. int rowscount = 3 and 4 int colscount = 4;:

These lines define two variables, rowscount and colscount , to hold the number of rows and columns in the rowmajorarray array.

  1. Loops that are nested:
  • The outer loop (for (int i = 0; i rows_count; i++)) iterates over the array's rows.
  • The inner loop (for (int j = 0; j cols_count; j++)) cycles through the array's columns.

Printing the elements of a 2D array using the printf("%d ", rowmajorarrayi); statement is a common practice. Following each row's elements, a newline character is printed using printf("\n"); to move to the next row.

Returning 0 signifies the successful completion of the main function, indicating that the program has executed without any errors.

2. Column-Major Order:

Values within an array that has multiple dimensions are sequentially stored column by column following the column-major order. The elements from the first column are placed consecutively in a two-dimensional array, followed by the elements from each subsequent column in a similar manner. This arrangement ensures that the memory addresses are contiguous and follow the order of the columns.

Example: column_major.c

Example

#include <stdio.h>

int main() {

 int rows = 3;

 int cols = 4;

 int matrix[rows][cols];



 // Initialize the matrix

 int count = 1;

 for (int j = 0; j < cols; ++j) {

 for (int i = 0; i < rows; ++i) {

 matrix[i][j] = count++;

 }

 }



 // Print the original matrix (row-major order)

 printf("Original Matrix (Row-Major Order):\n");

 for (int i = 0; i < rows; ++i) {

 for (int j = 0; j < cols; ++j) {

 printf("%d\t", matrix[i][j]);

 }

 printf("\n");

 }



 // Print the matrix in column-major order

 printf("\nMatrix in Column-Major Order:\n");

 for (int j = 0; j < cols; ++j) {

 for (int i = 0; i < rows; ++i) {

 printf("%d\t", matrix[i][j]);

 }

 printf("\n");

 }



 return 0;

}

Output:

Output

Original Matrix (Row-Major Order):

1	4	7	10	

2	5	8	11	

3	6	9	12	



Matrix in Column-Major Order:

1	2	3	

4	5	6	

7	8	9	

10	11	12

Explanation:

  1. The program begins by defining a 3x4 matrix using the matrix 2D array.
  • Two variables, rows and cols (3 rows and 4 columns in the present example) give the dimensions of the matrix.
  • A variable count is set to 1. The value of this variable is used to fill up the matrix's elements.
  1. Filling the Matrix:
  • The nested loops are used for iterating through every column first, followed by each row within the column.
  • The count variable has been assigned to the currently selected element in the matrix, and then the count is increased. As a result, the matrix has been filled with sequential numbers beginning with 1.
  1. Printing the Original Matrix (In Row-Major Order):
  • The program begins by printing the original matrices in row-major
  • Two nested loops are utilized to cycle across the rows and columns of the matrix.
  • The printf statement displays each matrix element, followed by the tab character (t). After displaying a row, a newline character (\n) is written to advance to the following line.
  1. Printing the Matrix in Column Major Order:
  • After displaying the original matrix, the program prints the identical matrix in column-major order.
  • The loops are organized differently: The outer loop iterates over columns, whereas the inner one iterates over rows inside each column.

Items are displayed based on the row-major sequence, resulting in a matrix shown row by row.

Input Required

This code uses input(). Please provide values below: