### 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include 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:

 ``1234567`` ``````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?

 ``12345678910`` ``````while (z<10){ c=0; while(c<5){ if (select[z]-prob[c]<0){ mat [z]=c; c++; } z++; } }``````

FIXED:

 ``12345678910`` ``````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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include 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.