开发者

create matrix using character arrays in c++

开发者 https://www.devze.com 2023-02-10 10:14 出处:网络
I have 8 different arrays, and each array has 8 characters in it, std::string str_v1 = v1.to_string();

I have 8 different arrays, and each array has 8 characters in it,

        std::string str_v1 = v1.to_string();
        char arr_v1[9] = {0}; 
        std::copy(str_v1.begin(), str_v1.end(), arr_v1); // from str_v1 to str_v8

        std::string str_v8 = v8.to_string();
        char arr_v8[9] = {0}; 
        std::copy(str_v8.begin(), str_v8.end(), arr_v8); 

how to convert this into 8x8 matrix? I want to put these values column by column, like, each array is converted to one column of the matrix, like array1 to column1, array2 to column2, and so on, like array1 values will be placed like matrix[0][0], matrix[1][0], matrix[2][0] and so on.. I think, something like this needs to be done:

char matrix[8][8];
        for( int y=0;y<8;y++)
        {
                matrix[y][0] = arr_v1[y];
                matrix[y][1] =开发者_C百科 arr_v2[y];
                matrix[y][2] = arr_v3[y];
                matrix[y][3] = arr_v4[y];
                matrix[y][4] = arr_v5[y];
                matrix[y][5] = arr_v6[y];
                matrix[y][6] = arr_v7[y];
                matrix[y][7] = arr_v8[y];
        }


for( int y=0;y<8;y++)
        {
            for( int z=0;z<8;z++)
            {
             switch(y)
             {
             case 0:
             matrix[z][y] = arr_v1[z]; //Be pretty sure, possibly you are better than you believe
             break;                    // I've placed y before z as y is the outer loop, hence it 
             case 1:                   // should be responsible for ROWS and z for COLUMNS
             matrix[z][y] = arr_v2[z]; // Goes in matrix[0][1],[1][1],[2][1],[3][1]...[7][1]
             break;
             case 2:
             matrix[z][y] = arr_v3[z]; // Goes in matrix[0][2],[1][2],[2][2],[3][2]...[7][2]
             break;
             case 3:
             matrix[z][y] = arr_v4[z]; // Goes on
             break;
             case 4:
             matrix[z][y] = arr_v5[z]; // And on
             break;
             case 5:
             matrix[z][y] = arr_v6[z]; // And on
             break;
             case 6:
             matrix[z][y] = arr_v7[z];
             break;
             case 7:
             matrix[z][y] = arr_v8[z];
             break;
             }
            }  // Finally all 8 1x8 arrays stored into single 8x8 matrix
        }

There you have it then your ways,each 1x8 coming as a COLUMN instead of a row as in previous one :)


char *matrix[9];
for (int i = 0; i < 9; ++i) {
    matrix[i] = new char[9];
    std::copy(your_ith_string.begin(), your_ith_string.end(), matrix[i]);
}
//Finish your work with the matrix
for (int i = 0; i < 9; ++i) {
    delete[] matrix[i];
}


You yourself were quite correct :

for( int y=0;y<8;y++)
        {
            for( int z=0;z<8;z++)
            {
             switch(y)
             {
             case 0:
             matrix[y][z] = arr_v1[z]; //Be pretty sure, possibly you are better than you believe
             break;                    // I've placed y before z as y is the outer loop, hence it 
             case 1:                   // should be responsible for ROWS and z for COLUMNS
             matrix[y][z] = arr_v2[z]; // Goes in matrix[1][0],[1][1],[1][2],[1][3]...[1][7]
             break;
             case 2:
             matrix[y][z] = arr_v3[z]; // Goes in matrix[2][0],[2][1],[2][2],[2][3]...[2][7]
             break;
             case 3:
             matrix[y][z] = arr_v4[z]; // Goes on
             break;
             case 4:
             matrix[y][z] = arr_v5[z]; // And on
             break;
             case 5:
             matrix[y][z] = arr_v6[z]; // And on
             break;
             case 6:
             matrix[y][z] = arr_v7[z];
             break;
             case 7:
             matrix[y][z] = arr_v8[z];
             break;
             }
            }  // Finally all 8 1x8 arrays stored into single 8x8 matrix
        }

Hope this helps, if it doesn't just let me know, I'd be happy to help.


char matrix[8][8];
char *arr[8] = {arr_v1, arr_v2, arr_v3, arr_v4, arr_v5, arr_v6, arr_v7, arr_v8};
for( int y=0;y<8;y++) {
    for (int i = 0; i < 8; ++i) {
        matrix[y][i] = arr[i][y];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消