Copy Array to text file c++ error munmap_chunk() -
hi trying create text file stores value of array. program runs when try insert code copy text file error
munmap_chunk(): invalid pointer munmap_chunk(): invalid pointer
the text file created empty
#include <iostream> #include <string> #include <cstdlib> #include <fstream> using namespace std; int generator(void){ int state; state = 1 + rand() % 2; return state; } int main() { int nwalks,nsteps,x,state; double xsum1,xsum2,xaccum; int *arxaccum = new int[nwalks]; int *arxsquared = new int[nwalks]; cout << "give number of walks : "; cin >> nwalks; cout << "give number of steps : "; cin >> nsteps; ofstream arraydata("/home/path/xaverage.txt",ios::app); srand(1256); xaccum = 0.0; xsum1 = 0.0; xsum2 = 0.0; (int walks = 1; walks <= nwalks; walks++) { x = 0; (int steps = 1; steps <= nsteps; steps++) { state = generator() ; if (state == 1) x += +1; else x += -1; } xaccum =+ x; arxaccum[walks] = xaccum; arxsquared[walks] = xaccum*xaccum; xsum1 += arxaccum[walks]; xsum2 += arxsquared[walks]; } (int = 1; <= nwalks; i++){ arraydata << arxaccum[i] << endl; } cout << " average displacement <x> : " << xsum1/nwalks << endl; cout << " average of squared displacement <x^2> : " << xsum2/nwalks << endl; return 0; }
Comments
Post a Comment