Help with array selection

I'm trying to make a selection process using roulette wheel selection. To do this I created two matrix, one random probabilities and one increasing probabilities. The idea is to choose a number according to the random probabilities. Here is the code I've written.

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
#include <iostream>
#include <conio.h>
#include <cstdlib>

using namespace std;
int main (){
	float select [5], prob [10], mat [5];
	int c, r, z;
	cout.precision (2);
	cout << "Random Number:" << endl;
	for (c =0; c<5; c++){
		select [c] = (double) rand()/RAND_MAX;
		cout << select [c] << " ";
	}
	cout << endl << "\nIncreasing Probabilities:" << endl;
	for (c = 0; c< 10; c++){
		prob [c] = (double) (c+1)/10;
		cout << prob [c] << " ";
	}
	cout << endl;
	r = 0;
	c = 0;
	z = 0;
	
	while (z<5){
		c=0;
		while(c<10){
			if (select[z]-prob[c]<0){
				mat [z]=c;
				z++;
			}
		c++;
		}
	}
	cout << "\nSelected Column:" << endl;
	for (c =0; c<5; c++){
		cout << mat [c] << endl;
	}
	getch ();
	
}


The result I got is as follows:
Random Number:
0.0013 0.56 0.19 0.81 0.59

Increasing Probabilities:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Selected Column:
0
5
6
8
9


The evaluation doesnt seem to start from c=0 again. The selected column should be 0, 5, 1, 8, 5.

Can you help me? Thanks in advance.
I believe this is your problem:

1
2
3
4
5
6
7
while(c<10){
			if (select[z]-prob[c]<0){
				mat [z]=c;
				z++;
			}
		c++;
		}


mat array can only take 5 items, but your while loop runs 10 times, which means that after a while, the elements of the array will start to get overwritten.

Maybe you want to have it like this?

1
2
3
4
5
6
7
8
9
10
while (z<10){
		c=0;
		while(c<5){
			if (select[z]-prob[c]<0){
				mat [z]=c;
				c++;
			}
		z++;
		}
	}


FIXED:

1
2
3
4
5
6
7
8
9
10
while (z<10){
		c=0;
		while(c<5){
			if (select[c]-prob[z]<0){
				mat [c]=z;
				c++;
			}
		z++;
		}
	}
Last edited on
Thanks. But it turns out, there is an empty number. Here is the result using that code.
Selected Column:
0
1.3e+032
1
3
1.3e+032
Whoops fixed it now, try the new one
Its returning the same results as before.. :D


Selected Column:
0
5
6
8
9
Print the RAND_MAX value on your computer and post it here, I need to use it to test something

E: Nvm, I just did it manually:

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
#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
  //srand(time(NULL));
  float select [5], prob [10], mat [5];
  int c, r, z;
  cout.precision (2);
  cout << "Random Number:" << endl;
  select[0] = 0.0013;
  select[1] = 0.56;
  select[2] = 0.19;
  select[3] = 0.81;
  select[4] = 0.59;
//   for (c =0; c<5; c++)
//   {
//     select [c] = (double) rand()/RAND_MAX;
//     cout << select [c] << " ";
//   }
  cout << endl << "\nIncreasing Probabilities:" << endl;
  for (c = 0; c< 10; c++)
  {
    prob [c] = (double) (c+1)/10;
    cout << prob [c] << " ";
  }
  cout << endl;
  r = 0;
  c = 0;
  z = 0;
  
  while (z<5)
  {
    c=0;
    r = z;
    while(c<10)
    {
      if (select[z]-prob[c]<0)
      {
	mat [z]=c;
	z++;
      }
      c++;
    }
    z = r + 1;
  }
  cout << "\nSelected Column:" << endl;
  for (c =0; c<5; c++)
  {
    cout << mat [c] << endl;
  }
  
}




$ ./temp
Random Number:


Increasing Probabilities:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 

Selected Column:
0
5
1
8
5
Last edited on
Thanks a lot man!
Topic archived. No new replies allowed.