The correct way to define array

I need help in defining the size of array P and Q where it is from maximum number of random number.Here is my code
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
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <conio.h>//define getch
#include <iomanip>
#include <fstreaM>
#include <string>
#include <map>
#include <random>
#include <chrono>


#define m 10



using namespace std;


int main()
{
	int i, j,max,M,KMAX;
	double P[KMAX];
	double Q[KMAX];
	int a[m];
	double p = 0.95;
	double a1 = 20.79, a2 = 0.005;
	int Vmax = 3, KM = 5;
	cout.setf(ios::fixed);
	cout.precision(3);

	
	//value of X-axis
	

	double X_axis[m];
	for (i = 1; i <= m; i++)
	{
		X_axis[i] = ((i - 1) / 4.0);
		cout << "   " << X_axis[i] << endl;
	}

	
	//random number follow poisson distribution
	
	cout << "some Poisson-distributed results:" << endl;

	unsigned seed =     std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
	std::poisson_distribution<int> distribution(2 * X_axis[m]);

	for (int i = 0; i < m; ++i)
	{
		a[i] = distribution(generator);
		cout << i << "   " << a[i] << "  " << endl;
	}


////maximum number of DSB
	
	max = a[1];
	for (int i = 0; i < m; ++i)
	{
		if (max <= a[i])
			max = a[i];
		KMAX = max;
	}
	cout << "maximum number is " << KMAX << endl;

	
	//matrix
	
	M = ((KMAX + 1)*(KMAX + 2)) / 2;
	

	for (i = 1; i <= KMAX; i++)// kiraan untuk P
	{
	P[i] = (p*Vmax*i) / (KM + i);
	Q[i] = (1 - p)*Vmax*i / (KM + i);

	cout << "P[" << i << "] = " << P[i] << "\t" << "Q[" << i << "] = " << Q[i] << endl;
	}
	cout << endl;
	_getch();
}

Last edited on
Array sizes must be compile-time constants, unless they are dynamically created.
Instead of using arrays I would recommend using std::vector.

1
2
3
4
5
6
7
8
9
10
11
12
// Creates an empty vector of integers.
std::vector<int> vec;

// Adds 5 to the end of the vector, increasing the size by 1.
vec.push_back(5);

// Sets the size of the vector to 10. All new elements will have the value 0.
vec.resize(10);

// Using indices to access elements in the vector, the same way as you do with arrays.
vec[7] = 13;
std::cout << vec[7];

http://www.cplusplus.com/reference/vector/vector/
Last edited on
sorry to ask,why push_back (5) and resize(10)?i still can't get this one and how about vec[7]=13? where these integer come from?or it just an example?
It's just an example.
You can't create arrays from random numbers generated at run time. You can create them dynamically or use vectors.

Dynamically created arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>

int main()
{
        std::srand(time(NULL));
        int size = rand() % 10 + 1; // generate random number 1 - 10

        int *pointer = new array[size];

        std::cout << "Size of array is: " <<
        ( sizeof(pointer) / sizeof(*pointer) ) << '\n';

        delete[] pointer;

        return 0;
}

Vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <cstdlib>

int main()
{
        std::srand(time(NULL));
        unsigned short int size = std::rand() % 10 + 1; // random 1 - 10

        std::vector<int> vec(size);

        std::cout << "Size of vector is: " << vec.size() << '\n';

        return 0;
}

You should change/remove some headers.


stdlib.h (remove; not needed here)

math.h (change to cmath; C++ version of math.h)

fstreaM (change to fstream)
Last edited on
boost lexical cast wrote:
1
2
std::cout << "Size of array is: " <<
( sizeof(pointer) / sizeof(*pointer) ) << '\n';

This will not give you the number of elements in the array because sizeof(pointer) returns the size of the pointer, not the size of the array.
I did for display P, however,i face new problem in positioning P in MX matrix, what should i do for MX matrix

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
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <conio.h>//define getch
#include <iomanip>
#include <fstream>
#include <string>
#include <map>
#include <random>
#include <chrono>
#include <cstdlib>
#include<algorithm>


#define m 10



using namespace std;


void main()
{
	int i, j,max,M,KMAX,L,A,k;
	int a[m];
	double MX;
	double p = 0.95;
	double a1 = 20.79, a2 = 0.005;
	int Vmax = 3, KM = 5;
	cout.setf(ios::fixed);
	cout.precision(3);

	//////////////////////////////////////////////////////////////////////////////////////////
	//value of X-axis
	/////////////////////////////////////////////////////////////////////////////////////////

	double X_axis[m];
	for (i = 1; i <= m; i++)
	{
		X_axis[i] = ((i - 1) / 4.0);
		cout << "   " << X_axis[i] << endl;
	}

	///////////////////////////////////////////////////////////////////////////////////////
	//random number follow poisson distribution
	//////////////////////////////////////////////////////////////////////////////////////
	cout << "some Poisson-distributed results:" << endl;

	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); 
	std::default_random_engine generator(seed);
	std::poisson_distribution<int> distribution(2 * X_axis[m]);

	for (int i = 0; i < m; ++i)
	{
		a[i] = distribution(generator);
		std::cout << i << "   " << a[i] << "  " << endl;
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	//maximum number of DSB
	///////////////////////////////////////////////////////////////////////////////////
	max = a[1];
	for (int i = 0; i < m; ++i)
	{
		if (max <= a[i])
			max = a[i];
		KMAX = max;
	}
	cout << "maximum number is " << KMAX << endl;
	
    /////////////////////////////////////////////////////////////////////////////////////////
	//matrix
   ////////////////////////////////////////////////////////////////////////////////////////
    
	unsigned short int size = KMAX;
	std::vector<int> vec(size);

	std::cout << "Size of vector is: " << vec.size() << '\n';

	for (int i = 0; i < vec.size(); i++)
	{
	    double	P = (p*Vmax*i) / (KM + i);
		double  Q = (1 - p)*Vmax*i / (KM + i);
		cout << "P[" << i << "] = " << P << "\t" << "Q[" << i << "] = " << Q << endl;
	}
	
	M = ((vec.size() + 1)*(vec.size() + 2)) / 2;


	
	
	//////////////////////////////////////////////////////////////////
        //position P in matrix MX[][]
        //////////////////////////////////////////////////////////////////
	//vector<vector<double>> MX;
	/*for (i = vec.size(); i >= 1; i--)
	{
		for (j = 1; j <= i; j++)
		{
			
			//MX[j - 1 + L][ vec.size() + k] = P[A+1];
			cout << "MX[" << j - 1 + L << "][" << vec.size() + k << "] = " << MX <<        endl;
			k++;
		}
		cout<<endl;
		L = k + A;
		A++;

	}*
_getch();
} */



Last edited on
Topic archived. No new replies allowed.