Click here to Skip to main content
15,893,994 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HellO Hii ..

Please Correct Me .. Regarding MultiDimentional Array Storage Ordering. i.e Row Major And Col Major.
I want to enter data row by row and print in column by column ..

for example:

C++
const int rows = 2; // Declared As Global Variable
const int cols = 3;	// Declared As Global Variable
void getByRow(int *ary, int row){ // Row Major
	int offset = -1;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			offset = i * cols + j;
			cin >> *(ary + offset);
		}
	}
}
void printByCol(int *arr, int col){
	int offset = 1210;
	for (int i = 0; i < col; i++)
	{
		for (int j = 0; j < rows; j++)
		{
			offset = i*rows + j;
			cout << *(arr + offset) << " ";
		}
		cout << endl;
	}
}


for instance 2D array [2 * 3], 2 ROWS AND 3 COLUMNS

Matrix Form = 1 2 3 // first row entered
              4 5 6 // 2nd row
OK .. Now here i want to print data Col by Col,
it should print first column first, then 2nd column, and at last the 3rd One .. 
col 1 = 1, 4
col 2 = 2, 5
col 3 = 3, 6

OR this is not possible . ?? 
Also by default which storage method C++ follow .. :?

Thanks For Consideration .. 
Posted

1 solution

Just make the inside of the loops use the same math:
void printByCol(int *arr, int col){
	int offset = 1210;
	for (int i = 0; i < col; i++)
	{
		for (int j = 0; j < rows; j++)
		{
			offset = i * cols + j;
			cout << *(arr + offset) << " ";
		}
		cout << endl;
	}
}
 
Share this answer
 
Comments
Usman Hunjra 9-Feb-14 4:02am    
thank you respected sir .. +5
OriginalGriff 9-Feb-14 4:16am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900