Starting from:
$35

$29

PS1 SOLUTION

Q1

Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that one and itself are the only numbers that evenly divide it (e.g., 3,5,7,11,13,17, . . . ).

One way to solve this problem is to use a doubly-nested loop. The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n then n cannot be prime. If none of the values from 2 to n-1 evenly divide n, then n must be prime. (Note that there are several easy ways to make this algorithm more efficient).



Q2

The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll:

    • If the player rolls a 2-6 then he can either:

– ROLL AGAIN or

– HOLD. At this point the sum of all rolls made this turn is added to the player’s total score and it becomes the other player’s turn.
    • If the player rolls a 1 then the player loses his turn. He gets no new points and it becomes the opponent’s turn.

If a player reaches 100 or more points after holding then the player wins.

Write a program that plays the game of Pig, where one player is a human and the other is the computer. Allow the human to input “r” to roll again or “h” to hold.

The computer program should play according to the following rule: Keep rolling on the computer’s turn until it has accumulated 20 or more points, then hold. Of course, if the computer wins or rolls a 1 then the turn ends immediately. Allow the human to roll first.

Write your program using at least two functions:

int humanTurn(int humanTotalScore);

int computerTurn(int computerTotalScore);

These functions should perform the necessary logic to handle a single turn for either the computer or the human. The input parameter is the total score for the human or computer. The functions should return the turn total to be added to the total score upon completion of the turn. For example, if the human rolls a 3 and 6 and then holds, then humanTurn should return 9. However, if the human rolls a 3 and 6 and then a 1, then the function should return 0.

Notes: Special attention should be made to the description of the humanTurn and computerTurn functions. It indicates that an outside variable must keep track of the total score and add it to the value returned by the functions at the completion of each player’s turn.



Turn In

    • Make and submit a zip file(<your_full_name>_PS1.zip) which includes the following:

– Source code of Q1: q1.cpp

– Source code of Q2: q2.cpp

– Run Q1 and Q2 and attach screenshots(in jpg format, not exceeding 300kb each) which show that your programs are running.
– At least 1 screenshot for each question.





1

More products