Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- #include <iostream>
- #include <cmath> // fabs
- using namespace std;
- // Вариант 1. Массив указателей на строки.
- bool is_digmatrix(double const* const* matrix, size_t rows, size_t cols)
- {
- for (size_t i = 0; i < rows; ++i)
- {
- int k=i,j=0;
- while(k<rows && j<cols){
- if(matrix[i][0]!=matrix[k][j])return false;
- k++;
- j++;
- }
- }
- for (size_t i = 0; i < cols; ++i)
- {
- int k=i,j=0;
- while(k<cols && j<rows){
- if(matrix[0][i]!=matrix[j][k])return false;
- k++;
- j++;
- }
- }
- // Все элементы "хорошие".
- return true;
- }
- // Вариант 2. Упакованный массив.
- bool is_digmatrix(double const matrix[], size_t rows, size_t cols)
- {
- for (size_t i = 0; i < cols*rows; i+=rows)
- {
- for (size_t j = i; j < cols*rows; j+=rows+1)
- {
- if(matrix[i]!=matrix[j]){return false;}
- }
- }
- for (size_t i = 0; i < cols; ++i)
- {
- for (size_t j = i; j < cols*rows; j+=rows+1)
- {
- if(matrix[i]!=matrix[j]){return false;}
- }
- }
- // Все элементы "хорошие".
- return true;
- }
- // Обёртка для теста упакованного массива как массива указателей.
- bool is_digmatrix_packed_adapter
- (double const matrix[], size_t rows, size_t cols)
- {
- // Создадим временный массив для хранения указателей на строки.
- auto r = new double const*[rows];
- for (size_t i = 0; i < rows; ++i, matrix += cols)
- r[i] = matrix;
- // Вызовем функцию is_01matrix.
- bool result = is_01matrix(r, rows, cols);
- // Удалим массив.
- delete[] r;
- // Вернём полученный результат.
- return result;
- }
- // Тест.
- int test_is_digmatrix
- (bool (&isdig)(double const matrix[], size_t rows, size_t cols))
- {
- double m[]
- {
- 1 2 3 4
- 2 1 2 3
- 3 2 1 2
- };
- if (!isdig(m, 3, 4))
- return 1;
- return 0;
- }
- int main()
- {
- cout << "is_digmatrix tests (should write 0 0): ";
- cout << test_is_digmatrix(is_digmatrix) << ' ';
- cout << test_is_digmatrix(is_digmatrix_packed_adapter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement