This is a simple code to get an array of a random walk. If I increase the array size anymore it gives me a segmentation fault. I need it to be larger and 2D (like a matrix). How can I do that without getting the error?
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
int main(){
srand (time(NULL));
int iter = 1000000;
int x[iter]; x[0] = 0;
int p = 50;
int r;
for(int i=1; i<iter; i++){
r = rand() % 101;
if(r<p){
x[i+1] = x[i] - 1;
} else if(r>p){
x[i+1] = x[i] + 1;
} else if(r=p){
x[i+1] = x[i];
}
}
ofstream myFile("walk.csv");
for(int j=0; j<iter; j++){
myFile << x[j] << "\n";
}
myFile.close();
return 0;
}