Macierz z wektorem-dynamicznie

0

"Program przestał działac" po probie uruchomienia. Co robie źle ? Nie moge odnaleźć rozwiazania tego problemu.

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <math.h>
#include <cstddef>
#include <iomanip>
using namespace std;

double epsilon=1e-12;

class Vector
{
    int n;
    double *tab;
public:
    Vector();
    Vector(int);
    ~Vector();
    void printVector();
    double get(int);
    void set(int,double);
};

Vector::Vector(){}

Vector::Vector(int n)
{
    this->n=n;
    tab=new double [n];

    tab[0]=0;
    tab[1]=114;
    tab[2]=-5;
    tab[3]=177;
}

Vector::~Vector()
{
    delete [] tab;
}

void Vector::printVector()
{
    cout << "  ________ " << endl;
    for(int i=0;i<n;i++)
    {
        cout << " |  " << tab[i] << "\t | " << endl;

    }
    cout << "  ________ " << endl;
}

double Vector::get(int i)
{
    return tab[i];
}

void Vector::set(int i,double tmp)
{
    tab[i]=tmp;
}

class Matrix
{
    double **tab;
    int N,M,W;
public:
    Matrix(int,int);
    ~Matrix();
    void initMatrix();
    void printMatrix();
    double get(int,int);
    void set(int,int,double);

};

Matrix::Matrix(int N,int M)
{
    this->N=N;
    this->M=M;

    tab=new double*[N];
    for(int i=0;i<N;i++)
        tab[i]=new double[M];

    this->initMatrix();
}

Matrix::~Matrix()
{
    for(int i=0;i<N;i++)
        delete [] tab[i];

    delete [] tab;
}

void Matrix::initMatrix()
{
    tab[0][0]=1;
    tab[0][1]=20;
    tab[0][2]=-30;
    tab[0][3]=-4;
    tab[1][0]=4;
    tab[1][1]=20;
    tab[1][2]=-6;
    tab[1][3]=50;
    tab[2][0]=9;
    tab[2][1]=-18;
    tab[2][2]=12;
    tab[2][3]=-11;
    tab[3][0]=16;
    tab[3][1]=-15;
    tab[3][2]=14;
    tab[3][3]=130;
}

void Matrix::printMatrix()
{
    cout << " __________________________________ " << endl;
    for(int i=0;i<N;i++)
    {
        cout << " | ";
        for(int j=0;j<M;j++)
        {
            cout << " " << tab[i][j] << "\t ";
        }
        cout << " | " << endl;

    }
    cout << " __________________________________ " << endl;
}

double Matrix::get(int i,int j)
{
    return tab[i][j];
}

void Matrix::set(int i,int j,double tmp)
{
    tab[i][j]=tmp;
}

//Wyznaczenie wektora X na podstawie macierzy i vectora
//wektor/wektor wyliczony X/ n rozmiar
bool solveLU(Matrix *matrix,Vector *vec,Vector *sol,int n)
{
    double suma;
    sol->set(0,vec->get(0));
    for(int i=1;i<n;i++)
    {
        suma=0;
        for(int j=0;j<n;j++)
            suma+=matrix->get(i,j) * sol->get(j); //obliczamy sume iloczynow

        sol->set(i,vec->get(i)-suma); //obliczamy X[i]
    }
    if(fabs(matrix->get(n-1,n-1))<epsilon) return false;
        //unikamy dzielenia przez zero

    sol->set(n-1,matrix->get(n-1,n-1)); //obliczamy wektor X

    for(int i = n - 2; i >= 0; i--)
    {
        suma=0; //zerujemy sume
        for(int j = i + 1; j < n; j++) suma += matrix->get(i,j) * sol->get(j); //obliczamy sume iloczynow

        if(fabs(matrix->get(i,i))<epsilon) return false; //unikamy dzielenia przez zero

        sol->set(i,(sol->get(i) - suma) / matrix->get(i,i)); //obliczamy X
    }
    return true;
}
// Funkcja dokonuje rozkładu LU macierzy A
bool LU(Matrix *matrix,int n)
{
  int i,j,k;
  for(k = 0; k < n - 1; k++)
  {
    if(fabs(matrix->get(k,k)) < epsilon) return false;

    for(i = k + 1; i < n; i++)
      matrix->set(i,k,matrix->get(i,k)/matrix->get(k,k));

    for(i = k + 1; i < n; i++)
      for(j = k + 1; j < n; j++)
        matrix->set(i,j,matrix->get(i,j)-matrix->get(i,k)*matrix->get(k,j));

  }

  return true;
}

int main()
{
    Vector *v=new Vector(4);
    Vector *sol=new Vector();
    Matrix *m=new Matrix(4,4);
    m->printMatrix();
    v->printVector();

    if(LU(m,4) && solveLU(m,v,sol,4))
    {
        for(int i=0;i<4;i++)
            cout << "x" << i+1 << " = " << setw(9) << sol->get(i) << endl;
    }
    else
        cout << "Dzielenie przez zero!" << endl;



    return EXIT_SUCCESS;
}

0

Teraz zauwazylem brak w konstruktorze Vector() tab=new double[n];

0

Nie twórz niezainicjowanego obiektu vector:

Vector::Vector(){}

Nie ma on przydzielonej pamięci. (Usuń tę linijkę lub dodaj alokowanie pamięci).

Czyli jak już tak zrobisz to:

-Vector *sol=new Vector();
+Vector *sol=new Vector(4);

Usuń obiekty po użytkowaniu, lub skorzystaj z kontenerów lub smart poitnerów:

	delete v;
	delete sol;
	delete m;

1 użytkowników online, w tym zalogowanych: 0, gości: 1