To add the diagonals of a matrix there is a condition:
The matrix must be a Matrix Cuadrada , this means that both the number of rows as the number of columns must be equal, since only thus, the values are taken as diagonal values. Otherwise, you can not execute the operation of adding the diagonals.
Just do a simple desktop test . This is just visualizing the matrix that you are going to create as an arrangement of positions and evaluate what conditions you need.
To add the first diagonal, it is enough to realize that each index of the number in the diagonal of the row is equal to the index of the column. Therefore, adding the diagonal simply needs a to advance the row and the column at the same time is added to the value stored in those positions of the matrix.
For the second diagonal, it's a little different. Here you realize that each number is in the limit index of both the row and the column respectively. So you would have to go through a cycle, where the index of the row starts at the end of the row and not at zero and the index of the column at zero. This will cause you to scroll through the values of the second diagonal in a single cycle. In this case you simply need a to advance the values at the same time, but both in the opposite direction, starting the row at the end and the column at the beginning (reversed) .
Molding a bit your code would look like this:
#include <iostream>
#include <stdlib.h>
using namespace std;
const int filas = 4;
const int columnas = 4;
int SumarPrimeraDiagonal(int matriz[filas][columnas]){
int sumaDiagonal = 0;
for(int i = 0, j = 0; i < filas && j < columnas; i++, j++)
sumaDiagonal += matriz[i][j];
// Otra forma alternativa, como filas = columnas para poder sumar
/*
for(int i = 0; i < filas; i++)
sumaDiagonal += matriz[i][i];
*/
return sumaDiagonal;
}
int SumarSegundaDiagonal(int matriz[filas][columnas]){
int sumaDiagonal = 0;
for(int i = filas - 1, j = 0; i >= 0 && j < columnas; i--, j++)
sumaDiagonal += matriz[i][j];
return sumaDiagonal;
}
int main(){
int matriz[filas][columnas];
for(int i = 0; i < filas; i++){
cout<<"\n\n";
for(int j = 0; j < columnas; j++){
cout << "Ingrese numero para la posicion" << "[" << i << ", " << j << "]:";
cin >> matriz[i][j];
cout << endl;
}
}
cout << "\n\n";
for(int i = 0; i < filas; i++){
for(int j = 0; j < columnas; j++){
cout << matriz[i][j] << " ";
}
cout << "\n";
}
cout << endl;
if(filas != columnas)
cout << "La matriz debe ser cuadrada para poder sumas sus diagonales" << endl;
else{
int primeraDiagonal = SumarPrimeraDiagonal(matriz);
int segundaDiagonal = SumarSegundaDiagonal(matriz);
cout << "Suma de la primera diagonal: " << primeraDiagonal << endl;
cout << "Suma de la segunda diagonal: " << segundaDiagonal << endl;
cout << "Suma de ambas diagonales: " << primeraDiagonal + segundaDiagonal << endl;
}
}
PD: Keep in mind that I defined the variables rows and columns as constants because in your code you defined them consistently equally, this in order to be able to pass the two-dimensional array without problems, many alternatives, but I decided to create global constant variables. If in any case you want to assign rows and columns dynamically (reading the values) and do not have much knowledge in programming, you can pass the code of the methods SumarPrimeraDiagonal
and SumarSegundaDiagonal
to the end of main()
and the constant variables declare them normal in it. It will work for you without problems.
To test matrices of other sizes, change the number of rows and columns constant:
const int filas = #Numero que quieras de filas;
const int columnas = #Numero que quieras de columnas;