0
To perform the sum of items of a matrix (primary diagonal and secondary diagonal) I was able to write the following code. Can someone help me dry up the code? so I can compare the codes... C++: And I wonder if there’s any function for that kind of problem...
(It may be with other libraries than)
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;
}
Compare with what?
– Francisco
was not to compare, only to realize the sum of the diagonal Prim. and after the secund.
– Elder Son