User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jan 2008
Posts: 3
Reputation: akiller47 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
akiller47 akiller47 is offline Offline
Newbie Poster

Select a number from an Array

  #1  
Jan 7th, 2008
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

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 405
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 35
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Select a number from an Array

  #2  
Jan 7th, 2008
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;
                    }
     }
I don't really understand about your code.
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
Reply With Quote  
Join Date: Jan 2008
Posts: 3
Reputation: akiller47 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
akiller47 akiller47 is offline Offline
Newbie Poster

Re: Select a number from an Array

  #3  
Jan 7th, 2008
Originally Posted by invisal View Post
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;
                    }
     }
I don't really understand about your code.
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.
Reply With Quote  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: Select a number from an Array

  #4  
Jan 7th, 2008
Originally Posted by akiller47 View Post
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??
Originally Posted by akiller47 View Post
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.)
again you are generating random numbers and comparing them to array element?? what do u want???what's the purpose of array??
Originally Posted by akiller47 View Post
(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...
Reply With Quote  
Join Date: Jan 2008
Posts: 3
Reputation: akiller47 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
akiller47 akiller47 is offline Offline
Newbie Poster

Re: Select a number from an Array

  #5  
Jan 7th, 2008
Originally Posted by rajatC View Post
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.
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 405
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 35
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Select a number from an Array

  #6  
Jan 7th, 2008
Why don't loop from the first index till the last index, and output only index that is bigger than the random number.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Select a number from an Array

  #7  
Jan 7th, 2008
I believe you are using the wrong index in the line
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
Reply With Quote  
Join Date: Feb 2008
Posts: 1
Reputation: doanhongquang is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doanhongquang doanhongquang is offline Offline
Newbie Poster

Re: Select a number from an Array

  #8  
Feb 26th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC