Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am having a problem with my class, please help. (can't describe)

Status
Not open for further replies.

OverkillMonkey

Programmer
May 24, 2001
9
US
My other class, DivideGenes, is supposed to divide the genes created in Genes in half (to create a sperm cell). I do this by

1. copying the array
2. finding random indexes in the array
3. coppying the values of the random indexes to a different array. (this array has twice as little genes, because it is the sperm cell)


The problem that I encounter is making sure that I don't copy an index more then once.

For example, if my original cell's array is
1,0,1,1,1,0,1,0
I don't want it to get the index - 1 - 3 times.
I don't want
1,1,1,1,1,1,0

Know what I mean?

So I tried to do the following:

1. After I copy the value of the random index, I set the value of that index equal to 3.

2. I check, in a while loop, on the next round of the for loop, if the current index of the array is equal to 3.

3. If it is equal to 3, then I set an integer equal to another random value of the array.

4. I check to see if the integer's value is equal to 3 (just in case it chose something that has been chosen already) and if it is, then I use the "continue;" statement to redo this.

5. If the integer's value IS NOT equal to 3, then i set the current index of my new array (the sperm array) equal to the value of the integer.

For some reason this isn't working. When I run the program, it shows me a lot of 3's in the output. In fact, the amount of 3's in the output accumilate as the program goes on (probably because my while loop is not filtering them out of it.)

Here is my code:


code:--------------------------------------------------------------------------------
public class DivideGenes

{
public int badcopy =0;

boolean[] genes;

public int[] dgene = new int[250];
public int tempgene;

public int integergene[] = new int[501];

public int geneholder;

public DivideGenes(boolean[] genes)

{
this.genes = genes;


}


public int randommath() { //finds a random number


double tempgene2 = Math.random();
tempgene2 = tempgene2*100;
tempgene = (int)tempgene2;
return tempgene;
}

public void randmategenes() //main process
{
System.out.println("STARTED THE DivideGenes MAIN PROCESS");

for (int i =0; i <= 500; i++) {
System.out.println(&quot;Genes: &quot; + genes);

if (genes == true) {

//converts the boolean array to an integer array

integergene = 1;
}
else {
integergene = 0;
}

System.out.println(&quot;Intergergene: &quot; + integergene);
}

for (int i = 0; i < 250; i++) { //creates the sperm cell




while (integergene == 3) {
//this is where the troubles begins

geneholder = integergene[randommath()];



if (geneholder == 3) {
continue;
}
else {
dgene = geneholder;
break;



}



}
dgene = integergene[randommath()];
integergene[tempgene] = 3;
System.out.println(&quot;The random gene is &quot; + dgene);

}
System.out.println(&quot;There were &quot; + badcopy + &quot;bad copies&quot;);
}
}

--------------------------------------------------------------------------------


sorry, its not very well commented. I can't really comment on this , i am afraid that it will look weird.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top