I always give a positive rating!
Language is C++
I know this looks really long, but this is actually
pretty easy.
Just follow the steps in the document below and paste
your code in the answer.
Thanks!
In-Class 4 - Random Number 12-Sided Die For this exercise, you get some practice with writing code for random number generation and using if statements. You simulate rolling a 12-sided die that has 6 sides with numbers 1 to 6 in red and 6 sides with numbers 1 to on black: 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6. You must use the starter code at the end of this assignment!!! That code produces output that I am using in class to show you Head & Tails AND how to generate number 1 to 6 and print the words one, two, three, four, etc. Enter a seed value: 9 Tails . 3 two Red2 two The code for this assignment just prints this line. Objectives Practice with Random number generator Practice how to scale random numbers to a desired range Writing IF statements to translate generated numbers to text Writing Booleaan conditions NOTE: Look at the Input/Output!!! Description Your program is a simulation of rolling a die that has 12 sides, numbers 1 to 6 in Red and numbers 1 to 6 in Black. There is NO input for this program. When you run the program it generates 1 of 12 outputs: 1. Redi One Red2 Two Red3 Three Red4 Four NOTICE the all- 5. Red5 Five important spacing!!! Red6 Six 7. Blackl One 8. Black2 Two 9. Black3 Three 10. Black4 Four 11. Black5 Five 12. Black6 Six You should try to print the numeric value (1, 2, 3, 4, 5, 6) in a single cout but the text (One, Two, Three, etc.) inside if-then-else statements. You must use srand (seed) and rand() to generate numbers, not the code from Chapter3 that includes random_device engine and uniform_int_distribution int>. In order to test your code on Web-CAT, you need to generate numbers not-completely randomly! As you write your code, it should include a call to time and srand like 1
int seed = time (0); cout << "Enter a seed value: "; // cin >> seed; srand (seed); But when you submit your code to Web-CAT, you need to un-comment the cin line int seed = time (0) ; cout << "Enter a seed value: "; cin >> seed; srand (seed); This makes your code always produce the same output so it can be graded. Input/Output NO input. Your added code should randomly produce one of these: Redl One Red2 Two Red3 Three Red4 Four Red5 Five Red6 Six Black1 One Black2 TWO Black 3 Three Black4 Four Black5 Five Black 6 Six Start with this Code (You MUST use this code!) #include <iostream> #include <chrono> /** to use the time() function call for randomness */ #include <random> /** all the random function calls */ using namespace std; int main() { int seed = time (O); cout << "Enter a seed value: "; 2
// cin >> seed; // UN-comment this line before you submit! // Otherwise your code will not be graded srand (seed); int val = rand(); if ( val % 2 ) // != 0 ) // ois FALSE, everything else is TRUE cout << "Heads\n\n"; else cout << "Tails\n\n"; int vall = (rand() % 6) + 1; if ( vall == 1) cout << "one\n"; else if ( vall == 2) cout << "two\n"; else if (vall == 3) cout << "three\n"; else if ( vall == 4 ) ? cout << "four\n"; else if ( vall = 5 ) cout << "five \n"; else if ( vall = 6) cout << "six\n"; /** simulate the roll of a 12-sided die that has numbers 1 thru 6 red and and 1 thru 6 black, e.g. Redi, Blacki, Red2, Black2, Red3, ... Red6, Black 6 add in the words for the numbers: one, two, etc. */ /* declare a var && generate a number 1 - 12, 1-6 read and 1-6 black #/ int num1 = // fill in this blank, use rand() /* 2 4 6 8 10 12 are black=0, and 1 3 5 7 9 11 are red=1 */ int color = ((numi % 12) + 1) - 6; /* 1 & 7 are 1, 2 & 8 are 2, 3 and 9 are 3, etc */ int die = ; // fill in this blank using numi, not a new rand() number //YOUR CODE goes here to output the assignment return 0; } Programming Style An important part of programming is using proper programming style, formatting, and comments. At this point, the most important items are: 3
. Use descriptive variable names • Vertical (proper use of blank line mostly) and horizontal spacing (putting spaces around operators, indentation, ctc.) o Indent properly - statements and braces should be lined up; auto-indent USE Plugins→Source Code Formatter (AStyle) in CodeBlocks USE in XCode default shortcut for Formatting is ctrl+I o Put a blank line between each small section of your program, so that groups of related statements are logically grouped by separation with blank lines Tlave a beginning comment that describes what the program is doing (just "Homework 2" is not good enough). See the main Canvas page for Coding Guidelines about what every program header needs to contain. Each section (i.e. logical chunk of code) begins with a comment describing what that section does, so that the comments alone would provide an outline of the program (It's NOT good to follow every line of code with a comment.) a Continue typing your code on the next line if it is too long. Too long is over 80 characters. In Code::Blocks, there is a column indicator at the bottom-middle-ish of the screen that tells you which column the cursor is at. . . Submit Your Work To submit your work to Web-CAT to be graded. 4
I always give a positive rating! Language is C++ I know this looks really long, but this is actually pretty easy. Just f
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
I always give a positive rating! Language is C++ I know this looks really long, but this is actually pretty easy. Just f
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!