C/C++

Функция Raznost - почему не работает? Меняются только 00 и 10 эл-ты исходного массива

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;
}
};

Class MatrixChild:public Massiv
{
public: MatrixChild():Massiv(){};
MatrixChild(int a):Massiv(a) {};
MatrixChild(int c1, int c2, int c3, int c4):Massiv(c1, c2, c3, c4) {};

int Sravnenie (MatrixChild ob)
{
int ravenstvo=0;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
if(array[i][j]==ob.array[i][j]) ravenstvo++;

if(ravenstvo) return 1;
else return 0;
}

void Raznost (MatrixChild ob)
{
int B[2][2];
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
B[i][j]=array[i][j]-ob.array[i][j];

shet(B,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]<<" ";}
}
#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;
}
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;
a.show(4);
b.show(4);
c.show(4);
system("pause > nul");
}
Дима Глазунков
Дима Глазунков
53 608
Лучший ответ
protected: int array[][2], size;
вот здесь array - это flexible array member, такие конструкции не являются частью стандарта языка и реализованы только в качестве расширений отдельных компиляторов
у меня, например, этот фрагмент кода вообще не компилится, т. к. gcc требует, чтобы fam был последним членом класса, а тут после него ещё и size
не нужно использовать эту ерунду
если ты пытаешься изобрести динамический массив, лучше используй std::vector
AT
Aslan Tumyshov
36 956
Алексей Геннадьевич Бабенков спасибо. вы действительно помогли)