C++代写:CS133 Exercises

代写一份C++作业,包含五个基础的练习题。

Exercise #1 – Composite Number Problem

A prime number is defined as any number that is only divisible by itself and 1. For instance the numbers 2, 17, and 53 are all prime numbers. A composite number is simply defined as any number that is not a prime number. For your program you will create a function called isComposite() which takes an integer as an argument and returns a boolean value (true if the integer is composite and false if otherwise.) As an example the following function call will return true:

isComposite(16)

Your program will then find the first 100 composite numbers and output the result to a file called composite.txt (with the main() function calling isComposite() in the process.)

Save your solution in a file named composite.cpp.

Exercise #2 – Coin Problem

Write a function called flipCoin() which takes in no arguments and returns the following integer randomly:

  • being HEADS
  • being TAILS

Your program will ask the user how many times the coin should be flipped. Your program should then call flipCoin() as many times, and then output the number of times HEADS landed and the number of times TAILS landed.

Save your solution in a file named coin.cpp.

Exercise #3 – Rock Paper Scissors Problem

The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic:

  • ROCK defeats SCISSORS (“smashes”)
  • PAPER defeats ROCK (“wraps”)
  • SCISSORS defeats PAPER (“slices”)

If both players choose the same selection the game ends in a tie. Write a program that asks the user to select one of the three choices. The computer will randomly choose one of the three options as well. Your program will then display both players’ selections as well as the who won the game.

Be sure to divide your program into functions which perform each major task.

Save your solution in a file named rps.cpp.

Exercise #4 – Date Problem

A leap year is defined as any calendar year which meets the following criteria:

  • If the year is not divisible by 4, it is a common year
  • If the year is not divisible by 100, it is a leap year
  • If the year is not divisible by 400, it is a common year
  • Otherwise it is a leap year

For your program you will take in three integers from the user:

  • A four digit year
  • The day of the month (between 1 and 31)
  • The month (between 1 and 12, 1 representing January and 12 representing December) Your program will output whether or not the date entered is a valid calendar date.

Be sure to divide your program into functions which perform each major task.

Hint: There are 30 days in September, April, June and November. If the year is a leap year, there are 29 days in February. If the year is a common year there are 28 days in February. All other months have 31 days.

Save your solution in a file named date.cpp.

Exercise #5 – Monty Hall Problem

Imagine yourself on the set of a game show. You’re given the choice of three doors. Behind one of the doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats.

After you initially guess the door, the host of the game show (who knows the door holding the car) opens one of the other doors revealing a goat. He gives you the choice: stick with your original door, or choose the other unopened door.

This problem is reminiscent of a Sixties game show hosted by an individual named Monty Hall. For this assignment you will write a program simulating this game and the results of either choice. You will ask the user how many times they wish to run the simulation. If the user enters 1000 for instance, your program will run through 1000 simulations where the player sticks with the original door and 1000 simulations where the player chooses the other unopened door. After the program is completed execution, the winning percentages will then be displayed for each strategy. (Be sure to make the initial selection, Monty’s selection, and the second-chance selection happen randomly.)

Be sure to divide your program into functions which perform each major task. (The purpose of this assignment is to find out whether or not it’s to the player’s advantage to change their selection.)

Save your solution in a file named monty.cpp.