# Homework help

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-14-2012
arsenalftw067
Homework help
need some help with my C++ homework. here is the assignment:

PROGRAM #1 - DICE ROLLING SIMULATION This program should simulate the roll of a single die (dice) (1-6) using the C++ random number functions. First ask the user how many times they would like to have the die (dice) rolled. Next, have the program simulate the number of rolls of the die (dice) the user requested and keep track of which number the die (dice) landed on for each roll. At the end of the program print out a report showing how many times the die (dice) roll landed on each number and what percentage of the total times the die (dice) roll landed on each number. Do NOT use functions or arrays on this - use what I showed you during lecture, you should always listen during lecture to get the right techniques, if you forgot what I said during lecture look at the slides.
Input Validation: Do not allow the user to enter a number less than 1 as the number of times they would like to roll the dice. Your output should look similar to what is below (exact solution on Hypergrade "View Required Output" link):

DICE ROLL STATISTICS

# Rolled # Times % Times
------ ------- --------
1 4 16.00%
2 3 12.00%
3 5 20.00%
4 7 28.00%
5 4 16.00%
6 2 8.00%

HINT: Put your sRand() function outside of your loop (remember what happened in class when we put it inside the loop!). Watch what happens if you put it inside loop and see if you can figure out why that happens. Make sure your program does not use the same random sequence each time you run it.

Not asking you guys to do my homework, just have a few questions.

1) Kinda confused as to what the variables are supposed to represent. So far I've got
Code:

``` #include <cstdlib>#include <iostream> #include <ctime> using namespace std; int main() {         int times;         int roll;         roll = rand() % 6 + 1;         cout << "How many times would you like to roll the dice?" << endl;         cin >> times;         for (roll =1; int <= times;                                 cout <<"```
I'm guessing to keep counters on time, you start with time = 1 and do time += 1 until it hits time <= times?

Also I have no idea how to keep track of the rolls and how many times it has rolled that specific number. Any help is much appreciated!
• 03-14-2012
laserlight
Quote:

Originally Posted by arsenalftw067
Do NOT use functions or arrays on this - use what I showed you during lecture, you should always listen during lecture to get the right techniques, if you forgot what I said during lecture look at the slides.

Uh, I probably wouldn't write a function other than main for this simple task, but I would use an array, so this begs the question: what did your instructor show you during lecture?

Quote:

Originally Posted by arsenalftw067
I'm guessing to keep counters on time, you start with time = 1 and do time += 1 until it hits time <= times?

Yes, but it is more canonical to start from 0, e.g.,
Code:

`for (int i = 0; i < times; ++i)`
Quote:

Originally Posted by arsenalftw067
Also I have no idea how to keep track of the rolls and how many times it has rolled that specific number.

I would use an array of 6 integers to keep track of the count for each possible face of the die, except for your instructor's mysterious instruction.
• 03-14-2012
oogabooga
If you really can't use arrays, you could use six separate int variables, d1, d2, d3, d4, d5, d6, and a switch statement (if you're allowed to use that!).

You need a srand((unsigned)time(0)); call before your loop, and you need to put the rand() call inside the loop.
• 03-15-2012
Elysia
Quote:

Originally Posted by oogabooga
...You need a srand((unsigned)time(0));...

Or better yet,
Code:

`std::srand((unsigned int)std::time(nullptr));`
(Make sure you use the ones in the std namespace and nullptr instead of the evil 0.)
• 03-15-2012
whiteflags
You could even hash the actual time to an unsigned int and not cast anything at all.
• 03-21-2012
arsenalftw067
I don't understand what you guys are saying, I'm a beginner at all of this.

So far from what I understand is I need 6 variables, one for times the user wants rolled, one for time starting at 0, int = 1 and int =6 since the dice consists of 1-6, i make a nested loop right? but what I don't understand is how I input the result as how many times it rolled 6 or how many times it rolled 3, I'm not understanding that at all
• 03-21-2012
laserlight
What did your instructor show you during lecture?
• 03-21-2012
arsenalftw067
if for loops, she told us specifically to use nested loops but no arrays. I have no idea how to do this and its due on saturday
• 03-21-2012
laserlight
Quote:

Originally Posted by arsenalftw067
if for loops, she told us specifically to use nested loops but no arrays.

Have you been taught about arrays? What technique did she demonstrate?

The thing is, you were very sternly instructed to "use what (she) showed you during lecture". Until you very clearly tell us what she showed you, any "yes" or "no" answer from us could be totally off and result in you failing your assignment.

For example, she told you to use nested loops, but if I were to write a solution for this, I would not use nested loops as I find them unnecessary here. So perhaps she has some other idea in mind that just does not cross my mind right now.
• 03-21-2012
arsenalftw067
I don't even know what an array is.
A nested loop is a loop inside the body of another loop
Pay attention this is what part of HW #3 is about
Example:
Code:

```for (int row = 1; row <= 3; row++) { for (int col = 1; col <= 3; col++) { cout << row * col << endl; } }```
one of her lecture slides pointing out the homework.
• 03-21-2012
laserlight
Quote:

I don't even know what an array is.
Ah. This means that the rule is aimed at more advanced students. So, just create six variables to record the number of times each of the six faces of the die are rolled.

I still don't see why you would need a nested loop though. You need one loop to get the user input with the error checking for >= 1 and loop to roll the die, but these loops are not nested.
• 03-21-2012
arsenalftw067
I don't get the last part about the nested loops. I thought it would be something like if (times = 0; times >= rolled; rolled++) to keep track of how many times the user wants to roll the dice. You say I just need one loop, how? also, how would I find the percentage of the times the number was specifically rolled IE 60 percent rolled 6 or something, and how would I keep track of how many times I rolled each number?
• 03-21-2012
oogabooga
Why don't you post some code? Do the part about getting the number of rolls the user wants and the loop to roll the die that many times. For now, just print out the result of each roll. Let's see some code! :)
• 03-24-2012
arsenalftw067
Alright so this is what I have so far

Code:

```// CSIS 135, Spring 2012, Homework#3 /*PSEUDOCODE: Ask user desired amount of times dice rolled Store data under variable time_roll Input random number generator for numbers 1-6 Calculate amount of die rolled for each number 1-6 Calculate percentage of die rolled for each number 1-6 Input result to user*/ #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; int main() {         srand ((unsigned int)time(0));         int times_rolled;         int rolled;         int num1 = 0;         int num2 = 0;         int num3 = 0;         int num4 = 0;         int num5 = 0;         int num6 = 0;         cout <<"How many times would you like to roll the dice?" << endl;         cin >> times_rolled;                         for (int i = 0; i < times_rolled; ++i)                 {                         rolled = rand() % 6 + 1;                                         if (rolled = 1)                         {                                 num1++;                         }                         else if (rolled = 2)                         {                                 num2++;                         }                         else if (rolled = 3)                         {                                 num3++;                         }                         else if (rolled = 4)                         {                                 num4++;                         }                         else if (rolled = 5)                         {                                 num5++;                         }                         else if (rolled = 6)                         {                                 num6++;                         }                 }                 cout <<"#Rolled \t #Times \t %Times" << endl;                 cout <<"----- \t ------- \t -------" << endl;                 cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << (num1/times_rolled)*100 << "%" << endl;                 cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << (num2/times_rolled)*100 << "%" << endl;                 cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << (num3/times_rolled)*100 << "%" << endl;                 cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << (num4/times_rolled)*100 << "%" << endl;                 cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << (num5/times_rolled)*100 << "%" << endl;                 cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << (num6/times_rolled)*100 << "%" << endl;                 system("PAUSE");                 return 0; }```
It compiles fine, but when I run it, and I enter lets say 5, it would only roll 1 5 times and skip the other numbers
• 03-24-2012
arsenalftw067
Sorry for double post, I realized the error was that I was declaring the value for a variable by putting else if ( rolled = 4 ) or something when it should be ==.

the program now shows how many times I rolled each number but no th
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last