Homework help

This is a discussion on Homework help within the C++ Programming forums, part of the General Programming Boards category; need some help with my C++ homework. here is the assignment: PROGRAM #1 - DICE ROLLING SIMULATION This program should ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    Quote Originally Posted by oogabooga View Post
    ...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.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    You could even hash the actual time to an unsigned int and not cast anything at all.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    What did your instructor show you during lecture?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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?

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help
    By andrei1599 in forum C# Programming
    Replies: 1
    Last Post: 02-06-2011, 08:38 PM
  2. My homework
    By Bala in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2010, 11:11 AM
  3. help about homework
    By agathery in forum C Programming
    Replies: 27
    Last Post: 05-19-2010, 09:17 PM
  4. homework
    By misplaced in forum C++ Programming
    Replies: 18
    Last Post: 10-04-2004, 06:56 AM
  5. I want homework!
    By Tynnhammar in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 02:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21