•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,058 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,417 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 716 | Replies: 7
![]() |
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
Im trying to figure out how to display a number from an array where it has been chosen by a random number.
I have got an array with 10 different numbers (always different when program is ran).
Each index is added with the previous index number
So lets say
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)
What I have done in addition to this is make the index number 9 a total value, so that a random number can be generated from 0-200.
Now
If the random number is 150, the program should display the value from index number 7 which is 152.
If the random number is 70, the program should display the value from index number 4 which is 71.
(I am looping this 10 times, so 10 random numbers are generated.)
This is where the problem comes in I can't seem to display the value from index 7.
Can someone point me in the right direction please. Would appreciate it a lot.
This is where I think the problem is coming but can't seem to figure it out
(if the post is unclear, please say so and I will try elaborate abit more)
Thanks
Im trying to figure out how to display a number from an array where it has been chosen by a random number.
I have got an array with 10 different numbers (always different when program is ran).
Each index is added with the previous index number
So lets say
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)
What I have done in addition to this is make the index number 9 a total value, so that a random number can be generated from 0-200.
Now
If the random number is 150, the program should display the value from index number 7 which is 152.
If the random number is 70, the program should display the value from index number 4 which is 71.
(I am looping this 10 times, so 10 random numbers are generated.)
This is where the problem comes in I can't seem to display the value from index 7.
Can someone point me in the right direction please. Would appreciate it a lot.
This is where I think the problem is coming but can't seem to figure it out
for (i=0; i < pop; i++)
{
randomno = (rand()%total);
cout<<"Random Number = "<<randomno<<endl;
for (j=0; j < pop; j++)
if (randomno > running_tot[i])
for (k=0; k < chro; k++)
{
par[i][k] = mosome[j][k];
j = pop + 1;
cout<<"Par ["<<i<<"] = "<<par[i];
cout<<endl;
}
}(if the post is unclear, please say so and I will try elaborate abit more)
Thanks
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 405
Reputation:
Rep Power: 5
Solved Threads: 35
for (i=0; i < pop; i++) { randomno = (rand()%total); cout<<"Random Number = "<<randomno<<endl; for (j=0; j < pop; j++) if (randomno > running_tot[i]) for (k=0; k < chro; k++) { par[i][k] = mosome[j][k]; j = pop + 1; cout<<"Par ["<<i<<"] = "<<par[i]; cout<<endl; } }
1) What is pop?
2) What is running_tot?
3) What is chro?
4) Is par 2d array or 1d array?
A full source-coude would be nice.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
I don't really understand about your code.for (i=0; i < pop; i++) { randomno = (rand()%total); cout<<"Random Number = "<<randomno<<endl; for (j=0; j < pop; j++) if (randomno > running_tot[i]) for (k=0; k < chro; k++) { par[i][k] = mosome[j][k]; j = pop + 1; cout<<"Par ["<<i<<"] = "<<par[i]; cout<<endl; } }
1) What is pop?
2) What is running_tot?
3) What is chro?
4) Is par 2d array or 1d array?
A full source-coude would be nice.
1) Pop is defined as 10. (#define pop 10)
2) running_tot is the array
3) chro is used in another part of my program which holds another array.
4) par will become a new array from the numbers selected by the random number. - 2d or 1d,currently I want it as 1d but will later change it to 2d.
void roulette_wheel(int fitness[population], char chromosome[population][chromelength], char parent[population][chromelength])
{
int i;
int j;
int k;
int total_fitness;
int running_total[population];
int random_integer;
int current_total;
current_total=0;
cout<<endl;
for (i=0; i < population; i++)
{
current_total = fitness[i] + current_total;
running_total[i] = current_total;
cout<<"Running Total ["<<i<<"] = "<<running_total[i];
cout<<endl;
}
cout<<endl;
total_fitness = current_total;
cout<<"Total Fitness = "<<total_fitness<<endl;
cout<<endl;
for (i=0; i < population; i++)
{
random_integer = (rand()%total_fitness);
cout<<"Random Number = "<<random_integer<<endl;
for (j=0; j < population; j++)
if (random_integer > running_total[i])
for (k=0; k < chromelength; k++) //#define chromelength 5
{
parent[i][k] = chromosome[j][k];
j = population + 1;
cout<<"Parent ["<<i<<"] = "<<parent[i];
cout<<endl;
}
}
}Problem I get is that either the program runs and the crashes. or it will display values that are totally irrelevant, such as squares etc.
Last edited by akiller47 : Jan 7th, 2008 at 8:55 am.
•
•
•
•
Hi,
Im trying to figure out how to display a number from an array where it has been chosen by a random number.
what are you using to generate randome number ??? the array or the rand function???
what do u exactly want??
•
•
•
•
Now
If the random number is 150, the program should display the value from index number 7 which is 152.
If the random number is 70, the program should display the value from index number 4 which is 71.
(I am looping this 10 times, so 10 random numbers are generated.)
•
•
•
•
(if the post is unclear, please say so and I will try elaborate abit more)
it's not clear..please elaborate!!!
Life is about being happy...
•
•
Join Date: Jan 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
what are you using to generate randome number ??? the array or the rand function???
what do u exactly want??
again you are generating random numbers and comparing them to array element?? what do u want???what's the purpose of array??
it's not clear..please elaborate!!!
I am using rand()%total_fitness to generate a random number from 0-total_fitness.
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)
total_fitness = the last number in the array, this being 200.
the array is running_total[i].
Lets say that the generated random number is 150
It will go through the array:
0 = 10
1 = 30 (+20)
2 = 32 (+2)
3 = 40 (+8)
4 = 71 (+31)
5 = 76 (+5)
6 = 100 (+24)
7 = 152 (+52)
8 = 163 (+11)
9 = 200 (+37)
If the random number is less than the number in the array, it will display that number. In this case 150 is not less than index 0, 1, 2, 3, 4, 5, 6 but it is less than index 7, so it will display the value of index 7 which is 152.
Last edited by akiller47 : Jan 7th, 2008 at 9:10 am.
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 405
Reputation:
Rep Power: 5
Solved Threads: 35
I believe you are using the wrong index in the line
Shouldn't that be
Also, please work in your formatting consistency. Your code is just a little difficult to follow. See this.
if (random_integer > running_total[i])Shouldn't that be
running_total[j]?Also, please work in your formatting consistency. Your code is just a little difficult to follow. See this.
Age is unimportant -- except in cheese
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Help me code OQL!Help me help me.Send to my Email: daohaoquang@yahoo.com. Thanks you very much.
Last edited by doanhongquang : Feb 26th, 2008 at 1:12 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Select from a 2-dimensional array (ASP)
- Populating an array with results from a mysql query... (PHP)
- select sort using array of pointers---getting wrong values. (C++)
- Show the number not again and again (Visual Basic 4 / 5 / 6)
- Using binary operators! (C)
- Reading txt file into Hash Table (C++)
- structures containing a pointer to an array (C++)
- Array Values not found in Methods (Java)
- using asp array to store db information (ASP)
Other Threads in the C++ Forum
- Previous Thread: Restaurant Menu
- Next Thread: what is "floating point exception" in C++



Linear Mode