To sum the items of an array (primary diagonal and secondary diagonal) I was able to write the following code. Can someone help me make the code leaner? so I compare the codes ... C ++: And I would like to know if there is any function for this type problem ...
(Can be with other libraries that are not)
int main()
{
int matriz[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int somaP = 0;
for(int i = 0, j = 0; i < 3; i++, j++)
{
somaP += matriz[i][i];
}
cout << somaP << endl;
int somaS = 0;
for(int x = 0, y = 2; (x < 3) && (y > -1); x++, y--)
{
somaS += matriz[x][y];
}
cout << somaS << endl;
return 0;
}