Vector out of range

I try to make an iteration which is depend on X_axis[e]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <unsupported/Eigen/MatrixFunctions>
#include <iostream>
#include <fstream>
#include <math.h>
#include <conio.h> //===define getch
#include <iomanip>
#include <fstreaM>
#include <string>
#include <map>
#include <random>
#include <chrono>
#include <vector>



#define m 4
#define N 10   //===number of cells
#define MAX 100


using namespace std;
using namespace Eigen;

void main()

{



	int k = 1, L = 0, A = 0, i, j, e, max, count;
	
	
	


	int a[N];
	int freq[100];
	double p = 0.95;
	double a1 = 20.79, a2 = 0.005;
	int Vmax = 3, KM = 5;
	cout.setf(ios::fixed);
	cout.precision(4);





	//===value of X-axis===========
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
	double X_axis[m];
	int KMAX;


	for (e = 2; e <= m; e++)

	{
		X_axis[e] = ((e - 1) / 4.0);
		cout << " X-axis=  " << X_axis[e] << endl;

		poisson_distribution<int> distribution(2 * X_axis[e]);
		
		for (i = 0; i < N; ++i)// generate N randon number
		{
			a[i] = distribution(generator);
		}

		max = a[1];
		for (int i = 0; i < N; ++i)
		{
			if (max <= a[i])
				max = a[i];
			KMAX = max;
		}
		cout << "KMAX=  " << KMAX << endl;

		vector<int> vec(KMAX);
	
		//============P  formulation================
		vector<double> P(MAX);
		

		for (i = 1; i <= KMAX; i++)
		{
			P[i] = (p*Vmax*i) / (KM + i);
			cout << "P[" << i << "]=" << P[i] << endl;
		}cout << endl;

		
			//========Position P in Matrix===================
		for (i = KMAX; i >= 1; i--)
		{
			for (j = 1; j <= i; j++)
				{
					MXd2[j - 1 + L][KMAX + k] = P[A + 1];
					cout << "MXd2[" << j - 1 + L << "][" << KMAX + k << "]=" << MXd2[j - 1 + L][KMAX + k];
					k++;
				}cout << endl;
				L = k + A;
				A++;
		}cout << endl;
	}


before,it is successful when i fixed KMAX, then i next make an iteration depends on X_axis[e]
Last edited on
Indices are counted from zero in C++. X_axis[0] is the first element and X_axis[m - 1] is the last element. You do it correctly for the a array but the indices for all the other arrays and vectors need to be corrected.
Last edited on
it does not work for poisson distribution with mean=0,thats why i start with e=2
1
2
3
4
5
6
7
 double X_axis[m];
//...
for (e = 2; e <= m; e++)
{
    X_axis[e] = ((e - 1) / 4.0);
/...
}

so when e == m, you're trying to write to X_axis[m] which is out-of-bounds, it has nothing to do with where you start from

You can of course loop over the elements in any order you want. What I meant was that the first element is always at index zero so if the array has size m the last valid index has to be m-1. If you want to access index m you have to increase the size of the array.
Topic archived. No new replies allowed.