Sei sulla pagina 1di 2

ME 318M Programming and Engineering Computational

Methods
Homework # 2

Assigned: September 4
th
, 2014
Due: September 11
th
, 2014


Name: _____________________ Section Unique Number:___________________

UID: _________________

Please enclose relevant printouts of the code you wrote and MATLABs command window
in which you executed it.


Problem 1:
Ask the user to enter his/her age, with the prompt: Please tell me how old you are? -> Then,
output a string: You just told me that you are <whatever the user entered> years old. (Instead of
<whatever the user entered>, you should put whatever the user entered as his/her age).
Demonstrate that this works with a printout of your workspace.

Problem 2:
Write a program that will do the following.
Ask the user to enter his/her age, with the prompt: How old are you?
Then, if the age of the user is above zero and less than 7, output to the screen: Oh thats so
cute!
If the age of the user is more or equal 7 and less than 23, then write: What school do you go
to?
If the age of the user is more or equal to 23 and less than 65, then write: Do you work?
Finally, if the user enters anything equal to or above 65, write: Are you retired yet?
Demonstrate that this works by entering several (at least 2) possible ages (numbers above zero)
and printing the workspace so that we see how the computer responds.
Then enter a negative number as the age and tell us what happens (or doesnt happen). Finally,
enter 1000000 (the person is 1 million years old). Tell us what happens.

Problem 3:
Modify your code from Problem 2 so that ages that are not logical are taken care of.
If someone enters a negative number, write: Thats impossible.
And if the user enters anything above 110, write: Thats highly improbable.




Problem 4 (a bit tough but you can do it):
Begin just like problem 1. I.e., ask the user to enter his/her age, with the prompt: Please tell me
how old you are? -> Then, prompt the user to input a string using a prompt: You just told me
that you are <whatever the user entered> years old. Is that correct y/n ->. If the user enters y or
yes, display Excellent, so you are <whatever the user entered> years old! If the user enters n
or no, display Darn, next time answer correctly! If anything else is entered, display What? I
dont get it!
Hint: use strcmp command to compare strings, as well as logical operations and (&) and or (||).

Problem 5:
Create five variables in the workspace X1, X2, X3, X4 and X5 assign values of your choice
to these variables, and save the three variables as Problem5.mat.
Create a program that will load the file Problem3.mat, and will calculate the mean and the
sum of the variables X1, X2, X3, X4 and X5 in that file.
Display the mean and the sum of the variables X1, X2, X3, X4 and X5 with messages The
mean of these numbers is <what_ever_it_is>. and in the next line (using another disp
command). The sum of these numbers is <what_ever_it_is>.

Potrebbero piacerti anche