#include #include typedef float Value; struct Matrix { int n; Value **data; }; Matrix Alloc_Matrix (int n); void Free_Matrix (Matrix &t); Value S_D (const Matrix &t, int i); int Max_Diagonal (const Matrix &t); void Read (std::istream &stream, Matrix &t, bool verbose); int main (int argc, char **argv) { Matrix t; Read (std::cin, t, true); std::cout << std::endl << "A legnagyobb osszegu atlo: " << Max_Diagonal(t) << std::endl; Free_Matrix (t); return 0; } Matrix Alloc_Matrix (int n) { Matrix t; assert (n >= 0); t.n = n; t.data = new Value*[n]; for (int i = 0; i != n; ++i) t.data[i] = new Value[n]; return t; } void Free_Matrix (Matrix &t) { for (int i = 0; i != t.n; ++i) delete[] t.data[i]; delete[] t.data; } Value S_D (const Matrix &t, int i) { Value s = 0; int k = 0; if (i >= 0) { while (k != t.n - i) { s += t.data[k][k + i]; ++k; } } else { while (k != t.n + i) { s += t.data[k - i][k]; ++k; } } return s; } int Max_Diagonal (const Matrix &t) { int i = -(t.n - 1); int k = i; Value max = S_D (t, -(t.n - 1)); while (i != (t.n - 1)) { Value s = S_D (t, i + 1); if (s >= max) { k = i + 1; max = s; } ++i; } return k; } // Testing framework starts here void Read (std::istream &stream, Matrix &t, bool verbose) { if (verbose) std::cout << "Matrix merete: "; int n; stream >> n; t = Alloc_Matrix (n); if (verbose) std::cout << "Matrix elemei" << std::endl; for (int i = 0; i != t.n; ++i) { for (int j = 0; j != t.n; ++j) { if (verbose) std::cout << "[" << i << "," << j << "]: "; stream >> t.data[i][j]; } } }