Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created February 24, 2017 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/e19458808dbe31869106e5c42346dd11 to your computer and use it in GitHub Desktop.
Save fpdjsns/e19458808dbe31869106e5c42346dd11 to your computer and use it in GitHub Desktop.
row major ordering & column major ordering
#include<iostream>
#define MAX 100
using namespace std;
//make row major ordering
int (*make_row_major(int a, int b))[MAX]
{
int arr[MAX][MAX] = { 0 };
int temp = 1;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
arr[i][j] = temp++;
return arr;
}
//make column major ordering
int (*make_column_major(int a, int b))[MAX]
{
int arr[MAX][MAX] = { 0 };
int temp = 1;
for (int j = 0; j < b; j++)
for (int i = 0; i < a; i++)
arr[i][j] = temp++;
return arr;
}
//print a x b array
void print(int (*arr)[MAX], int a, int b)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf("%3d", arr[i][j]);
printf("\n");
}
}
int main()
{
int row, column;
int(*arr)[MAX];
cout << "row : "; cin >> row;
cout << "column : "; cin >> column;
cout << "== row major ordering ==" << endl;
arr = make_row_major(row, column);
print(arr, row, column); cout << endl;
cout << "== column major ordering ==" << endl;
arr = make_column_major(row, column);
print(arr, row, column); cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment