#include <iostream>
int m,n;
using namespace std;
int main(){
cout<<"Cuantas filas tiene su matriz: ";
cin>>m;
cout<<"Cuantas columnas tiene su matriz: ";
cin>>n;
int matriz[m][n];
//Es obvio...
cout<<"\n\n";
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cout<<"Ingrese el dato en posicion ["<<i+1<<"]["<<j+1<<"]: ";
cin>>matriz[i][j];
}
}
//Reviso si hay ceros en la matris para no contarlos en la fila de la dispersa...
int a=m*n;
int aux=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(matriz[i][j]==0){
aux++;
}
}
}
//Hallo la matriz dispersa...
a=a-aux;
int fil=1;
int col=0;
int c, d;
cout<<"\n\n";
int dispersa[a+2][3];
dispersa[0][0]=m-1;
dispersa[0][1]=n-1;
dispersa[0][2]=a;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(matriz[i][j]!=0){
dispersa[fil][col]=i;
dispersa[fil][col+1]=j;
dispersa[fil][col+2]=matriz[i][j];
fil++;
}
d=j;
}
c=i;
}
dispersa[fil][col]=c+1;
dispersa[fil][col+1]=d+1;
dispersa[fil][col+2]=0;
//Muestro las matrices...
//Primero la matriz normal...
cout<<"Matriz:\n";
for(int i=0; i<m; i++){
cout<<endl;
for(int j=0; j<n; j++){
cout<<matriz[i][j]<<" ";
}
}
cout<<"\n\n";
//Ahora la dispersa...
cout<<"Matriz dispersa:\n";
for(int i=0; i<a+2; i++){
cout<<endl<<" "<<dispersa[i][0]+1<<" "<<dispersa[i][1]+1<<" "<<dispersa[i][2];
}
//Compruebo si la matriz puede ser triangular superior o identidad con una pregunta...
int aux1=0;
int aux2=0;
int aux3=0;
int aux4=0;
if(m==n){
//Para ver si es triangular superior...
for(int i=0; i<a+2; i++){
if(dispersa[i][0]>dispersa[i][1]){
aux1=1;
}
}
//Para ver si es triangular inferior...
for(int i=0; i<a+2; i++){
if(dispersa[i][0]<dispersa[i][1]){
aux2=1;
}
}
//para ver si es identidad...
if(m==a){
for(int i=0; i<a+2; i++){
for(int j=0; j<3; j++){
if(i==j){
if(dispersa[i][j]==1){
aux4=1;
}
else{
aux4=0;
}
}
}
}
}
}
if(aux4==1){
cout<<"\nLa matriz es identidad...\n\n";
}
else{
if(aux1==0){
cout<<"\nLa matriz es triangular superior...\n\n";
}
else if(aux2==0){
cout<<"\nLa matriz es triangular inferior...\n\n";
}
}
cout<<"\n\n";
system("pause");
}