0

I am working on a code for gaussian reduction, but for some reason I am getting the error:

Exception thrown at 0x00A9F6DF in Excersise-7-3.exe: 0xC0000005: Access violation writing location 0xFDFDFDFD.

at S[h][0] = i; (line 147)

I cant seem to resolve this. What should I do? How can I prevent this from happening in the future?

#include "stdafx.h"
#include "Gauss.h"
#include <fstream>


Gauss::Gauss()
{
x.resize(v.size());
vector<double>::iterator it;
for (it = x.begin(); it != x.end(); it++)
{
    v[*it] = 0;
}

S = new double*[m];
for (int i = 0; i < m; i++)
{
    S[i] = new double[3];
    for (int j = 0; j < 3; j++)
    {
        S[i][j] = 0;
    }
}
}

Gauss::~Gauss()
{
}

bool Gauss::read_vector(string filename)
{
ifstream in;
in.open(filename);

if (in.fail())
{
    cout << "File could not be opened." << endl;
    return false;
}

in >> size;

for (int i = 0; i < size; i++)
{
    int y;
    in >> y;
    v.push_back(y);
}

in.close();
return true;
}

bool Gauss::read_matrix(string filename)
{
ifstream in;
in.open(filename);

if (in.fail())
{
    cout << "File could not be opened." << endl;
    return false;
}

in >> m;
in >> n;

A = new double *[m];
for (int i = 0; i < m; i++)
{
    A[i] = new double[n];
    for (int k = 0; k < n; k++)
    {
        in >> A[i][k];
    }
}

in.close();
return true;
}

void Gauss::print_vector()
{
for (int i = 0; i < size; i++)
    cout << v[i] << endl;
}

void Gauss::print_matrix()
{
for (int i = 0; i < m; i++)
{
    for (int j = 0; j < n; j++)
    {
        cout << A[i][j] << " ";
    }
    cout << endl;
}
}


void Gauss::elimination(int &i, int &k)                     
for (int j = 0; j < m - 1; j++)                         
{
    if(j != i)                                          
    {
        double fraction = A[j][k] / A[i][k];            
        for (int l = 0; l < n; l++)                     
        {
            A[j][l] = A[j][l] - fraction*A[i][l];       
            v[j] = v[j] - fraction*v[i];                
        }                                               
    }
}
}

void Gauss::row_swap(int &i, int &k)
{
double sub;

for (int j = 0; j < n; j++)                             
{
    sub = A[i][j];
    A[i][j] = A[k][j];
    A[k][j] = sub;
}
                                    
{
    sub = v[i];
    v[i] = v[k];
    v[k] = sub;
}
}

void Gauss::reduction()
{
int h = 0;
int i = 0;
int k = 0;
while (i != m || k != n)
{
    if (A[i][k] != 0)
    {
        elimination(i, k);
            S[h][0] = i;
            S[h][1] = k;
            S[h][2] = A[i][k];
            h++;
            cout << h;
    }
    else
    {
        for (int j = i + 1; j < m; j++)
        {
            if (A[j][k] > 0)
            {
                row_swap(i, k);
                elimination(i, k);
                    S[h][0] = i;
                    S[h][1] = k;
                    S[h][2] = A[i][k];
                    h++;
            }
            else
            {
                k++;
            }
        }
    }
    i++;
    k++;
}

}
void Gauss::solution()
{
for (int j = 0; j < m; j++)
{
    x[S[j][1]] = v[S[j][0]] / S[j][2];
}
}
Community
  • 1
  • 1

0 Answers0