Version:0.9 StartHTML:0000000105 EndHTML:0000012156 StartFragment:0000000152 EndFragment:0000012122
/******************************************************/
/*************** Isidro Pastor Jorda ******************/
/******************************************************/
/****************** Telematica ************************/
/******************************************************/
/************* Practica 6 Ejercicio 6 *****************/
/******************************************************/


#include <iostream.h>
#include <stdlib.h>

const int MAXFILAS = 10;
const int MAXCOLUM = 10;

typedef float Matriz[MAXFILAS][MAXCOLUM];
typedef float Vector[3];

void ContarPosNeg(Matriz M, Vector v, int filas, int columnas) ;
void InicializaMatriz(Matriz M, int reset);
void MostrarMatriz(Matriz M, int filas, int columnas);
void GrabarMatriz(Matriz M, int filas, int columnas);



int main()
{
    int filas, columnas;
    Matriz M;
    char sino;
    Vector v = {0,0,0};

    do
    {
        cout << "\n Introduce las filas que quieres para la matriz -> ";
        cin >> filas;
    }
    while ( filas < 0 || filas > MAXFILAS);
    do
    {
        cout << "\n Introduce las columnas que quieres para la matriz -> ";
        cin >> columnas;
    }
    while ( columnas < 0 || columnas > MAXCOLUM);

    InicializaMatriz(M, 0);
    GrabarMatriz(M, filas, columnas);

    cout << "\n  Quieres sumar los elementos negativos y los positivos de la matriz ? ( S/N ) -> ";
    cin >> sino ;

    if ( sino == 's' || sino == 'S')
        ContarPosNeg(M, v, filas, columnas);



    if (v[1] != 0 )
        cout << " \n El numero de elementos positivos sumados es de " << v[1];
    else
        cout << " \n No hay numeros positivos en la matriz ";
    if (v[2] != 0 )
        cout << " \n El numero de elementos negativos sumados es de " << v[2];
    else
        cout << " \n No hay numeros negativos en la matriz ";

    if ( v[0] !=0 )
        cout << " \n El numero de elementos nulos encontrados es de " << v[0];

    cout << endl << endl;

    system("PAUSE");
    return 0;
}

/******************************************************************
* Funcion: InicializaMatriz
* Descripcion: Funcion que inicializa una matriz al valor de reset
*
*
* Parametros:
*
* Nombre      E/S       Descripcion
* ------     -----      -----------
*   M         E/S        Matriz a inicializar
* reset       E          Valor usado para inicializar la matriz
*
* Valor devuelto:
*       void, la matriz no devuelve nada pero modifica el vector
*******************************************************************/

void InicializaMatriz(Matriz M, int reset)
{

    for (int i = 0; i < MAXFILAS; i++ )
        for (int j = 0; j< MAXCOLUM; j++)
            M[i][j] = reset;

    return;
}



/******************************************************************
* Funcion: GrabarMatriz
* Descripcion: Funcion para grabar en la matriz los datos introducidos
*              por teclado.
*
* Parametros:
*
* Nombre      E/S       Descripcion
* ------     -----      -----------
*   M         E/S        Matriz que iremos asignando valores leidos por teclado
*
*
* Valor devuelto:
*       void, no devuelve nada la funcion pero modifica la matriz M
*******************************************************************/

void GrabarMatriz(Matriz M, int filas, int columnas)
{
    for (int i = 0; i < filas; i++ )
        for (int j = 0; j< columnas; j++)
        {
            cout << " Introduce el valor para la entrada "<< i << " " << j << endl;
            cin >> M[i][j];
            system("cls");
            MostrarMatriz(M, filas, columnas);
        }
}


/******************************************************************
* Funcion: MostrarMatriz
* Descripcion: Funcion para mostrar por pantalla la matriz tabulada
*
*
* Parametros:
*
* Nombre      E/S       Descripcion
* ------     -----      -----------
*   M         E          Matriz a mostrar por pantalla
*
*
* Valor devuelto:
*       void, la funcion solo muestra por pantalla la matriz
*******************************************************************/

void MostrarMatriz(Matriz M, int filas, int columnas)
{
    cout << endl << endl;
    for (int i = 0; i < filas; i++ )
    {    for (int j = 0; j< columnas; j++)
         {
             if (M[i][j] < 10) // Si el numero es menos que 10 introducimos 3 espacios para que quede bien la salida por pantalla
                cout << "   " << M[i][j];  
             else
                 if (M[i][j] < 100) // Si es mas pequeño que 100 introduciremos el numero con solo 2 espacios de separacion
                     cout << "  " << M[i][j];
                 else
                     cout << " " << M[i][j]; // Y si fuera mayor que 100 entonces un espacio simple para separar las columnas
         }
        cout << endl;
    }
    cout << endl;
}





void ContarPosNeg(Matriz M, Vector v, int filas, int columnas)
{
    for (int i = 0; i < filas; i++ )
        for (int j = 0; j< columnas; j++)
        {
            if ( M[i][j] > 0)
                v[1] = v[1] + M[i][j];
            if ( M[i][j] < 0)
                v[2] = v[2] + M[i][j];
            if ( M[i][j] == 0 )
                v[0]++;


        }

    return;
}