Troubleshooting Code: Counting A Wins Over B

In summary, if there was an error in which one of the numbers was mistaken for the other, the program would not work as intended. You would need to account for this error in your code.
  • #1
magnifik
360
0
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:
     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;
    }
 
Physics news on Phys.org
  • #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:
 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:
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:
if ( !(rand()%2) )
   A++;
else
   B++;
Or reverse the order to minimize the not gates:
Code:
if ( (rand()%2) )
   B++;
else
   A++;
 
  • #3
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.
 
  • #4
ok, thanks. it's still a little slow, but it's not as bad as before.
 
Last edited:
  • #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?
 

1. How can I troubleshoot my code for counting wins?

To troubleshoot your code for counting wins, first check for any syntax errors or typos. Then, make sure that your code is properly structured and organized. Check for any logical errors in your code, such as incorrect conditional statements or loops. Additionally, try using a debugger or printing out values at different points in your code to see where the issue is occurring.

2. Why is my code not counting wins correctly?

There could be several reasons why your code is not counting wins correctly. Check to see if your logic for determining a win is accurate and if you are properly incrementing the win count. Also, make sure that you are correctly accessing and comparing the necessary variables. If you are using a loop, ensure that it is properly set up and terminating at the correct point.

3. How do I fix my code if it is not counting all wins?

If your code is not counting all wins, check to see if your code is properly handling edge cases or unique situations. For example, if your code is only counting a win if a certain condition is met, make sure that the code is also accounting for situations where that condition may not be met. Additionally, double check that your code is looping through all necessary elements or data points.

4. What can I do if my code is counting wins for both players?

If your code is counting wins for both players, it is likely that your logic for determining a win is incorrect. Make sure that you are properly comparing the scores or values for each player and only incrementing the win count for the correct player. Double check that your conditional statements are accurately capturing the win conditions.

5. How can I optimize my code for counting wins?

To optimize your code for counting wins, consider using more efficient data structures or algorithms. For example, you may be able to use a dictionary or hash table to store the scores and their corresponding win counts. Additionally, try to reduce the number of unnecessary operations or loops in your code. You can also consult with other programmers or reference online resources to see if there are any more efficient solutions for counting wins in your specific scenario.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
751
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top