School-Coding-Cpp/sfusi/SommaDeiNeriEBianchi.cpp

39 lines
1.2 KiB
C++

//Patriche Robert Cosmin
#include <iostream>
using namespace std;
void sommaScacchiera(int matrice[5][5], int n) {
int sommaNera = 0, sommaBianca = 0;
// In una scacchiera, le celle nere sono quelle che soddisfano la condizione (i + j) % 2 == 0
// Le celle bianche sono quelle che soddisfano (i + j) % 2 == 1
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i + j) % 2 == 0) {
sommaNera += matrice[i][j]; // Cella nera
} else {
sommaBianca += matrice[i][j]; // Cella bianca
}
}
}
cout << "Somma delle celle nere: " << sommaNera << endl;
cout << "Somma delle celle bianche: " << sommaBianca << endl;
if (sommaNera > sommaBianca) {
cout << "La somma delle celle nere è maggiore." << endl;
} else {
cout << "La somma delle celle bianche è maggiore." << endl;
}
}
int main() {
int matrice[5][5] = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
sommaScacchiera(matrice, 5);
return 0;
}