Instructions: Create a subdirectory named “hw1” in your cs410 directory. Use that subdirectory
for your files submission on this assignment. You will create the following two files in your hw1
directory:
1. a single C++ compilable file containing a program written in C++ named “hw1.cpp”
2. a “typescript” file demonstating program compilation, execution and testing. Use the
commands below at the UNIX prompt to generate the typescript file:
script command to start a typescript.
ls -l to list files in your directory and write date/time
cat hw1.cpp to print out solution file
g++ -o hw1 hw1.cpp to compile program
./hw1 to execute program with test input (provided on last page)
exit command to end typescript file
Background: Hans Moleman needs a new pair of glasses. He’s going
to try out a new optometrist in town, a Dr. Riviera. He has been involved
in “cutting edge” (see pic) research (see other pic) which is why he has
changed residences and started a new practice in optometry. But I
digress. Dr. Nick has developed a new formula for determining the
grinding thickness for lenseseses. Your program is going to do that
computation. Roughly speaking, the thickness of the lenses (both the same for right and left, since
Dr. Nick believes in simplicity) is dependent on both left and right visual acuity, some cliometric1
constants, and the patient’s social security number. The formula is given below
2
1
5
VA
VA
L
thickness K IAF
R K
where:
thickness is an integer value in cm (centimeters),
LVA is left visual acuity, a positive integer,
RVA is right visual acuity, a positive integer,
K1 is the index of refraction constant that Dr. Nick has
discovered empirically. It’s value is the constant
integer value 2,
K2 is the Nick Needs Money constant which ensures Dr. Nick a minimum income from
each customer; currently it is 4.2, and
IAF is the insurance adjustment factor
IAF is the sum of the last two digits of the patient’s soc sec number. This factor is added into the
thickness computation ONLY IF the patient has insurance. Why? Well, it screws up the
prescription and then they have to come back to Dr. Nick for another diagnosis and more new
lenses. Hence, Nick can bill the insurance companies for more money.
1 We have no idea what this word means. It sounds cool and impressive, and it boosts our esteem
to use it; we come off as really smart now. Don’t look it up.
Specifications: Your program is to prompt the user for:
Left visual acuity (LVA) and right visual acuity (RVA), both entered as
integers,
The last 4 digits of the patient’s social security number (this is
entered as a single 4-digit integer), and
Whether the patient has insurance (1 for yes) or not (0 for no). The
value be read into a boolean type variable.
Note: The thickness value computed should always be rounded down to the nearest integer value.
You are not allowed to use conditional statements (if or if-else) in the program. Think carefully
how to use the information you have input from the user, how it is stored, how boolean variables
are represented. It is up to you how you want to name your variables and constants. Your program
should be “user friendly” in that it should have an opening statement or greeting, user friendly and
understandable prompts, and clear and concise outputs and sign-off. Here’s a good example of bad
output:
Hello
34
12
3456
0
22
Notice that there are NO prompts to speak of, and the output is completely unexplained. This is
horrible output. Something like the following is much much better:
You are using the LENS-O-MATIC program
enter:
left visual acuity: 34
right visual acuity: 12
ssn: 3456
ins? 0
thickness of lens: 23 cm
Good luck with those glasses! And come back to see Dr. Nick !
When you submit: When you submit, you will have to enter inputs as a user of the program.
Now, in order to make the output uniform for the grader and to keep them sane, ALL OF YOU
will enter the same information. For this assignment, it is:
left visual acuity: 20
right visual acuity: 30
last 4 of soc sec: 4567
yes for ins coverage
If you have any questions about this assignment, be sure to ask your TAs or instructor.
Background: After getting the right thickness of his lenses, Hans Moleman decided to become an
orthopedist. To clarify, that’s a person who treat issues affecting the bones, muscles, tendons, and
ligaments. He seems to have had a history of muscle cramps and pain. Towards this end, he has been
doing some of his own home-grown research, writing up results of exquisite and intricate experiments
on how muscular-aches occur and are mitigated or treated. For the benefit of the general public, he has
published a flow chart on how one might treat or react to bone and muscular issues. His flow chart is
shown on the next page. You’ll notice that, as you follow the flow in this chart (this flow chart), you
eventually end up with either a recommendation or some kind of medical commentary that might help
you out in the near future. Some of it is just simplistic babble, but the user might still find some value
in it.
Specifications: You are to write a program that implements this flowchart. Briefly, your program will
prompt for and input information about the user (the sick person) and use the responses to lead to one
of the “diagnostic messages” Hans has crafted. Of course, you are to do this right. Here are some
requirements:
You are to use if and if-else statements for decision branching. You are NOT allowed to use the
switch-case statement.
You are NOT to us the goto, the break, or the continue statements. You are to use logic and
logically structured code to implement.
Your program should greet the user and prompt for and read in their name (first name only.
Assume the name is a contiguous string of non-whitespace characters). Your code should then
use that name, in at least one prompt, for info after that and in the final
output/diagnosis/recommendation.
Your program should do what we call “input cleansing” or range checking. That is, when you
prompt for information that can be validated for value, your code should do that. For example,
in many of the prompts you will have, you will ask for a response of yes or no (y/n). If you input
the response as a char, you can then check to see if they indeed entered ‘y’ or ‘n’ and re-prompt
them if not.
At the end of the execution of a “run” of this stress inquiry, your program should ask the user if
they wish to start again – ostensibly for someone else. In any case, the whole line of questioning
and diagnosis should repeat until no one else has muscular problems.
2
When you submit: When you run your program, use the following responses to questions as indicated
by the flowchart:
yes, no, no, no, no, no, no, yes
enter a “yes” response to evaluate another problem
answer no to all questions
again, enter a “yes” response to evaluate another problem
no, yes, no, yes
enter a “no” response to exit the program
As usual, if you have any questions about this assignment, be sure to ask your TAs or instructor.
Hans Moleman
Instructions: Create a subdirectory named “hw3” in your cs410 directory. Use that subdirectory for your all file submissions on this assignment. At the end of the homework assignment, these two files should be found in your hw3 directory: 1. a single C++ compilable file containing a program written in C++ named “hw3.cpp” 2. a “typescript” file demonstating program compilation, execution and testing. Use the commands below at the UNIX prompt to generate the typescript file: script command to start a typescript. ls -l to list files in your directory and write date/time cat hw3.cpp to print out solution file g++ -o hw3 hw3.cpp to compile program ./hw3 to execute program exit command to end typescript file Background: Poor Hans. For most of us, life is a trial-live one day at a time and we usually do pretty well with minimal effort. However, for Hans Moleman, life is really tough. Being nearly blind, he has a hard time seeing, something most of us take for granted. Nevertheless, he got new lenses for his glasses and that should have solved his problem. But, alas, he got the lenses at the new super store in Springfield, GalMart, which employs a Dr. Riviera who uses some ridiculous, crazy new formula for computing lens thickness. Now he can hardly see what he’s doing or where he’s going with his glasses on. SO, he needs to get back to Dr. Nick’s to have his sight re-evaluated and new lenses made. The office is 1414 feet from his house. He cannot drive in his current condition, and he’s scared to death of buses, so he’s going to walk. The thing is, Hans may or may not make it to his intended destination! Three possibilities exist: 1) he makes it to the doctor’s office; 2) he ends up back home; and 3) after a long time, he’s so far away that he is picked up by the local police and taken to a homeless shelter. You’re going to write a program that will simulate his walk across town. In fact, your program will generate 250 possible walks and gather statistics on the results. Specifications: Your program will simulate 250 walks. A “walk” begins at home and proceeds from decision point to decision point until the walk terminates. Termination happens when a decision point is: within 500 feet of Dr. Nick’s office within 50 feet of home and there are at least 5 other decision points preceding this point the 400th decision point The first of the above conditions implies that he is close enough to his intended destination to be able to see that he has arrived. The second implies that he has been walking for a while and hasn’t actually gone anywhere, so he just gives up and goes back inside and watches re-runs of the Simpsons! DOH! The third simulates a lost old man wandering off into the horizon never to return. Details: At each decision point, he will almost always turn right. This is because his eye-sight is so screwed up that his vision “pulls” him in that direction. However, every 7th decision point he turns left. Also, for the first walk, he will walk 20 feet away from his house to his first decision point. On the second walk, he walks 21 ft before the first decision point (note this is NOT the second step of the first walk), etc…Each first decision point is 1 foot further from the house than the previous one. You can choose the direction of this first move, but be consistent from walk to walk, starting in the same direction. And, the distance he walks after each decision point is given by: 0 20 1 , 21 2 , st nd d initial number of feet from the house for walk f or walk etc 1 124985 1367892 1654872235 300 21, 1 , 2, 3, … i i d d mod mod i If you are unfamiliar with an iterative formula like this, this is how it works. The first distance walked (as stated above) is 20 ft. You’ll substitute this into the second equation above to compute the 2nd distance, d2. Then substitute d2 into the equation to compute d3. Continue in this way. For his second walk, you’d start with first distance walked being 21. The output from your program should be a listing of the instances when he either goes nowhere (ends up at home) or finds his way to Nick’s office, and how many feet he traveled to get either of those two places. And, if the distance walked is greater than a mile, output that distance in miles. After that, output the percentages of times he got to Nick’s, ended up at home, and got lost in the ozone. Part of my output looks like this: Ended walk at total dist traveled (ft) Nick’s 17709 (~ 3.35398 miles) home 9493 (~ 1.79792 miles) Nick’s 17663 (~ 3.34527 miles) home 7803 (~ 1.47784 miles) home 2161 home 2742 Nick’s 18774 (~ 3.55568 miles) home 9804 (~ 1.85682 miles) home 15501 (~ 2.9358 miles) Nick’s 11444 (~ 2.16742 miles) Nick’s 17673 (~ 3.34716 miles) Also: In order to code this problem, I’d suggest you establish a coordinate system for the location of Hans, the doctor’s office and Hans’s home. Since Hans starts his walks at home, put home at (0, 0). Since Nick’s office is 1414 feet (straight line) away, put it at (1000, 1000). You can work out the rest from there. In order to compute the distance from one point to another, you will need to use the sqrt() function. It is contained in the library . Just #include it like you do the iostream library. This function will return a float type and you will need to put what you wish to find the square root of inside the parentheses. When you submit: Just submit, since there is no input from the user. And, as always, let your TAs or instructor know if you need any help.
Instructions: Create a subdirectory named “hw4” in your cs410 directory. Use that subdirectory for your all
file submissions on this assignment. At the end of the homework assignment, these two files should be found
in your hw4 directory:
1. a single C++ compilable file containing a program written in C++ named “hw4.cpp”
2. a “typescript” file demonstating program compilation, execution and testing. Use the commands
below at the UNIX prompt to generate the typescript file:
script command to start a typescript.
ls -l to list files in your directory and write date/time
cat hw4.cpp to print out solution file
g++ -o hw4 hw4.cpp to compile program
./hw4 to execute program
exit command to end typescript file
Background: In this assignment, you are going to write a menu driven program to help
Hans out with his “health management issues”. Yep, indeed, it’s no longer enough to just
take care of yourself by eating right and getting enough exercise. You had better spend
a lot of money, have a personal health trainer, a membership to a gym, take way too
many pills and subscribe to every hair-brained self-help scheme alive. But I digress. This
program will present the user with several options concerned with improving health in
varied manners. The user will choose an option, following instructions about prompted
information, receive the intended recommendation, and then have the menu presented to
them once again. Of course, the user can quit at any point. Your code will be
implemented using functions. Now you will have your practice with them.
Specifications: You are to use functions in the program. In fact, below is a description of the functions we
want you to use. Your program will present a greeting and menu of options to the user. It should look
something like this:
Health-o-Matic Options
—————————-
1. Taking a Walk!
2. Your Medications
3. Nap Time!!
4. Caloric Intake
5. Quit
1. When the user chooses option 1, the program should prompt for and read in the number of “legs” of the
walk the user wishes to have. The total number of steps from the “legs” should be computed and
displayed.
2. Option 2 chosen will trigger prompts for the user to input their current anxiety level (1 -> 10) and the day
of the week (1 -> 7). From this information, the program will compute and output the number of pills the
user should take that day.
3. If option 3 is chosen, your program is to refuse to do anything if both options 1 and 2 haven’t yet been
chosen during that run of the program. That is because the computations for option 3 require the
information from options 1 and 2. Now, if options 1 and 2 have indeed been chosen, then your program
code for option 3 starts by prompting the user for the number of hours they slept the previous night. This
information, along with the computed values from options 1 and 2, will be used to compute and output
the number of minutes you may nap for.
4. Choosing option 4 will initiate prompts for body weight, body height, and room temperature. From this,
the code should compute and output caloric intake allowable at that moment.
5. Choosing quit will terminate the program….of course.
Details: First, you are required to use the switch statement for
handling the options chosen from the menu. Secondly, you will use
functions for the program and they are laid out here:
1. A function that does nothing more than displaying the greeting for the program. It should have no
parameters and return nothing.
2. A function to display the menu and reads in a response (character) from the user and returns it to the main
function. It should NOT “input cleanse”; any response from the user should be handled by the switch in
main.
3. A function that will prompt for and read in a positive integer for the number of legs of the walk. This
(cleansed) value is returned to the calling function (presumably main).
4. A function to which you pass the value returned by the preceding function, and it will output the total
number of steps according to:
total number of steps number of legs !
e.g. if number of legs is 4, then total number of steps = 4! = 4*3*2*1=24 steps.
5. A function that prompts for and reads in anxiety level (valid input is 1 -> 10) and returns it.
6. A function that prompts for and reads in the day (valid input is 1 -> 7) and returns it.
7. A function to which you pass the two values above (anxiety and day) and it returns the number of pills
the user should take according to:
number of pills anxiety day if this is non negati – , ; 0 . ve otherwise
8. A function to which you pass the number of pills and it will output it to the screen.
9. A function that prompts for and reads in the number of hours slept the previous night.
10. A function to which you pass the number of hours slept, the distance walked (i.e. total number of steps)
as determined by option 1, and the number of pills as determined by option 2. It will return the number
of minutes for a nap according to:
0, 1
dist walked minutes hrs slept
number of pills if divide by
11. A function to output the number of minutes for a nap. It will have one parameter.
12. A function which prompts for and reads in the user’s weight, height, and room temp (along with the
distance walked), then computes and returns calories according to the formula
1
cals wt ht dist walked 6 2 .
temp
13. A function to which you pass the value from #12 above and it will output it.
For the foregoing functions, if they are to prompt and input values, they should “cleanse input” with
reasonable limits. For example, prompting for a weight should reject responses that are non-positive and
those over 800lbs.
When you submit: As usual, when you submit, you are all to enter the same information so as not to drive
the graders crazy. Following these steps:
choose option 3 (better get an error message!)
choose option 4 (get an error message)
choose option 1 and enter 6 (6 legs for the walk)
choose option 3 (again, error!)
choose option 2 and enter anxiety of 3 and day 5
choose option 3 and enter 5 hrs sleep
choose option 4 and enter 250 lbs, 72 inches, and 79.7 F.
quit
And, as always, let your TAs or instructor know if you need any help.
Background: Our friend Hans Moleman has been through a series of health problems these past weeks. Interestingly, he believes he has paid more on his bills than he should have. He feels very vulnerable with his finances because he is very bad at math. Now he wants to improve his math skills, so you are going to write a program that will help him check his work. He will be manually computing values for exponentials, trig functions, and roots of numbers. So he needs a “calculator” of sorts so he can check his answers. Specifications: Your program will begin by presenting a menu with these choices: OPTIONS ———— 1. Factorial of x 2. Exponential of x 3. Cosine of x 4. Roots of x 5. Hyperbolic sine of x 6. Quit In fact, your program should present this menu until the user chooses the quit option. You are to “function-ize” the code as much as practicable. That is, you will create functions to do much of the work, and your main will call functions, and they will call functions….and maybe even they will call functions. The idea is to make your code easier to use and read. Do not use the cmath library. When option #1 is chosen, offer to compute the factorial of any input. Prompt for the input (should only allow non-negative input less than or equal to 10) When option #2 is chosen, you will prompt for any value of x for calculating ex . You will use the formula “pattern” given in hw 4: e x = 1 + x + x2 /2 + x3 /6 + x4 /24 + x5 /120 + … = x 0 /0! + x1 /1! + x2 /2! + x3 /3! + x4 /4! + x5 /5! + x 6 /6! + … (Note: 0! = 1) The number of terms used in this calculation is 8. But that should be changeable to any positive integer at a moment’s notice! (In other words, you’d better use loop(s) for this computation.) When option #3 is chosen, the user is to be prompted for the value of x (any real value) for which to compute the cos(x). But then, they are to next be prompted for a positive integer between and including 1 and 5 representing the accuracy of the computation. This value will determine the number of terms to be included in the truncated Taylor series which calculates an approximation to the cos(x). This is that formula: cos(x) = 1 – x 2 /2! + x4 /4! – x 6 /6! + x 8 /8! … + x2k/(2k)! k = 1, 2, 3, … Thus, if one wants 2 terms, that corresponds to k = 2 in this formula and cos(x) = 1 – x 2 /2! Of course, it’s not really equal (=), but we’ll pretend. By the way, the larger k is, the better the approximation is. And if you add on terms forever, the equality is indeed guaranteed. Try it! It’s definitely worth the time. Note: k! is “k factorial” is k * (k-1) * (k-2) * … * 3 * 2 * 1 (e.g. 4! = 4*3*2 *1 = 24). And, of course, output the value. When option #4 is chosen, you will prompt for the value, x, to find the root of. But then you will prompt for which root to find: square root (n = 2), cube root (n = 3), fourth root (n = 4), or fifth root (n = 5). The formulas for the nth root calculations is written below. the kth root of A is given by this iterative formula: xn+1 = [(k-1)*xn + A/xn (k-1)] / k Use A as your first (initial) value for the root, x0. Output the designated root and return to the main menu. (If the user wants another root, they have to pick option 4 again.) Use 100 iterations for the computation. When option #5 is chosen, you are to compute the hyperbolic sine of a value, sinh(x). Here’s the formula: sinh(x) = (ex – e -x )/2 You can compute this using the exponential function from option 2. Thoughts: For this assignment, you are to use the switch-case statement to branch to the various choices for the first menu. You are forbidden to use (which is the same as ) for this assignment! You should double for a computational type. The numbers for these calculations can get big, so you will need precision on your side. Don’t make a do_everything function. Some people do this: int main() { do_everything(); return 0; } This is nothing more than renaming main; it accomplishes nothing. Leave something in main….perhaps your switch. When you submit: As usual, when you submit, you are all to enter the same information so as not to drive the graders crazy. Following these steps: option 1 x = 5 option 3 x = 1 using 8 terms option 4 x = 60 find the fifth root option 4 again for x = 729 find the 3rd root option 5 x = 2 option 2 x = 4 quit And, as always, let your TAs or instructor know if you need any help.
Background: After weeks of stress, Hans Moleman is taking a vacation to
Atlantis. But now, the Atlantians (those wierdos living in Atlantis) have made
it so that he can’t leave to go back to home unless he jumps through the right
hoops. Those “hoops” are that he first visits with enough of the inhabitants
(weirdos) to satisfy the Atlantis Visitors’ Bureau. And to prove that he has
done so, he has to collect the business cards of each one he visits and show the
“gate keepers” as he leaves. There’s just one problem …… Hans Moleman is
his own worst enemy in that he has a business card fetish: he won’t carry in
his hands multiple cards unless he can stack them in such a way that no card
is smaller than the card below it1
. Now, he can orient the stacked cards any way he wants, but a
card can’t “hang over” (or be the same size as) the card above it. This is stupid, but oh well! It’s
part of the reason why Hans Moleman is an interesting character. Fortunately for Hans Moleman,
there’s an out: IF he visits enough different inhabitants, then he can get out of Atlantis regardless
of how the cards are stacked.
Specifications: Your program is to simulate this situation. Hans Moleman will visit at
random some of the various “creatures” (inhabitants) of Atlantis, whereupon each of the creatures
will say something chosen at random from a list of comments (below), and then give him their
card which he will put in his pocket. Each creature has a card specific to them. What is important
about their card is its dimensions: length and width (assume these are integers). Each time he gets
a card, he needs to assess whether or not he can leave Atlantis (like really! No good hay-bars…only
sushi restaurants.) When he confirms he has satisfied the Rules of Leavature, he needs to output
1 As an example: card1 is 4 x 8, card2 is 2 x 9. These can NOT satisfy Hans Moleman’s criteria since there is no
way to orient (at right angles) these two cards without one hanging over the other. As another example: card1 is 2 x
3 and card2 is 1 x 2. These two CAN satisfy Hans Moleman’s criteria since card2 can be stacked below card1 in a
way that it will not hang over the top card since 1 < 2 and 2 < 3. So, you see, you must compare the cards in both
orientations. Both dimensions of a lower card must not exceed either dimensions of an upper card, and at least one
dimension of an upper card must be strictly greater than at least one dimension of a lower card. (Another ex: a 1 x 1
card can be below a 1 x 2 card)
an exclamation that he is leaving (we’ll leave the wording up to you – make it clean) and returning
home, and state with detail what gives him the right to leave. Leavature Rules are specified
below. And what details are we expecting? Either output the names of the creatures’ cards and
their dimension of the 6 stackable cards1
, or output the 14 different names of the 14 different
creatures he got cards from.
Details:
You are expected to use a struct for a creature that includes its name and the dimensions
of its card.
You are expected to use arrays in this assignment as appropriate. For example, you could
set up a const string array loaded with the sayings that the creatures can speak. Think
about how you could randomly choose an entry from that array.
You can assume that our hero Hans will collect no more than 150 cards. (Hmmmm, is
that another hint about an appropriate use of arrays???)
Leavature Rules (a.k.a. Rules of Leavature): Hans can leave if he has 6 (six)
“stackable”1
cards (like, you know, n’stuff, non-overlapping); OR he has cards from
14 different creatures (implies 11 tunas’ cards and 3 sardines’ cards ain’t gonna do it!)
In your code, generate a random creature before you generate a random saying.
If you end up stacking cards that you had repeats of (e.g. you stack a 2 x 3 and you got
two of these cards from two different creatures), we care not which creature’s card you
use.
Each creature has many cards. So, each time it is visited, it can hand a card to Hans
Here’s the data to use (creature and their card specs):
o Wanda Walrus 2 x 6
o Stanley Sardine 3 x 1
o Sylvia Seahorse 4 x 2
o Janie Jellyfish 1 x 10
o Doris Dolphin 8 x 4
o Bob Blobfish 1 x 5
o Sammy Shark 8 x 4
o Walter Whale 6 x 2
o Stevie Salmon 2 x 3
o Sheila Shellfish 1 x 3
o Daniel Octopus 3 x 7
o Mark Herrings 9 x 5
o Bernie Tuna 3 x 5
o Oscar Tilapia 5 x 3
Here’s a list of the things they can say:
o Whazzup?
o Duuude, totally love the horse head and human body combo!”
o Looking for a card (shark)?
o Are you someone famous?
o You look fishy to me; or are you horsing around?
o Don’t trust the salmon!!!
o Here’s some advice: the shark wants you to be his chum!
o Better take some paper towels if you visit the blobfish
o Hey man, got any sea-weed?
o Let me give you my card…my phone number is on the back
o I’d gladly pay you Tuesday for a lobster roll today.
o Don’t be so crabby!
o Just keep swimming, swimmin, swimming, …
o You just keep sinking, sinking, sinking,…
Special Underwater Observation: You will find your coding easier if you were to sort your
cards when gathering them. Think carefully how this has to be done. You wouldn’t want
to overload your day with an overload of work when overloading your brain while coding…
When you submit: seed your random number generator with 37. Please understand that we
specify the seed for submission on the hopes that your output will be the same as everyone
else. However, because of slight differences in how you all code, this may not work out as
planned. But, we’re hoping that it pays off to some significant degree.
And, as always, let your TAs or instructor know if you need any help.

![[SOLVED] Cs 410: homeworks 1 to 6 solution](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[SOLVED] Ece472 assignments 1 to 4 solution](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.