Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGR 131:  Elementary Computer Programming

Lab Exercise #5

INSTRUCTIONS

Complete the exercises below and upload them to Canvas as a single MATLAB script file using the naming convention “ENGR131_22F_Lab##_abc123.m”, replacing abc123 with your Case ID, and ## with the two-digit lab number.

For example, if Dr. Williams were submitting Lab 5 it would be ENGR131_22F_Lab05_mrw8.m For your script, please perform the following:

1. Separate each question into separate, runnable sections using the “%%” comment notation.

2. You may use the code and notes from class, the textbook, MATLAB’s documentation, and anything you find using Google to solve these problems.

3. Use comments as appropriate to indicate your thoughts and how your code works (or is supposed to work). This is 5 points (10%) of your grade.

QUESTIONS

There are 3 questions for this lab.

1. SOLVING SYSTEMS OF EQUATIONS (10 PTS)

There’s a chill in the air and Fall is here. There are so many neat things that make Fall so special, as shown in Figure 1. Solve for the value of each Fall icon using:

a) the numeric systems of equations approach (5 pts).

b) using the symbolic solver (5 pts).

You will know you have the right answer as the correct values in the correct order, when multiplied by 5, rounded, converted to characters, and transposed, should spell out a recognizable word describing this time of year. Return your solution values to the Command Window.

2. VOLUME OF A REVOLVED SOLID (10 PTS)

The profile of a fancy glass is shown in Figure 2. The profile (radius) follows the equation:

r = 0.2h + 0.3cos(0.3ph) + 1.8

where h is the height in cm. This fancy glass is intended to be filled up to 10.5 cm. Compute the volume of the glass when properly filled using:

a. Step sizes of 0.1 and 0.01 cm (4 pts)

b. Using both the trapezoidal and summation integration methods for determining the volume of a revolved solid (4 pts).

Record the results in a variable that uses the rows for each

step size and the columns for the method of integration. Report this variable to the Command Window when all four values have been computed. Take a look at the values – are they all the same or different? Finally:

c. Plot the profile as shown (2 pts).

· Hint #1: You’re given two step sizes and a set of actions to repeat for each one – what kind of loop does that suggest?

· Hint #2: Do not let the figure fool you.

· Hint #3: To plot the figure, swap the order of the independent and dependent axes.

3. PUMPKIN DICE (25 PTS)

Dice games are a fun way to pass the time. Around this time of year, you may play a game called Pumpkin Dice where you choose a target number and roll 5 dice. If that number is rolled, you get the sum of all dice with that number. But beware! If you roll more 1’s (pumpkins) than your target number, you’re busted and get 0. Each player takes turns rolling with the game ending after 3 or more busts total across all players or 5 rounds (usually 10, but for brevity, we’ll make it less).

To create this game, perform the following:

a. Start out by displaying the basic rules to the command window.

b. Initialize your overall game variables including the number of rounds played, a flag for whether or not the game is over, the total number of busts, and the results for each player (2 players, 5 rounds).

c. Create another variable that is 5 random integers between 2 and 6 for the computer’s pre-determined target numbers.

d. Create a nested loop structure where (10 pts):

a. The outer loop runs the game until it’s over

b. The inner loop loops between the two players (human and computer). For each type of player, call either a function called GetHumanSelection or GetCompSelection (described below).

c. Once the player selections are found, call the RollDice function.

d. After both players have had their turn, check to see if the game is over due to either completing all the rounds or 3 or more busts. If the game is over, report why the game is over to the Command Window.

e. Once the game is over, tally up the scores and use a selection statement to report who won to the Command Window.

f. GetHumanSelection function (2 pts): Write a function that gets the user input from the human player. Use error checking to enforce that they enter a value between 2 and 6. Return this value as the output argument of the function.

g. GetCompSelection function (6 pts): Write a function that takes in the predetermined choices created in Part C as an input argument.

a. Randomly pick one of the predetermined choices:

i. Use the find command to get a vector of all of the choices that are greater than 0.

ii. From this array, randomly select one of the values (Hint: you’re picking the location (an integer) from an ever shortening vector)

iii. Set the player selection to the predetermined value that is in the location selected in g.a.ii

b. Assign a 0 to the predetermined choices element selected in g.a.iii (so it cannot be selected again).

c. Return both the player selection as well as the modified computer choices vector as output arguments.

d. Be sure to let the human player know what the computer selected by reporting it to the Command Window.

e. Hint: This may seem more convoluted than it needs to be (randomly picking a value from a set of available randomly generated values) but it will be useful later on in the semester…

h. RollDice function (7 pts): Write a function that takes in the player selection, the total number of busts, and the player number (1 or 2) as input arguments.

a. Randomly determine 5 integers between 1 and 6.

b. Find the number of pumpkins (1’s) by summing all the dice equal to 1.

c. Find the players score by summing all the dice equal to their selection.

d. Use a selection statement to determine if they won the roll or busted and report it to the Command Window. Be specific about which player it is. If they won, report how many points they earn for the round.

i. If the player busted update the total busted variable by +1

e. Return the results (score) for that player and the updated total busts variables as output arguments.