Error in TAD operations C ++ matrix

0

I am implementing some operations for a matrix TAD, the operations are: addition, subtraction, multiplication and transpose.

Most of the TAD is already programmed in the exercise, I just have to define and implement those operations.

The code is as follows:

In the header:

#ifndef MATRIZ_HPP
#define MATRIZ_HPP

#include <stdexcept>
#include <iostream>
#include <iomanip>

#define F 3
#define C 3

class Matriz
{
public:

  // Definición de las clases de excepciones
  class ECoordenadasIncorrectas: public std::runtime_error
  {
  public:
    ECoordenadasIncorrectas(const std::string& w = "ECoordenadasIncorrectas"): std::runtime_error(w) {}
  };

  Matriz();

  ~Matriz();

  float valor(int i, int j);

  void asignar(int i, int j, float v);

  template <typename ModificaElemento>
  void modificar(ModificaElemento modifica_elemento);

  Matriz& operator=(Matriz& m);

  /** Constructor de copia */
  Matriz(Matriz& m);

  bool operator==(Matriz& m);
  bool operator!=(Matriz& m);    

  //ESTA ES LA PARTE QUE YO ESTOY IMPLEMENTANDO
  void identidad();
  void ceros();
  void unos();
  void multiplicar(Matriz m, Matriz& salida);
  void sumar(Matriz m, Matriz& salida);
  void restar(Matriz m, Matriz& salida);
  void trasponer(Matriz& salida);
  int filas();
  int columnas();
  //HASTA AQUÍ
private:
  float elementos_[F][C];
};

// En las templates (TADs genéricos) hacemos la inclusión al revés el .cpp en el .hpp (para que
// al incluir el matrices.hpp vaya todo el código y se pueda hacer la instanciación de la
// template).
#include "matriz.cpp"
#include "matriz_io.cpp"

#endif // MATRIZ_HPP

The cpp of the entry and exit from which nothing can be touched (it is done):

std::ostream& operator<<(std::ostream& os, Matriz & m)
{
  os << m.filas() << " " << m.columnas() << std::endl;
  os << std::setprecision(4) << std::fixed;
  for(int i=1; i <= m.filas(); i++)
  {
    for(int j=1; j <= m.columnas(); j++)
    {
      os << m.valor(i,j) << " ";
    }
    os << std::endl;
  }

  return os;
}

std::istream& operator>>(std::istream& is, Matriz& m)
{
  int filas, columnas;
  float v;

  is >> filas >> columnas;
  for (int i=1; i<=filas; i++)
  {
    for (int j=1; j<=columnas; j++)
    {
      is >> v;
      m.asignar(i,j,v);
    }
  }

  return is;
}

The cpp with the implementation of the operations (only I put mine):

void Matriz::unos()
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            elementos_[i][j]=1;
        }
    }
}

int Matriz::columnas()
{
    return C;
}

int Matriz::filas()
{
    return F;
}

void Matriz::sumar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]+=m.elementos_[i][j];
        }
    }

}

void Matriz::restar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]-=m.elementos_[i][j];
        }
    }
}
void Matriz::multiplicar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            for(int k=0; k<C;k++)
            {
                salida.elementos_[i][j]+=m.elementos_[i][k]*salida.elementos_[k][j];
            }
        }
    }
}
void Matriz::trasponer(Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=salida.elementos_[j][i];
        }
    }
}

And the main.cpp to test all the operations (I only put the ones that affect my operations, everything is done here):

#include "matriz.hpp"

#define Elemento float

#define MatrizPrueba Matriz


void probarCrearMatriz()
{
  MatrizPrueba m;
  std::cout << m;
}
void probarMultiplicar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.multiplicar(m2, m3);

  std::cout << m3;
}

void probarSumar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.sumar(m2, m3);

  std::cout << m3;
}

void probarRestar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.restar(m2, m3);

  std::cout << m3;
}

void probarTrasponer(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;

  m1.trasponer(m2);

  std::cout << m2;
}
void probarUnos()
{
  MatrizPrueba m;
  m.unos();
  std::cout << m;
}
int main()
{
  char opcion;

  // Lectura de la operaci?n a probar
  std::cin >> opcion;

  try
  {
    switch(opcion)
    {
      case 'c':  probarCrearMatriz(); break;
      case 'i':  probarIdentidad(); break;
      case 'u':  probarUnos(); break;
      case 'z':  probarCeros(); break;
      case 'a':  probarAsignar(std::cin);  break;
      case 'v':  probarValor(std::cin); break;
      case 'm':  probarMultiplicar(std::cin); break;
      case 'r':  probarRestar(std::cin); break;
      case 's':  probarSumar(std::cin); break;
      case 't':  probarTrasponer(std::cin); break;
      case 'o':  probarModificar(std::cin); break;
      case 'C':  probarCopiar(std::cin); break;
      case '=':  probarIgual(std::cin); break;
      case 'D':  probarDestruir(std::cin); break;
    }
  }
  catch (std::exception const& excepcion)
  {
    std::cout << "EXCEPCION GENERADA: "  <<  excepcion.what() << std::endl;
  }
}

The error that I can not solve is the following:

All operations that are not mine except unos and ceros work perfectly, but when you get to the operations suma resta multiplicacion and trasponer , one of the matrices you have to enter for keyboard is not saved, and in suma , resta and multiplicación print me the 2nd matrix that I enter by keyboard (instead of the result of the operation of both matrices), in 'transpose' I printed a 3x3 matrix of 0's.

Can anyone see the error? I'll keep looking.

    
asked by AguaSal 21.06.2017 в 10:21
source

1 answer

0

In the end I found the solution. Thanks to anyone who has tried to find the error, it is long, but quite silly.

It turns out that the functions suma resta multiplicacion and trasponer receive some input parameters, among which is the parameter 'alida' that everyone shares.

Well, this parameter does not receive any keyboard information, it is simply to save the result of the current matrix with the matrix m (the other parameter).

Conclusion, (you just have to modify this part of the code) the code looks like this:

//LA OPERACIÓN DEL BUCLE FOR(de cada función) ES LO CORREGIDO

void Matriz::sumar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]+m.elementos_[i][j];
        }
    }

}

void Matriz::restar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]-m.elementos_[i][j];
        }
    }
}

void Matriz::multiplicar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            for(int k=0; k<C;k++)
            {
                salida.elementos_[i][j]+=elementos_[i][k]*m.elementos_[k][j];
            }
        }
    }
}

void Matriz::trasponer(Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[j][i];
        }
    }
}

It was just this part of the code in the .cpp. Corrections welcome.

    
answered by 22.06.2017 в 16:12