Random number coin toss

I was going through a programming book and there was a question to write a program in which you toss 2 coins 20 times and show the highest number of combination so i wrote a program and it is running but it is not showing the combination which is occurinng the most times so any idea what could i be doing wrong?
The program is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
	int count1=0,count2=0,count3=0;
	
	int toss;
	int sum;
	for (toss=0;toss<20;toss++)
	{
		int head,tail;
		head= 1 + rand()%2;
		tail= 1 + rand()%2;
		sum=head+tail;
	switch (sum)
	{
		case1:count1++;
		case2:count2++;
		case3:count3++;
		break;
	}
	}
	if(count1>count2&&count3)
	cout<<"count 1 occurs the most"<<count1<<endl;
	if(count2>count1&&count3)
	cout<<"count 2 occurs the most"<<count3<<endl;
	if(count3>count1&&count2)
	cout<<"count 3 occurs the most"<<count3<<endl;
	
	return 0;
	}[code]
[/code]
Last edited on
There are a couple of things that you're going to need to do.

First thing is to use the code brackets when you post code, its the [<>] button on the right side when you're posting.

Please put a space between case and the test value. case 1: count1++;
The breaks should be between each case and the next, and is not needed at the very end (and usually there is a default: just in case an unexpected number is fed into your switch).

Your if statements also have a problem, &&count3 does not mean what you think it does.
1
2
3
4
5
if(count1 > count2) // a good boolean-check

if(count1 > count2 && count1 > count3) // Probably what you wanted, two boolean checks in a single if statement

if(count1 > count2 && count3) // Bad: actually means; if count1 is greater than count2 and count3 doesn't equal 0 


In your second if statement, you want to change your cout to output count2 instead of count3.

Finally when you get this worked out, you're going to want to look up srand(time), which will seed your random number generator.

Good attempt, especially if you're studying on your own out of a book, but please consider posting in the beginner's forum, people are more likely to give you step by step help in that forum (including me, I was a bit terse here, I'm less rude over there.)

(Really, I'm sorry about being rude about your code, I can see you're a first time poster here, don't worry, I'm just up later than I should be, I promise this community is more inviting than what I've let on...)

[Edit]
I got my own karma for posting so late, you actually had the correct understanding of modulus, I've removed reference to that in my instructions.
Last edited on
Thanks for the suggestion/help I will try to post my questions in the beginner forum next time and that about the code i did what you said but the problem remained the same the program is running but it is not showing the output.
When I compile and run the program only an empty console window opens showing the time in which program is executed and only that press any key to continue.
You also have problems with your switch block (lines 16 - 22). If you do not put a break statement after each case, you will fall through to the next case. In other words, if sum == 1 (in the existing code), you will not only increment count1, but also count2 and count3. Each of the lines 18-20 should have break; at the end.
Thank you for using the brackets, it's more than just a for-the-look kind of thing (although that's a big part too), it actually allows us to test your code if we aren't on a computer with a compiler by hitting that little gear at its top right corner.

Would you mind updating the code as well, show us what you've done so far?
Please re-read my instructions, I had to edit them from last night.
Last edited on
Also, there may be 3 possible combinations of coin tosses (both heads, both tails, or one of each), but there are 4 ways to the two coins can be tossed. So I think the switch statement should be:
1
2
3
4
5
6
	switch (sum) {
		case1:count1++; break;
		case2:count2++; break;
		case3:count3++; break;
		case 4: count4++; break;
	}

You'll have to declare and initialize variable count4 of course.

Have you learned about arrays? They would make this problem easier.
but there are 4 ways to the two coins can be tossed.

There are 3 possible outcomes sum-wise, but they are in the set [2,3,4] not [1,2,3].

1 + 1 = 2
1 + 2 = 3
2 + 1 = 3
2 + 2 = 4


So the sum of 3 should occur, on average, twice as many times as 2 or 4.
SMH. Thank you cire.
well i have an exam tomorrow and also one day after tomorrow so i will be posting the code later any way thanks for your help
well i have exams for two consecutive days so i will be posting the code on friday i hope
Topic archived. No new replies allowed.