Hello,

I'm new to c++ and I have this problem.
1) Write a program to simulate rolling a pair of dice 100 times.
2) Write the logic so that:
** When the result is less than 6, display "....."
** When the result is 6, 7, or 8, display "......"
** When the result is greater than 8, display "....."
3) Record how many times each value (2, 3, 4, ...12) occurs and how many rolls in total.
4) Display the numbers of times each outcome occurred and its percent of the total numbers of rolls.

I am fine until I get to number 3... I'm drawing a blank on how to count how many times each number occurs. I have the total rolls figured out. And then I'm stuck on number 4 on how to count each outcome to figure out the percent.

** The bottom of my code is not complete I just typed out the cout where I will put the results from numbers 3 and 4

** And we haven't learned functions yet so I cant use anything like that.

Any help would be greatly appreciated!!

Thanks!!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #include using namespace std; int main() { int die1, die2, total_dice, count, i; srand(time(0)); count = 0; for (i = 1; i <= 100; i++) { die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; total_dice = die1 + die2; cout << "Result from roll is: " << total_dice << endl; count ++; if (total_dice < 6) { cout << "Hypertron battery failure imminent, MAYDAY, MAYDAY!!" << endl; cout << "" << endl; } if (total_dice == 6 || total_dice == 7 || total_dice == 8) { cout << "Normal function, continue operations" << endl; cout << "" << endl; } if (total_dice > 8 ) { cout << "I've never seen such performance, Dr. Spock. It's a miracle!!" << endl; cout << "" << endl; } } cout << "Total Rolls: " << count << endl; cout << "" << endl; cout << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; cout << "The number " << " " << " occured " << "" << " times and its percentage of occurence is " << "" << endl; return 0; }``````
Have you learned arrays?

You have to count up how many times each of the twelve possible `total_dice` values occurs. So if you have an array of twelve integers and increment the appropriate one every time that total_dice value occurs, you'll end up with the total of each at the end.

Jim
Jim,

Unfortunately we haven't learned arrays yet. Our next topic is functions then arrays. Is there another possible way to do it?

Emilio
If you're not allowed to use arrays, I'm guessing your teacher's intent is to have you write this without to show how 'ugly' the code is, and then show you how 'beautiful' it is with an array.

You're going to have to have twelve separate variables, one for each possible total_dice result, and increment the appropriate one in a big switch statement. You could use a big set of if-else statements, but that's ungainly. If you've learned switch statements use that ...

Jim
closed account (Dy7SLyTq)
have u learned pointers?
@Jim, Yes I do believe you are correct where the teacher wants us to write "ugly" code then shows us how to do it properly since our next topic is functions & arrays. And yes we have learned switch statement, but could you give me an example of how you mean to set it up? I just cant seem to picture it for some reason.

@DTSCode, and no we have not yet learned pointers.
 ``12345678910111213`` `````` unsigned roll_1 = 0 ; unsigned roll_2 = 0 ; // ... unsigned roll_12 = 0 ; // ... switch( current_roll ) { case 1: ++roll_1 ; break ; case 2: ++roll_2 ; break ; // .... case 12: ++roll_12 ; break ; }``````
@cire - thank you. Ill give it a shot later after classes.
Ok so I got the switch to work but for some reason cant get the percentage to work properly. I keep getting an output of 0. I have done percents before so what am I missing here?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109`` ``````#include #include #include using namespace std; int main() { int die1, die2, total_dice, count, i, roll_2, roll_3, roll_4, roll_5, roll_6, roll_7, roll_8, roll_9, roll_10, roll_11, roll_12 ; srand(time(0)); count = 0; roll_2 = 0; roll_3 = 0; roll_4 = 0; roll_5 = 0; roll_6 = 0; roll_7 = 0; roll_8 = 0; roll_9 = 0; roll_10 = 0; roll_11 = 0; roll_12 = 0; for (i = 1; i <= 100; i++) { die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; total_dice = die1 + die2; cout << "Result from roll is: " << total_dice << endl; count ++; if (total_dice < 6) { cout << "Hypertron battery failure imminent, MAYDAY, MAYDAY!!" << endl; cout << "" << endl; } if (total_dice == 6 || total_dice == 7 || total_dice == 8) { cout << "Normal function, continue operations" << endl; cout << "" << endl; } if (total_dice > 8 ) { cout << "I've never seen such performance, Dr. Spock. It's a miracle!!" << endl; cout << "" << endl; } switch (total_dice) { case 2: ++roll_2 ; break ; case 3: ++roll_3 ; break ; case 4: ++roll_4 ; break ; case 5: ++roll_5 ; break ; case 6: ++roll_6 ; break ; case 7: ++roll_7 ; break ; case 8: ++roll_8 ; break ; case 9: ++roll_9 ; break ; case 10: ++roll_10 ; break ; case 11: ++roll_11 ; break ; case 12: ++roll_12 ; break ; } } int percent_2; int percent_3; int percent_4; int percent_5; int percent_6; int percent_7; int percent_8; int percent_9; int percent_10; int percent_11; int percent_12; percent_2 = ((roll_2/100) * 100); percent_3 = ((roll_3/100) * 100); percent_4 = ((roll_4/100) * 100); percent_5 = ((roll_5/100) * 100); percent_6 = ((roll_6/100) * 100); percent_7 = ((roll_7/100) * 100); percent_8 = ((roll_8/100) * 100); percent_9 = ((roll_9/100) * 100); percent_10 = ((roll_10/100) * 100); percent_11 = ((roll_11/100) * 100); percent_12 = ((roll_12/100) * 100); cout << "Total Rolls: " << count << endl; cout << "" << endl; cout << "The number 2 occured " << roll_2 << " times and its percentage of occurence is " << percent_2 << " %" << endl; cout << "The number 3 occured " << roll_3 << " times and its percentage of occurence is " << percent_3 << " %" << endl; cout << "The number 4 occured " << roll_4 << " times and its percentage of occurence is " << percent_4 << " %" << endl; cout << "The number 5 occured " << roll_5 << " times and its percentage of occurence is " << percent_5 << " %" << endl; cout << "The number 6 occured " << roll_6 << " times and its percentage of occurence is " << percent_6 << " %" << endl; cout << "The number 7 occured " << roll_7 << " times and its percentage of occurence is " << percent_7 << " %" << endl; cout << "The number 8 occured " << roll_8 << " times and its percentage of occurence is " << percent_8 << " %" << endl; cout << "The number 9 occured " << roll_9 << " times and its percentage of occurence is " << percent_9 << " %" << endl; cout << "The number 10 occured " << roll_10 << " times and its percentage of occurence is " << percent_10 << " %" << endl; cout << "The number 11 occured " << roll_11 << " times and its percentage of occurence is " << percent_11 << " %" << endl; cout << "The number 12 occured " << roll_12 << " times and its percentage of occurence is " << percent_12 << " %" << endl; return 0; }``````
Last edited on
` percent_2 = ((roll_2/100.0) * 100); `

Without a decimal in there, it's all integer math.
Yes that's right, now I remember that. I made the same mistake last time :-)

Thanks again for your help cire!!
Topic archived. No new replies allowed.