Вопрос пользователя:
Ответ нейросети:
#include <iostream> #include <cstdlib>
template <typename T> class Matrix { private: int rows; int cols; T **data;
public: Matrix(int rows, int cols) { this->rows = rows; this->cols = cols; data = new T*[rows]; for (int i = 0; i < rows; i++) { data[i] = new T[cols]; } }
~Matrix() {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
void fillRandom() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = rand() % 100;
}
}
}
void fillFromKeyboard() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << "Enter value for element (" << i << ", " << j << "): ";
std::cin >> data[i][j];
}
}
}
void display() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
Matrix operator+(const Matrix& other) {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix& other) {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] - other.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& other) {
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
result.data[i][j] = 0;
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
Matrix operator/(const T& scalar) {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] / scalar;
}
}
return result;
}
T findMax() {
T max = data[0][0];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] > max) {
max = data[i][j];
}
}
}
return max;
}
T findMin() {
T min = data[0][0];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] < min) {
min = data[i][j];
}
}
}
return min;
}
};
int main() { Matrix<int> mat1(2, 2); mat1.fillRandom(); mat1.display();
Matrix<int> mat2(2, 2);
mat2.fillRandom();
mat2.display();
Matrix<int> mat3 = mat1 + mat2;
mat3.display();
Matrix<int> mat4 = mat1 - mat2;
mat4.display();
Matrix<int> mat5 = mat1 * mat2;
mat5.display();
Matrix<int> mat6 = mat1 / 2;
mat6.display();
std::cout << "Max element in mat1: " << mat1.findMax() << std::endl;
std::cout << "Min element in mat1: " << mat1.findMin() << std::endl;
return 0;
}
Напишите нам, если в вопросе есть ваши персональные данные ([email protected])
Обратите внимание: ответы, предоставляемые искусственным интеллектом, могут не всегда быть точными. Не рассчитывайте на них в критически важных областях, таких как медицина, юриспруденция, финансы или в вопросах, связанных с безопасностью. Для важных решений всегда обращайтесь к квалифицированным специалистам. Администрация сайта не несет ответственности за контент, сгенерированный автоматически.