一个简单的问题.. [英] A Simple Question ..
本文介绍了一个简单的问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HellO Hii ..
请纠正我......关于MultiDimentional数组存储排序。即Row Major和Col Major。
我想逐行输入数据并逐列打印..
例如:
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:
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 ..
推荐答案
只需使循环内部使用相同的数学: br />
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;
}
}
这篇关于一个简单的问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文