You are on page 1of 4

ENGR 131: Elementary Computer Programming

Team IN – Instructor

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

For example, if Dr. Williams were submitting this test, it would be ENGR131_22S_Midterm_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, and MATLAB’s documentation.
3. Use comments as appropriate to indicate your thoughts and how your code works (or is supposed to work).
This is 10 points (10%) of your grade.

QUESTIONS
There are 2 questions for this exam.

1. SOLVING SYSTEMS OF EQUATIONS (40 PTS)


The company you’re working for has an
electrical system with the schematic shown in
figure 1. There is a critical component that helps
to govern the current in the left and right loops.
Your boss figures the value is anywhere between
0 and 5 Ω, but they’re unsure of the exact value.
Fig. 1. Schematic of system with super critical, but unknown part.
What IS known is that the current in the left loop
cannot exceed 2.75 A and the current in the right
loop cannot go over 2.1 A. Also, your suppliers can get you this supper critical part with either a 20% or 25%
tolerance for the price your company is willing to pay. Write a script to perform the following:
A. Write a function that models this circuit as a system of equations. You may use either the numerical or
symbolic method to do so. (10 pts)
B. Use this function to compute the current in each loop for the range of possible resistances (in 0.1 Ω
increments) and identify the upper and lower limits based on the current limits specified (8 pts)
C. Plot each loop current using a different color and no markers. Plot the upper and lower limits using a
single marker for each and increase the size appropriately so your aging boss can clearly see them. Include
all pertinent labels. (10 pts)
D. Report the upper and lower limits (to 1 decimal place) back to the command window. (2 pts)
E. Compute the nominal resistance (the middle of the upper and lower bounds) and report this back to the
command window (2 pts)

ENGR 131 22S-IN-060-100-A (Midterm Instructions) 2/11/2022 Page 1 of 4


F. Use a loop and a selection statement, your computed nominal resistance, and the upper or lower bounds
computed in 1.B to determine whether the proposed tolerances are acceptable and report if they are or are
not back to the command window. (ie. Is it OK if your nominal resistance grows by 20%? What about
25%? 8 pts)

2. BLACKJACK! (50 PTS)


Write a script to play a simple game of Blackjack. The rules are:
• Each round costs the player $1
• Aces are worth 11 to start, but if the total is over 21, can be changed into a 1.
• A round starts with giving two cards to the player.
• The dealer must always draw until they reach a card total of 17 or are over 21 (busted). If the later occurs,
the round is over.
• In the event the dealer is not busted at the start of the round, the player can either take a card or end the
round (called standing)
• The round continues with the player sequentially taking cards until they decide to stop or are busted.
• At the end of the round, whoever is closer to a total of 21 without going over, wins. In the event of a tie,
the player gets $1.
• If the player wins, they get $1.50
• The game continues until the player decides to quit the game.

Use Matlab to program this game by doing the following:


A) Initialize the game and set the starting player cash at $10
B) Create a randomized set of cards consisting of 4 decks of 4 suits. In each suit, the values range from 2 to 9,
three cards worth 10, and one worth 11 (5 pts). Hint: While real playing cards have both numerical and text
labeled cards (Jack, Ace, etc.) as well as symbols (clubs, hearts, etc.) it’s MUCH easier to just go with the
values.
C) Display the rules to the player at the start of the game (5 pts)
D) Use a loop to get commands from the user with a prompt with instructions to press p or q to play a round or
quit (10 pts):
a. When the user presses p, take $1 from the play and call a function written to run one round.
b. The game loop should accept one p or q. For all other entries, remind the user to enter only the two
approved entries.
c. This game loop should run until the player is either out of cash, or quits by pressing q.
E) Use the following functions to keep your code modular:
a. A function which runs the mechanics of the round (15 pts):
i. Gets cards for the player and dealer
ii. Plots the cards for the dealer and player
iii. Executes a loop prompting the user to either draw (d) or stop (s). Proper input error checking
should be employed. If the player goes over 21, the round is over
iv. Determine the outcome (who won or is it a tie?)
v. Display in the command window the result of the round
b. A function to get cards that performs three distinct tasks (10 pts)

ENGR 131 22S-IN-060-100-A (Midterm Instructions) 2/11/2022 Page 2 of 4


i. Takes a card from the deck and places it into the players hand
ii. Sequentially corrects for any Aces (values of 11) if necessary, until the total is under 21 (if
possible)
iii. Determines if the total is over 21 (hence, busted).
c. A function that plots the cards (10 pts)
i. Changes the bar color to red if that entity’s total hand is over 21 (otherwise it’s black)
ii. Plots a bar for each card and the total of all cards, with data labels above each card as shown
in Figure 2.
iii. Appropriate labels and titles are used.

Dealer Dealer
30 30

22
20
20 20

10 10 10
10 10
7
5

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
1 2 3 4 5 6 7 8 9 10 Total 1 2 3 4 5 6 7 8 9 10 Total
Card Card
Human Human
30 30

21
20 20
17

11 10
10 10
6 7
4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
1 2 3 4 5 6 7 8 9 10 Total 1 2 3 4 5 6 7 8 9 10 Total

(a) Card (b) Card

Dealer
30

20 18

10
10
6
2
0 0 0 0 0 0 0
0
1 2 3 4 5 6 7 8 9 10 Total
Card
Human
30

20

12
10
9

1 2
0 0 0 0 0 0 0
0
1 2 3 4 5 6 7 8 9 10 Total

(c) Card

Fig. 2. Starting hands for the dealer and player in blackjack (a). Since the dealer busted, their hand is shown in red and the
player wins. The next round is shown in (b). In this round, the player chose to take a card after seeing that their first two
summed to 17 while the dealer had 20. That 4 gives the player the win. An example of a corrected Ace can be seen in (c).
In this round, the player started with a 13 (11 + 2) but couldn’t beat the dealer’s 18. Drawing a 9 put them over 21, so the
11 was reduced to a 1 for a total of 12. Still isn’t going to beat an 18, so they better draw another card…

ENGR 131 22S-IN-060-100-A (Midterm Instructions) 2/11/2022 Page 3 of 4


Hints:
• This may seem daunting but consider mapping out your thought process. Most of the operation is little
more than selection statements and a few loops.
• Consider what information each function needs to operate. Will this be entered as input arguments? Taken
from the workspace variables? Hard coded? Plan this out before you start coding.
• What information is each function modifying? Is this returned as an output or placed into the workspace?
Plan this out before you start coding
• Prototype each part as you go. Get each function working separately with “dummy” data first, and then
put it all together.
• You have seen and/or done all of this before – look back to your previous labs and lectures for inspiration!
• Yes, it’s an “exam” but it helps to think of it more as a puzzle to solve than a task to do.

Revision Description Date


A Original Document 2/11/2022

ENGR 131 22S-IN-060-100-A (Midterm Instructions) 2/11/2022 Page 4 of 4

You might also like