From d2edbc38cac8da52f58c5cd3da6c0c625fa05736 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 6 Feb 2021 19:56:36 +0100 Subject: conf: rename --- Year_1/Programming_1/h9_1.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Year_1/Programming_1/h9_1.cc (limited to 'Year_1/Programming_1/h9_1.cc') 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 + +// 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((diff1+diff2)/2) << std::endl; + + return 0; +} -- cgit v1.2.3-18-g5258