Vector dispersed operations

0

I'm doing this class called matrix, and I want to work with dynamic arrays, istreams ostreams, with scattered vectors, but I have not been able to start, this is what I did, any idea is appreciated:

// Operaciones con matrices:suma,resta,multiplicación, inversa istream ostream

#pragma once

#include<iostream>
#include <cmath>

using namespace std;

class matriz {
private:
    const int m_ = 50; //filas primera matriz
    const int n_ = 50:

    const int f_ = 50; //filas segunda matriz
    const int c_ = 50;

    const int b_ = 50;//filas matriz suma
    const int e_ = 50;

    int A[m_][n_];
    int B[f_][c_];

    int C[b_][e_];

    double * val_;
    int*  inx_;
    int  nz_;
    int n_;

    int *M= new int [m_*n_]; // array dinamico primera matriz
    int *N= new int [f_*c_]; // array dinamico segunda matriz

public:

    //constructores
    matriz (const int m,const int n, const int f, const int c) {
        m_=m;
        n_=n;
        f_=f;
        c_=c;
    }

    ~matriz() {}
    //getters y setters
    void setm_ (const int m) {m_=m;}

    const int getm_(void) {return m_;}

    void setn_ (const int n) {n_=n}

    const int getn_(void) {return n_;}

    void setf_ (const int n) {n_=n;}
    const int getf_(void) {return f_;}

    void setc_(const int c) {c_=c;}
    const int getc_ (void) {return c_;}

    //métodos


    int suma(void);

    int resta(void);

    int multiplicacion(void);

    int inversa(void);
};

for (int i=0; i<

So far what I did, thank you very much thank you very much

    
asked by Robert 26.02.2017 в 12:56
source

1 answer

0

Errors or inconsistencies in your code:

  • The lines of code end with a semicolon, not with a colon

    const int n_ = 50:
    //               ^
    
  • The variables must be descriptive for the code to be readable. If you need a comment to know what function that variable has, you are doing something wrong.

    const int m_ = 50; // Que es m_? 
    const int n_ = 50; // y n_? que diferencia hay entre ambas?
    
    const int f_ = 50; // idem
    const int c_ = 50; // idem
    
    const int b_ = 50; // idem
    const int e_ = 50; // idem
    
  • If you want to work with dynamic dimensions you can not declare variables as constants

    const int m_ = 50; // m_ no deberia ser const
    
    void setm_ (const int m) {m_=m;} // Error m_ es constante
    
  • If you want constant dimensions you have to use dynamic memory ( new and delete ) instead of creating arrays based on fixed size

    int A[m_][n_]; // A siempre sera de 50x50
    int B[f_][c_]; // B siempre sera de 50x50
    
    int C[b_][e_]; // C siempre sera de 50x50
    
  • If you want to use several arrays it will always be preferable to create several objects of type matriz .

    int A[m_][n_]; // memoria interna del objeto array
    int B[f_][c_]; // sobra
    int C[b_][e_]; // sobra
    
  • Dynamic memory reservations are made at run time, not compile. Consequently, these initializations must be done within a function (such as the constructor)

    // Incorrecto
    int *M= new int [m_*n_];
    int *N= new int [f_*c_];
    
    // Correcto (una posibilidad)
    matriz()
      : M(new int[m_*n_])
        N(new int[f_*c_])
    { }
    
  • If, for the internal management of an object, you make dynamic memory reservations to bad, the destructor must take charge of releasing said reserves

    ~matriz()
    {
      delete[] M;
      delete[] N;
    }
    
  • How should the class stay then?

    In the end, the simple is usually what works best (at least at the beginning):

    • Each object of type matriz should only manage a data matrix
    • Changing dimensions dynamically will not be allowed. For this it is preferable to create a new object
      • if the dimensions are larger, how are the new cells initialized?
      • if the dimensions are smaller, is it acceptable to lose data?
    • A mechanism is needed to modify the values of the matrix.

    A solution to the interface:

    class matriz {
    
      int filas_;
      int columnas_;
    
      int *datos_;
    
    public:
    
      //constructores
      matriz(int filas, int columnas)
        : filas_(filas),
          columnas_(columnas),
          datos_(new int[filas*columnas])
      {
      }
    
      ~matriz()
      {
        delete[] datos_;
      }
    
    
      //métodos
      int& Valores(int fila, int columna)
      {
        return datos_[fila*columnas_+columna];
      }
    
      matriz suma(matriz const& otra) const;
      matriz resta(matriz const& otra) const;
      matriz multiplicacion(matriz const& otra) const;
      matriz inversa(matriz const& otra) const;
    };
    

    Final advice: Do not get into shirts of eleven yards if your C ++ base is scarce.

        
    answered by 27.02.2017 в 09:20