arrays - Switching Matrix columns and rows in C++ -
i have 6 x 6 matrix, , storing values in 1 dimensional array of size 36. want rearrange rows columns , columns rows. method trying copy values array, sorted properly. trying loop:
(int = 0; < 6; ++i){ copyarray[i]= array[i*6]; }
this works fine first new row made of first column, how continue of them? have tried nested loops cannot come proper algorithm using iterators. manually, have done code.
i coding in c++, if can in similar language fine. feel math problem.
the question: how solve switching out rows , columns? (example: if denoting first rows , columns 0, in 6x6 matrix, both rows , columns go 0 5. therefore, switching rows , columns, value in row 2, column 5 switched value in row 5, column 2.)
couldn't use nested for
loop , this?
for (int = 0; < 6; ++i) (int j = 0; j < 6; ++j) copyarray[i*6+j]= array[j*6+i];
here's test program can run show works:
#include <stdio.h> int main() { int array[36] = {1,1,1,1,1,1, 2,2,2,2,2,2, 3,3,3,3,3,3, 4,4,4,4,4,4, 5,5,5,5,5,5, 6,6,6,6,6,6}; int copyarray[36]; (int = 0; < 6; ++i) (int j = 0; j < 6; ++j) copyarray[i*6+j]= array[j*6+i]; (int = 0; < 36; ++i) { if (i % 6 == 0) printf("\n"); printf("%d ", array[i]); } printf("\n"); printf("\n"); (int = 0; < 36; ++i) { if (i % 6 == 0) printf("\n"); printf("%d ", copyarray[i]); } return 0; }
output:
1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Comments
Post a Comment