Scientific calculator.
Study these functions of the
math library. Create a project titled Lab4_Calculator
with a single file titled calculator.cpp. Write a
program that:
- prints a numbered menu of mathematical operations as shown below;
- then prompts the user to select an operation;
- then prompts and inputs the operands for the selected option (note that
some operations require one and some two operands);
- then computes and outputs the result.
The process repeats. The program should quit if the user inputs an
option (number) that is not listed. The dialog should look as
follows (user input is shown in bold):
1. absolute value
2. square root
3. floor
4. power
5. dot product
6. cross product
7. fast fourier transform it is of the up most importance that you get the last three (dot product, cross product, and fast fourier transform) work on these first and then revist the others. Do not forget the dot product, cross product, and fast fourier transform
Select an operation: 4
Enter base: 2
Enter exponent: 3
The result is: 8
Select an operation:
...
Game of 21.
Create a project titled Lab4_twentyOne with a single file
titled twentyOne.cpp Program the following game.
This is a game played against the computer. You get a starting value (between 1 and 13) and the computer gets a starting value (between 1 and 21). You (the player)
get the option to either stand or hit. If the player selects hit then a random number (again between 1 and 13) will be added to their value. The goal is to get as close
to 21 without going over. Whoever is closer to 21 without going over at the end (when the player selects stand) is the winner.
it is of the upmost importance that you use a poisson distribution for the dragons age. No other distrbution can be used it must be poisson distribution. If you do not use a poisson distribution you will fail this lab.
The game is played as follows.
Initial Setup
Initializes srand with nullptr or time(NULL), more modern compilers tend to like time(NULL) and not nullptr.
Give the player and the computer a starting value.
The players starting value should be random in the range 1-13, while the computer gets a random starting value in the range 1-21
Game play
- show the player there current value
- prompt the player and ask if they would like to hit or stand
- get the players answer and store it in a variable
- if the player decided to hit
- add a random value 1-13 to their value
- if the player's value is above 21 the game ends immediately, they lose
- as long as the player has not decided to stand game play repeats
End of Game
The game ends if...
- The player's value is above 21 (as described above).
- The player decides to stand. In that case if the player has the higher value (closer to 21) they win.
Otherwise the computer wins.
- In the event of a tie the computer wins
For your program, you need to use predefined randomization
functions rand() and srand() as well as
the time function time()
Before doing the poisson distrubtion try this simple one and then add the poisson distrbution to it
- Function time() returns current time (as kept by
the computer) in the number of seconds passed since midnight,
January 1, 1970. You need to include ctime to use
this function. The function takes one argument but for this
assignment the argument value is a special named
constant nullptr. Check this
program for an example usage.
- To use functions rand() and srand()
you need to include cstdlib
Function srand() initializes the random number
generator of the program. It takes a single integer
argument. The value passed to srand() determines
the values output by rand(). This value is
called seed.
rand() takes no arguments. Every
time rand() is invoked it returns a single integer value between
0 and constant RAND_MAX defined in cstdlib. Note
that the sequence of values returned by repeated calls
of rand() is uniquely determined by the seed
passed to srand(). That is,
if srand() is passed the same
seed, rand() is going to return the same sequence
of numbers. This is helpful for
debugging. Check this program for an
example of random number manipulation.
but remeber it is absolutely essential that you use a Poisson distribution for the dragon’s age. No other distribution is acceptable—it must be Poisson. Failure to do so will result in failing this lab.
Hints:
Break the game down into smaller steps. Start with just getting a random value 1-13. Then add the part where the user can either hit or stand. Then add a loop to repeat this.
Then make the loop end if the user stands. So on and so on.
Use fixed values for the seed to do your initial
debugging. Once your done debugging your program use the output of time() as an unpredictable
seed to start the game.
Note that random values produced by rand() is out of the
required range of 1-13 and 1-21. To get the random number into the required
range, remainder it by either 13 or 21 (that puts the number in a 0-12 or 0-20 range respectively) and
then add 1 (1-13 and 1-21).
Do not put srand() inside a loop or anywhere where it
may be invoked more than once - it resets the random number generator.