Hi I'm pretty new to C++ and I need to dynamicacally allocate two-dimensional array. No error but in runtime when I set a order and first row then I get a runtime error: "Segmentation Fault"...Here's code:
#include <iostream>
using namespace std;
double ** allocateDynamicArray(int &order){
double ** arr = new double *[order];
for(int i = 0;i < order; i++){
*arr = new double[order+1];
}
return arr;
}
void deallocateDynamicArray(double **arr, int order){
for(int i=0; i<order; i++){
delete [] arr[i];
}
delete [] arr;
}
void addAndPrintArray(double **arr, 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[" << i << "][" << j << "]: ";
cin >> arr[i][j];
}
}
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
cout << arr[i][j] << " ";
if(arr[i][j] < 10 && arr[i][j] > -10){
cout << " ";
}
cout << endl;
}
}
}
int main()
{
int order;
cin >> order;
double **dynArray = allocateDynamicArray(order);
addAndPrintArray(dynArray, order);
deallocateDynamicArray(dynArray, order);
return 0;
}
vector<vector<double>>. Much less complicated to work with, since there are no allocation/deallocation headaches.*arr =should bearr[i] =new[]throwing an exception.arr[i]or rather always use vectors?