Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- size_t inputRowsCount(size_t&);
- size_t inputColumnsCount(size_t&);
- void inputElementsIntoTheArray(double**, size_t, size_t);
- void transposeTheSquaredArray(double**, size_t);
- void transposeTheNonSquaredArray(double**&, size_t&, size_t&);
- void swapTheRowsAndTheColumns(size_t&, size_t&);
- void printTheArray(double**, size_t, size_t);
- void printArrayAsTransposedOne(double**, size_t, size_t);
- void disposeTheArray(double**, size_t);
- int main()
- {
- size_t rowsCount = 0;
- inputRowsCount(rowsCount);
- size_t columnsCount = 0;
- inputColumnsCount(columnsCount);
- system("cls");
- std::cout << "Rows count: " << rowsCount << "\nColumns count: " << columnsCount << "\n\n\nThe array is a " << ( (rowsCount == columnsCount) ? "square.\n\n\n" : "rectangle.\n\n\n" );
- double** numbers = new double* [rowsCount];
- for (size_t i = 0; i < rowsCount; i++)
- {
- numbers[i] = new double[columnsCount];
- }
- std::cout << "Input elements into the array:\n\n";
- inputElementsIntoTheArray(numbers, rowsCount, columnsCount);
- std::cout << "\n\nPrinting the array:\n\n";
- printTheArray(numbers, rowsCount, columnsCount);
- std::cout << "\n\n\nPrinting the array as a transposed one:\n\n";
- printArrayAsTransposedOne(numbers, rowsCount, columnsCount);
- std::cout << "\n\n\nTransposing the array ....\n\n";
- if (rowsCount == columnsCount)
- {
- transposeTheSquaredArray(numbers, rowsCount);
- }
- else
- {
- transposeTheNonSquaredArray(numbers, rowsCount, columnsCount);
- }
- std::cout << "\nPrinting the array:\n\n";
- printTheArray(numbers, rowsCount, columnsCount);
- disposeTheArray(numbers, rowsCount);
- return 0;
- }
- size_t inputRowsCount(size_t& rowsCount)
- {
- system("cls");
- std::cout << "Input the desired rows: ";
- std::cin >> rowsCount;
- if (rowsCount < 1)
- {
- inputRowsCount(rowsCount);
- }
- return rowsCount;
- }
- size_t inputColumnsCount(size_t& columnsCount)
- {
- system("cls");
- std::cout << "Input the desired columns: ";
- std::cin >> columnsCount;
- if (columnsCount < 1)
- {
- inputColumnsCount(columnsCount);
- }
- return columnsCount;
- }
- void inputElementsIntoTheArray(double** numbers, size_t rowsCount, size_t columnsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- std::cin >> numbers[i][j];
- }
- if (i < (rowsCount - 1))
- {
- std::cout << "\n";
- }
- }
- }
- void transposeTheSquaredArray(double** numbers, size_t rowsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = i + 1; j < rowsCount; j++)
- {
- numbers[i][j] += numbers[j][i];
- numbers[j][i] = numbers[i][j] - numbers[j][i];
- numbers[i][j] -= numbers[j][i];
- }
- }
- }
- void transposeTheNonSquaredArray(double**& numbers, size_t& rowsCount, size_t& columnsCount)
- {
- double** numbersCopy = new double * [columnsCount];
- for (size_t i = 0; i < columnsCount; i++)
- {
- numbersCopy[i] = new double[rowsCount];
- }
- for (size_t i = 0; i < columnsCount; i++)
- {
- for (size_t j = 0; j < rowsCount; j++)
- {
- numbersCopy[i][j] = numbers[j][i];
- }
- }
- disposeTheArray(numbers, rowsCount);
- swapTheRowsAndTheColumns(rowsCount, columnsCount);
- numbers = new double * [rowsCount];
- for (size_t i = 0; i < rowsCount; i++)
- {
- numbers[i] = new double[columnsCount];
- }
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- numbers[i][j] = numbersCopy[i][j];
- }
- }
- disposeTheArray(numbersCopy, rowsCount);
- }
- void swapTheRowsAndTheColumns(size_t& rowsCount, size_t& columnsCount)
- {
- rowsCount += columnsCount;
- columnsCount = rowsCount - columnsCount;
- rowsCount -= columnsCount;
- }
- void printTheArray(double** numbers, size_t rowsCount, size_t columnsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- std::cout << numbers[i][j];
- if (j < (columnsCount - 1))
- {
- std::cout << " ";
- }
- }
- if (i < (rowsCount - 1))
- {
- std::cout << "\n";
- }
- }
- }
- void printArrayAsTransposedOne(double** numbers, size_t rowsCount, size_t columnsCount)
- {
- for (size_t j = 0; j < columnsCount; j++)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- std::cout << numbers[i][j];
- if (i < (rowsCount - 1))
- {
- std::cout << " ";
- }
- }
- if (j < (columnsCount - 1))
- {
- std::cout << "\n";
- }
- }
- }
- void disposeTheArray(double** array, size_t rowsCount)
- {
- for (size_t i = 0; i < rowsCount; i++)
- {
- delete[] array[i];
- }
- delete[] array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement