Array Representation – Column-major & Row-major

Array Representation:

  • Column-major
  • Row-major

Arrays may be represented in Row-major form or Column-major form.  In Row-major form, all the elements of the first row are printed, then the elements of the second row and so on upto the last row.  In Column-major form, all the elements of the first column are printed, then the elements of the second column and so on upto the last column.  The ‘C’ program to input an array of order m x n and print the array contents in row major and column major is given below.  The following array elements may be entered during run time to test this program:

Input:     Rows: 3;  Columns: 3;

1     2     3

4     5     6

7     8     9

Output:

Row Major:

1     2     3

4     5     6

7     8     9

Column Major:

1     4     7

2     5     8

3     6     9

 /* Program to input an array and display in row-major and column major form */

#include <stdio.h>

#define MAX 10

int main(){
int arr[MAX][MAX],m,n,i,j;
printf(“nEnter total number of rows?”);
scanf(“%d”,&m);
printf(“nEnter total number of columns?”);
scanf(“%d”,&n);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“nEnter number?”);
scanf(“%d”,&arr[i][j]);
}
}
printf(“nnRow-Major Order:n”);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“%dt”,arr[i][j]);
}
printf(“n”);
}
printf(“nnColumn-Major Order:n”);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf(“%dt”,arr[j][i]);
}
printf(“n”);
}
}