Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Not printing

  1. Jul 10, 2010 #1
    can someone tell me what is wrong with my code? i am trying to make a code that has 1000000 randomly choosing A or B 1000 times. if A is bigger than B, i want to increment the variable called 'aWins'... when i try to get it to print the screen is blank. i left out the int main and #include parts but i have them in my real code...

    Code (Text):

         for (int j = 0; j < 1000; j++){
            for (int i = 0; i < 1000000; i++){
            srand(time(0));
            if ((rand()%2) == 0)
                A++;
            else
                B++;
        }
        if (aWins(A,B))
            aWins++;
            }
            cout << aWins << endl;

       // this is my aWins function
        bool aWins(int x, int y){
        if (x > y)
            return true;
        else
            return false;
        }
     
     
  2. jcsd
  3. Jul 10, 2010 #2
    Could you post the entire program, so I can run it? Just by looking at it, I see nothing wrong.

    As a side note, you can rewrite your function as:

    Code (Text):
       
     bool aWins(int x, int y){
        return(x > y);
        }
    Or simply use that Boolean expression in place of the function with a simple comment beside it to denote its meaning:
    Code (Text):

    if (A > B) //If A "wins" aka is bigger
       aWins++;
     
    Though, I think anyone would know what you were doing without a comment or a function as long as you defined the purpose of the program at the top.

    I would also use how integers can behave like a Boolean:
    Code (Text):

    if ( !(rand()%2) )
       A++;
    else
       B++;
     
    Or reverse the order to minimize the not gates:
    Code (Text):

    if ( (rand()%2) )
       B++;
    else
       A++;
     
     
  4. Jul 10, 2010 #3

    Mark44

    Staff: Mentor

    Your inner loop is running one billion times, and in each iteration you're calling srand(). There's no need to call this function so many times; once will be fine. It seems likely to me that the reason you're not getting output is that your program is taking longer than necessary to do its calculations.

    Also, as xcvxcvvc noted, it's much simpler to just compare A and B in an if statement using <. You have the overhead of making a function call 1000 times. Moving srand out of both loops and getting rid of aWins function should speed up your program noticeable.
     
  5. Jul 11, 2010 #4
    ok, thanks. it's still a little slow, but it's not as bad as before.
     
    Last edited: Jul 11, 2010
  6. Jul 11, 2010 #5
    i have another question..what if there was a 15% error in which B was mistaken for A or vice versa (without taking into account error, 520000 chose A while 480000 were for B). how would i account for this in my code?
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook