查看完整版本: row major & column major
頁: [1]

mp22338 發表於 2019-12-16 05:16 PM

row major & column major

請問如何用C/C++寫 row major & column major
EX.
input 列數 & 行數
產生2D matrix
┌ 6 4 5 ┐
|  8 7 2 |
└ 3 1 9 ┘
然後print
row major: 6 4 5 8 7 2 3 1 9
column major: 6 8 3 4 7 1 5 2 9
謝謝
感恩喔喔!
<div></div>

issey123456 發表於 2020-2-25 11:18 AM

If the matrix size is fixed, such as 3 x 3, then you can simply use a nested array:

int matrix = {{6,4,5}, {8,7,2}, {3,1,9}};
for (int r(0); r < 3; ++r)
{
    for (int c(0); c < 3; ++c)
    {
        cout << matrix;
     }
}

This is row major. To write column major, you can do:

int matrix = {{6,8,3},{4,7,1},{5,2,9}};
for (int c(0); c < 3; ++c)
{
    for (int r(0); r < 3; ++r)
    {
        cout << matrix;
     }
}
頁: [1]