C++ 将数组复制到文本文件c++;错误munmap_chunk()
嗨,我正在尝试创建一个存储数组值的文本文件。该程序运行良好,但当我试图插入一些代码复制到文本文件时,我得到了错误 munmap\u chunk():无效指针munmap\u chunk():无效指针 创建的文本文件为空C++ 将数组复制到文本文件c++;错误munmap_chunk(),c++,arrays,text,copy,C++,Arrays,Text,Copy,嗨,我正在尝试创建一个存储数组值的文本文件。该程序运行良好,但当我试图插入一些代码复制到文本文件时,我得到了错误 munmap\u chunk():无效指针munmap\u chunk():无效指针 创建的文本文件为空 #include <iostream> #include <string> #include <cstdlib> #include <fstream> using namespace std; int generator(voi
#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 the number of Walks : ";
cin >> nwalks;
cout << "Give the number of Steps : ";
cin >> nsteps;
ofstream arrayData("/home/PATH/xaverage.txt",ios::app);
srand(1256);
xaccum = 0.0;
XSUM1 = 0.0;
XSUM2 = 0.0;
for (int walks = 1; walks <= nwalks; walks++)
{
x = 0;
for (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];
}
for (int i = 1; i <= nwalks; i++){
arrayData << ArXACCUM[i] << endl;
}
cout << " The average displacement <x> is : " << XSUM1/nwalks << endl;
cout << " The average of the squared displacement <x^2> is : " << XSUM2/nwalks << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
整数生成器(无效){
int状态;
状态=1+rand()%2;
返回状态;
}
int main(){
int nwalks、nsteps、x、state;
双XSUM1、XSUM2、xaccum;
int*ArXACCUM=newint[nwalks];
int*ArXSQUARED=newint[nwalks];
cout>nwalks;
cout>nsteps;
流数组数据(“/home/PATH/xaverage.txt”,ios::app);
srand(1256);
xaccum=0.0;
XSUM1=0.0;
XSUM2=0.0;
对于(int walks=1;walks 1。您已经使用未初始化的nwalks
2创建了两个数组。数组索引从0
开始,到nwalks-1
nwalks一个数字作为输入,如果您的意思是它们没有值。那么如果我想存储(假设nwalks=100)100 Resultls在nwalks之前我应该从零开始循环?我还想将nwalks的最后一个值存储到文本文件中。在请求输入之前,您正在分配数组。在数组上迭代的循环通常如下所示:for(int i=0;i
。非常感谢,我更正了错误,运行得很好。