Using namespace std;
void shet(int arr[][2], int num);
class Massiv{
protected: int array[][2], size;
public:
Massiv() {
array[0][0]=0;
array[0][1]=1;
array[1][0]=2;
array[1][1]=3;
}
Massiv(int a): size(2) {
{
for(int i=0; i<size; i++)
for (int j=0; j<size; j++)
array[i][j]=rand()%(2*a-a);
}
}
Massiv(int c1, int c2, int c3, int c4) {
array[0][0]=c1;
array[0][1]=c2;
array[1][0]=c3;
array[1][1]=c4;
}
void EnterMassiv () {
cout<<"Полученная матрица: \n";
shet(array, 2);
cout<<"\n";
}
void TransMassiv() {
int clone[2][2];
for(int i=0; i<2; i++)
for (int j=0; j<2; j++) {
clone[i][j] = array[i][j];
}
swap(clone[0][1], clone[1][0]);
cout<<"\n";
shet(clone, 2);
}
};
void shet(int arr[][2], int num)
{
for(int i=0; i<num; i++){
cout<<endl;
for (int j=0; j<num; j++) cout<<setw(3)<<arr[i][j]<<" ";}
}
C/C++
Почему не копируется массив (clone)? И можно ли это сделать другим способом?
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
template<const size_t Rows, const size_t Columns>
class Matrix {
public:
Matrix() { for (auto& row : matrix_) for (auto& value : row) value = 0; }
void show(const streamsize width)const {
for (const auto& row : matrix_) {
for (const auto value : row) cout << setw(width) << value;
puts("");
}
puts("");
}
void random_fill(int left, int right) {
Random_ rand;
for (auto& row : matrix_) for (auto& value : row) value = rand.next(left, right);
}
Matrix operator+(const Matrix& mx) {
Matrix<Rows, Columns> box = *this;
for (auto i = 0U; i < Rows; ++i) {
for (auto j = 0U; j < Columns; ++j) {
box.matrix_[i][j] += mx.matrix_[i][j];
}
}
return box;
}
private:
int matrix_[Rows][Columns];
class Random_ {
public:
Random_() {
random_device device;
random_generator_.seed(device());
}
int next(int first, int last) {
uniform_int_distribution<int> range(first, last);
return range(random_generator_);
}
private:
mt19937 random_generator_;
};
};
int main() {
Matrix<3, 5> a, b, c;
a.random_fill(10, 49);
b.random_fill(10, 49);
c = a + b;
Matrix<3, 5> d = c;
a.show(4);
b.show(4);
c.show(4);
d.show(4);
system("pause > nul");
}
#include <iomanip>
#include <random>
using namespace std;
template<const size_t Rows, const size_t Columns>
class Matrix {
public:
Matrix() { for (auto& row : matrix_) for (auto& value : row) value = 0; }
void show(const streamsize width)const {
for (const auto& row : matrix_) {
for (const auto value : row) cout << setw(width) << value;
puts("");
}
puts("");
}
void random_fill(int left, int right) {
Random_ rand;
for (auto& row : matrix_) for (auto& value : row) value = rand.next(left, right);
}
Matrix operator+(const Matrix& mx) {
Matrix<Rows, Columns> box = *this;
for (auto i = 0U; i < Rows; ++i) {
for (auto j = 0U; j < Columns; ++j) {
box.matrix_[i][j] += mx.matrix_[i][j];
}
}
return box;
}
private:
int matrix_[Rows][Columns];
class Random_ {
public:
Random_() {
random_device device;
random_generator_.seed(device());
}
int next(int first, int last) {
uniform_int_distribution<int> range(first, last);
return range(random_generator_);
}
private:
mt19937 random_generator_;
};
};
int main() {
Matrix<3, 5> a, b, c;
a.random_fill(10, 49);
b.random_fill(10, 49);
c = a + b;
Matrix<3, 5> d = c;
a.show(4);
b.show(4);
c.show(4);
d.show(4);
system("pause > nul");
}
Переделывать.
int array[][2] - flexible массивы поддерживаются далеко не каждым компилятором C++ и нестандартная фича. Тем более тут судя по всему совершенно не нужная.
не делайте ввод и вывод класса "массив" методами внутри класса. Это не их работа. Н-р у меня этот класс будет использоваться не в консольном приложении а в GUI и что Вы будете делать с cin / cout?
Что за пассаж rand()%(2*a-a); 2*a - a?
Хотелось бы видеть задание и полный код.
int array[][2] - flexible массивы поддерживаются далеко не каждым компилятором C++ и нестандартная фича. Тем более тут судя по всему совершенно не нужная.
не делайте ввод и вывод класса "массив" методами внутри класса. Это не их работа. Н-р у меня этот класс будет использоваться не в консольном приложении а в GUI и что Вы будете делать с cin / cout?
Что за пассаж rand()%(2*a-a); 2*a - a?
Хотелось бы видеть задание и полный код.
Похожие вопросы
- Почему вместо двумерного массива мы используем двойной указатеть?
- Выполните сортировку массивов ТРЕМЯ СПОСОБАМИ: методом пузырька, прямого поиска и быстрой сортировкой. С++
- Выполните сортировку массивов ТРЕМЯ СПОСОБАМИ: методом пузырька, прямого поиска и быстрой сортировкой. С++
- Почему нельзя освободить выделенную память для элемента массива или как это сделать правильно?
- Измерение времени на си. Почему-то скорость сортировки массива выводится со второго раза.. И еще надо измерить память
- Почему динамический массив напрямую в функцию передать нельзя, а динамический массив указателей можно?
- Помогите исправить и дописать программу с массивами на языке C++. Буду благодарен, т. к. самому уже не понятно.
- Заменить нулями элементы массива, которые расположены между первым минимальным и последним максимальным элементами масси
- Нужна помощь в составлении одномерных массивов С++
- Кольцевой сдвиг массива