Здравствуйте! Скажите, пожалуйста, в чем моя проблема?? Пытаюсь транспонировать матрицу разной размерности main.cpp
| Код | int main() { const size_t row = 3; const size_t column = 4; const size_t matrixSize = row * column;
float *inMatrix = new float[matrixSize]; float *outMatrix = new float[matrixSize];
fillMatrix(inMatrix, row, column); printMatrix(inMatrix, row, column); transpose(inMatrix, outMatrix, row, column); printMatrix(outMatrix, column, row);
delete[] inMatrix; delete[] outMatrix;
getchar();
return 0; }
|
cpu.cpp
| Код | void fillMatrix(float *matrix, const size_t row, const size_t column) { std::srand(std::time(0));
const float X = 100.0f;
for (size_t i = 0; i < row; i++) { for (size_t j = 0; j < column; j++) { matrix[i * column + j] = static_cast<float>(std::rand() / static_cast<float>(RAND_MAX / X)); } } }
void printMatrix(float *matrix, const size_t row, const size_t column) { std::cout << "\n"; for (size_t i = 0; i < row; i++) { for (size_t j = 0; j < column; j++) { std::cout << std::setw(10) << matrix[i * column + j]; } std::cout << "\n"; } }
void transpose(float *inMat, float *outMat, const size_t row, const size_t column) { for (size_t i = 0; i < row; i++) { for (size_t j = 0; j < column; j++) { outMat[i * row + j] = inMat[j * column + i]; } } }
|
cpu.h
| Код | void fillMatrix(float *matrix, const size_t row, const size_t column); void printMatrix(float *matrix, const size_t row, const size_t column); void transpose(float *inMat, float *outMat, const size_t row, const size_t column);
|
Результат программа выдает верный, но не до конца. Например при размерности 3 на 4
| Код | 24.6071 65.5538 64.5619 64.6474 35.1207 75.3258 58.3666 72.6402 66.5365 43.9955 76.3176 8.8229
24.6071 35.1207 66.5365 65.5538 75.3258 43.9955 64.5619 58.3666 76.3176 -1.21979e-012-4.31602e+008-4.31602e+008
|
А при 4 на 3 вообще падает |