Created
February 24, 2017 04:59
-
-
Save fpdjsns/e19458808dbe31869106e5c42346dd11 to your computer and use it in GitHub Desktop.
row major ordering & column major ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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