diff options
Diffstat (limited to 'Year_1/Programming_1/h9_1.cc')
-rw-r--r-- | Year_1/Programming_1/h9_1.cc | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Year_1/Programming_1/h9_1.cc b/Year_1/Programming_1/h9_1.cc new file mode 100644 index 0000000..12c629d --- /dev/null +++ b/Year_1/Programming_1/h9_1.cc @@ -0,0 +1,27 @@ +#include <iostream> + +// Array NxM. Trovare media dei valori differenza tra gli elementi della diagonale principale e secondaria + +int main() { + const short N = 4, M = 3; + int A[N][M] = { + {3, 1, 5}, + {5, 3, 1}, + {8, 7, 4}, + {4, 7, 4}, + }; + + int diff1 = A[0][0], diff2 = A[N-1][0]; + + // Condizione necessaria per matrici in cui M != N + short cond = (N < M) ? N : M; + + for(int i = 1, j = N-1, j2 = 1; i < cond; ++i, --j, ++j2) { + diff1 -= A[i][i]; + diff2 -= A[j][j2]; + } + + std::cout << static_cast<float>((diff1+diff2)/2) << std::endl; + + return 0; +} |