matrix - Gauss Elimination C++ segmentation fault -
i'm stuck code gauss elimination in c++, need return upper triangular matrix still thing segmentation fault. know there must sort of going of allocated memory can't find where.
code:
#include <iostream> using namespace std; double ** allocatedynamicarray(int order){ double ** dynarray = new double *[order]; int cols = order+1; double *pool = new double [order * cols]; for(int = 0;i < order; i++, pool += cols){ dynarray[i] = pool; } return dynarray; } void deallocatedynamicarray(double **dynarray){ delete [] dynarray[0]; delete [] dynarray; } void addandprintarray(double **dynarray, int order){ cout << "zadejte prvky pole radu " << order << endl; for(int i=0; i< order; i++){ for(int j=0; j< order+1; j++){ cout << "pole[" << << "][" << j << "]: "; cin >> dynarray[i][j]; } } for(int i=0; i< order; i++){ for(int j=0; j< order+1; j++){ if(dynarray[i][j] < 10 && dynarray[i][j] >= 0){ cout << " "; } cout << dynarray[i][j] << " "; } cout << endl; } } double ** gausselimination(double** dynarray, int order){ for(int j=1; j<=order; j++) /*horni trojuhelnikova matice*/ { for(int i=1; i<=order; i++) { if(i>j) { double c=dynarray[i][j]/dynarray[j][j]; for(int k=1; k<=order+1; k++) { dynarray[i][k] = dynarray[i][k] - c * dynarray[j][k]; } } } } return dynarray; } int main() { cout << "zadejte rad matice: "; int order; cin >> order; double **arr = allocatedynamicarray(order); addandprintarray(arr, order); gausselimination(arr, order); deallocatedynamicarray(arr); return 0; }
can tell me what's wrong?
the problem in c/c++ first element of array should have index 0
, your
for(int i=1; i<=order; i++)
should be
for(int i=0; i<order; i++)
in gausselimination
function.
Comments
Post a Comment