Top.Mail.Ru
Ответы

Нужны идеи по поводу этого

Пользователь задает размер квадратной матрицы с элементами типа double. Написать следующие функции: выделение требуемого количества памяти; освобождение памяти; заполнение матрицы с клавиатуры; заполнение матрицы псевдослучайными числами; вывод матрицы на экран; вычисление определителя; возвращение матрицы, умноженной на заданное число.

Только авторизированные пользователи могут оставлять свои ответы
Дата
Популярность
Аватар пользователя
Новичок
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
 #include <iomanip>
#include <iostream>
#include <random>

using namespace std;

static double* create(
    const size_t length
) {
    auto pointer = new(nothrow) double[length];
    if (pointer != nullptr) {
        for (size_t i = 0; i < length; ++i) {
            pointer[i] = 0;
        }
    }
    return pointer;
}

static void destroy(
    double*& pointer
) {
    if (pointer != nullptr) {
        delete[] pointer;
        pointer = nullptr;
    }
}

static double** create(
    const size_t rows,
    const size_t columns
) {
    auto matrix = new(nothrow) double* [rows];
    if (matrix != nullptr) {
        for (size_t i = 0; i < rows; ++i) {
            matrix[i] = create(columns);
            if (matrix[i] == nullptr) {
                for (size_t j = 0; j < i; ++j) {
                    destroy(matrix[j]);
                }
                delete[] matrix;
                matrix = nullptr;
                break;
            }
        }
    }
    return matrix;
}

static void destroy(
    double**& matrix,
    const size_t rows
) {
    if (matrix != nullptr) {
        for (size_t i = 0; i < rows; ++i) {
            destroy(matrix[i]);
        }
        delete[] matrix;
        matrix = nullptr;
    }
}

static void fill_from_keyboard(
    double** matrix,
    const size_t rows,
    const size_t columns
) {
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < columns; ++j) {
            cin >> matrix[i][j];
        }
    }
}

static void fill_random(
    double** matrix,
    const size_t rows,
    const size_t columns
) {
    uniform_int_distribution<> uid(10, 99);
    mt19937 gen{ random_device()() };
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < columns; ++j) {
            matrix[i][j] = uid(gen) / 10.0;
        }
    }
}

static void show_matrix(
    double** matrix,
    const size_t rows,
    const size_t columns,
    const streamsize width
) {
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < columns; ++j) {
            cout << setw(width) << matrix[i][j];
        }
        cout.put('\n');
    }
    cout.put('\n');
}

static double** multiplying_by_a_number(
    double** matrix,
    const size_t rows,
    const size_t columns,
    double value
) {
    auto new_matrix = create(rows, columns);
    if (new_matrix != nullptr) {
        for (size_t i = 0; i < rows; ++i) {
            for (size_t j = 0; j < columns; ++j) {
                new_matrix[i][j] = matrix[i][j] * value;
            }
        }
    }
    return new_matrix;
}

int main() {
    constexpr size_t rows = 3;
    constexpr size_t columns = 4;
    auto matrix = create(rows, columns);
    if (matrix != nullptr) {
        constexpr streamsize width = 8;
        constexpr auto value = 2.5;
        cout.setf(ios::fixed);
        fill_random(matrix, rows, columns);
        cout.precision(1);
        show_matrix(matrix, rows, columns, width);
        auto new_matrix = multiplying_by_a_number(matrix, rows, columns, value);
        cout.precision(2);
        show_matrix(new_matrix, rows, columns, width);
        destroy(new_matrix, rows);
        destroy(matrix, rows);
    } else {
        cout << "Error: not enough memory!\n";
    }
} 

P.S. Определитель матрицы: http://mindhalls.ru/matrix-determinant-calculation-recursive