Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: loop question

  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default loop question

    Hello friends,
    I tried my best to resolve this by myself but I could not.How can we create a loop in this code so that it keep on going until it reaches 0 from 100.

    Basically in this program first there will 100 total atoms.In first loop cycle around 50 % of 100 total atoms will be decayed or deducted .Left is around 50 total atoms.Then in second loop cycle around 50% of the 50 atome will be deducted.How many atome will be deducted/decayed will be calculated by Randomgenerator method.It should keep on going untill total number of atoms is 0.

    Sample should look like this.

    Initially there 100 atoms

    50 atoms survived in year 1

    25 atoms survived in year 2

    12 atoms survived in year 3

    6 atoms survived in year 4

    3 atoms survived in year 5.

    0 atoms survived in year 5.
    -------------------------


    I have almost writtend the code but could not figure out how to do it by using loop.here is my code:

    import acm.program.*;
    import acm.util.*;


    public class atomcoin extends ConsoleProgram{

    private static final int totalatoms=100;
    public void run(){


    int survived=0;
    int decayed=0;
    int total=0;
    int total1=0;
    int survived1=0;
    int decayed1=0;
    int total2=0;
    int survived2=0;
    int decayed2=0;

    RandomGenerator rgen = new RandomGenerator();


    for( int i=1; i<=totalatoms; i++){

    int coinflip = rgen.nextBoolean() ? 0 : 0;



    if (rgen.nextBoolean()){

    survived+=1;



    }else{

    decayed+=1;

    total=totalatoms-decayed;

    }


    }
    println("Initially there are "+totalatoms+" atoms.");
    println(" Survived "+survived+" & decayed "+decayed);

    As you can see from here above code is again repeated manually but this has to be done by loop automatically
    for( int i=1; i<=survived; i++){

    int coinflip = rgen.nextBoolean() ? 0 : 0;



    if (rgen.nextBoolean()){

    survived1+=1;



    }else{

    decayed1+=1;

    total1=total-decayed1;
    }


    }
    println(" Survived "+survived1+" & decayed "+decayed1);

    for( int i=1; i<=survived1; i++){

    int coinflip = rgen.nextBoolean() ? 0 : 0;



    if (rgen.nextBoolean()){

    survived2+=1;



    }else{

    decayed2+=1;

    total2=survived2-decayed2;
    }


    }
    println(" Survived "+survived2+" & decayed "+decayed2);

    }
    }
    Last edited by ccie007; 08-14-2010 at 03:39 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Please edit your code and add code tags to preserve its formatting. Its hard to read what you have posted.

    Could you add comments to your code showing where the problem is and what it is you want to do that you are unable to do?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ccie007 View Post
    Hello friends,
    I tried my best to resolve this by myself but I could not.How can we create a loop in this code so that it keep on going until it reaches 0 from 100.

    Basically in this program first there will 100 total atoms.In first loop cycle around 50 % of 100 total atoms will be decayed or deducted .Left is around 50 total atoms.Then in second loop cycle around 50% of the 50 atome will be deducted.How many atome will be deducted/decayed will be calculated by Randomgenerator method.It should keep on going untill total number of atoms is 0.

    Sample should look like this.

    Initially there 100 atoms

    50 atoms survived in year 1

    25 atoms survived in year 2

    12 atoms survived in year 3

    6 atoms survived in year 4

    0 atoms survived in year 5.
    If 50% of the atoms survives every year I'd say that there are 3 atoms left in year 5, 1 atom left in year 6 and no atoms left in year 7 ...

    kind regards,

    Jos

  4. #4
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default loop

    it is not exactly 50% but around 50 % which will be determind by Random Generator method .

  5. #5
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Hi Norm,
    I understand my code can be confusing I have tried to make it clear by commenting within quote.If anybody needs further explanantion please let me know.Thank you guys.much appreciated

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    I don't understand what the program is to do. There are no comments in the program describing what it is supposed to do. For example what is the purpose of this loop:
    for( int i=1; i<=totalatoms; i++){
    Why start at 1 vs 0?

    It should keep on going until total number of atoms is 0.
    The output you posted shows: '0 atoms survived in year 5.' as the last output.
    So is it working correctly?

    Then you say:
    create a loop in this code so that it keep on going until it reaches 0 from 100.
    Which is it supposed to do?
    Always do 100 loops
    or stop looping when the number of atoms is 0

    It would have to be one or the other.
    Can you explain?
    Last edited by Norm; 08-14-2010 at 03:54 PM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ccie007 View Post
    it is not exactly 50% but around 50 % which will be determind by Random Generator method .
    Does the percentage change per loop iteration? Please clarify your question and don't make us guess.

    kind regards,

    Jos

  8. #8
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default loop

    I don't understand what the program is to do.
    The actual question from the book "Art and science of Java" is posted below:

    Despite Einstein’s metaphysical objections, the current models of physics, and
    particularly of quantum theory, are based on the idea that nature does indeed involve
    random processes. A radioactive atom, for example, does not decay for any specific
    reason that we mortals understand. Instead, that atom has a random probability of
    decaying within a period of time. Sometimes it does, sometimes it doesn’t, and there
    is no way to know for sure.Because physicists consider radioactive decay a random process, it is not surprising that random numbers can be used to simulate that process. Suppose you start with a collection of atoms, each of which has a certain probability of decaying in any unit of time. You can then approximate the decay process by taking each atom in turn and deciding randomly whether it decays, considering the probability.
    Write a program that simulates the decay of a sample that contains 10,000 atoms of
    radioactive material, where each atom has a 50 percent chance of decaying in a year.
    The output of your program should show the number of atoms remaining at the end
    of each year, which might look something like this:


    There are no comments in the program describing what it is supposed to do. For example what is the purpose of this loop:
    for( int i=1; i<=totalatoms; i++){
    Why start at 1 vs 0?
    The reason why i chose 1 was to get correct result, I chose i=1 because i am using i<=totalatoms;i++.I believe I should get the
    same result with i =0 if i use i<totalatoms.can u see the difference.1 vs 0? there is no particular reason behind but I was merely trying to get correct result.

    Quote:
    It should keep on going until total number of atoms is 0.
    The output you posted shows: '0 atoms survived in year 5.' as the last output.
    So is it working correctly?
    "0 atoms survived in year ".This is just an example I wrote to make readers understand what the output of of the program should look like(sample).No mate it is not working correctly yet.
    Then you say:
    Quote:
    create a loop in this code so that it keep on going until it reaches 0 from 100.
    Which is it supposed to do?
    First there will be 100 total atoms or according the to the actual question from the book 10000.then every year or in every loop cycle some atoms will be deducted from the total number of atoms(100 or 10000 what u have set it to be).For instance out of 100 atoms, 50 atoms were deducted in first loop cycle or year 1 ,now left is 50 atoms.Then in 2nd loop cycle 25 atoms are deducted from the total 50 atoms.In 3 third loop cycle around 50 % of 25 is let say 12 is deducted.How many atoms deducted are determind by running nextBoolean method left number of atoms times.

    RandomGenerator rgen = new RandomGenerator();


    for( int i=1; i<=totalatoms; i++){ [

    int coinflip = rgen.nextBoolean() ? 0 : 0;



    if (rgen.nextBoolean()){

    survived+=1;



    }else{

    decayed+=1;

    total=totalatoms-decayed;

    }
    for( int i=1; i<=totalatoms; i++){
    //there is reason why i chosed to run this loop totalatoms times is because out of total atoms,I have find out how many will survive or decay(die).Suppose out of 100 atoms, 60 died, now left with 40 atoms.

    int coinflip = rgen.nextBoolean() ? 0 : 0;



    if (rgen.nextBoolean()){

    survived+=1;



    }else{

    decayed+=1;

    total=totalatoms-decayed;
    This part of the code just trying to find how many will will survive and how many will decay (die).

    Now again to find how many atoms will surive or die among the total surived atoms which is 40 I have again run this iteration loop 40 times. Then will find how many will survive from 40 atoms .lets say 15 survived and 25 died.Now I have to run above iteration loop again
    15 times to find how many surive and die among total 15 atoms.This process should go on untill total atoms becomes 0.

    any question plz reply me.





    Always do 100 loops
    or stop looping when the number of atoms is 0

    It would have to be one or the other.
    Can you explain?
    I believe my above explanation answered this question.If you need further explanation plz reply me.Thank you guys.
    Last edited by ccie007; 08-14-2010 at 04:52 PM.

  9. #9
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Yes Jos percentage changes on every loop.Percentage is simply a rough guide.It is not exact 50.Suppose out of 10 atoms,6 might gets deducted .Which is 60% deduction.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Here's my attempt:

    Java Code:
    int totalAtoms= 100; // we start with this many atom
    while (totalAtoms > 0) { // while anything left
       int dead= 0; // no dead atoms yet ...
       for (int i= 1; i <= totalAtoms; i++) // visit all atoms
          if (rgen.nextBoolean())// kill this atom?
             dead++;
       totalAtoms-= dead; // remove the dead atoms
       System.out.println(totalAtoms);
    }
    That's basically it ...

    kind regards,

    Jos

  11. #11
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Yes Jos, you have resolved my problem.Thank you both u and Norm.Jos can I ask how many years of programming experience you have mate?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ccie007 View Post
    Yes Jos, you have resolved my problem.Thank you both u and Norm.Jos can I ask how many years of programming experience you have mate?
    I started at university when I was 18 years old or so; now I am 53 years old and have been programming (besides other stuff) for all those years ...

    kind regards,

    Jos

    edit: b.t.w. you can remove the inner loop completely by doing totalAtoms-= rgen.nextInt(totalAtoms+1) if you trust the nextInt(n) method to generate a uniformly distributed number in the range [0, n)
    Last edited by JosAH; 08-14-2010 at 05:17 PM.

  13. #13
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    import acm.program.*;
    import acm.util.*;


    public class jos extends ConsoleProgram{


    public void run(){

    RandomGenerator rgen= new RandomGenerator();
    int totalAtoms= 100; // we start with this many atom
    System.out.println("There are 10000 atoms initially" );
    while (totalAtoms > 0) { // while anything left
    int dead= 0; // no dead atoms yet ...

    for (int i= 1; i <= totalAtoms; i++) // visit all atoms

    if (rgen.nextBoolean()){// kill this atom?
    dead++;




    totalAtoms-= dead; // remove the dead atoms
    }
    System.out.println(" There are "+ totalAtoms+" at the end of year "+i);
    }

    }

    }
    System.out.println(" There are "+ totalAtoms+" at the end of year "+i);
    I am trying to println the value "i" incrementing like year 1,2,3,4.But it doesn't show that value of i.Any clue.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Can you post what is shown?

    Can you use code tags to preserve the codes formatting, its hard to read/scan unformatted code.

    I don't see matching {} used by the for statement. Is i in scope? Does the code compile?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ccie007 View Post
    I am trying to println the value "i" incrementing like year 1,2,3,4.But it doesn't show that value of i.Any clue.
    Variable i isn't used as a year counter in my code; it is used for counting all atoms still alive. If you want a year counter you can do it like this (I'm using my original code)

    Java Code:
    int totalAtoms= 100; // we start with this many atom
    int year= 0; // year counter
    while (totalAtoms > 0) { // while anything left
       int dead= 0; // no dead atoms yet ...
       for (int i= 1; i <= totalAtoms; i++) // visit all atoms
          if (rgen.nextBoolean())// kill this atom?
             dead++;
       totalAtoms-= dead; // remove the dead atoms
       year++;
       System.out.println(year+": "+totalAtoms);
    }
    kind regards,

    Jos

  16. #16
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    cheers mate.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JosAH View Post
    I started at university when I was 18 years old or so; now I am 53 years old and have been programming (besides other stuff) for all those years ...
    Sorry to hijack, but I recall someone mentioning how old you were in another forum, but I didn't realize you were that old. Ha! I'm a whole year younger than you!

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Sorry to hijack, but I recall someone mentioning how old you were in another forum, but I didn't realize you were that old. Ha! I'm a whole year younger than you!
    Yes, so you should be extremely polite to me and I can do as I please; hah!

    kindest regards,

    Jos ;-)

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Glad to hear that Jos, I should be able to do what I want without any backtalk from you.

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Glad to hear that Jos, I should be able to do what I want without any backtalk from you.
    From me? My lips are sealed old man ...

    kind regards,

    Jos ;-)

Page 1 of 2 12 LastLast

Similar Threads

  1. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 05:08 PM
  2. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-15-2009, 11:23 PM
  3. How to loop a question when answer is negatve?
    By brazuca in forum New To Java
    Replies: 6
    Last Post: 04-23-2009, 05:46 AM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Question regarding foreach loop...
    By theonlywalks in forum New To Java
    Replies: 2
    Last Post: 03-15-2008, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •