EEC 243 Introduction to Programming – MATLAB
Lab 5: Loop Statements and Vectorizing Code
- Execute this script and be amazed by the results!You can try more points to get a clearer picture, but it may take a while to run.
- It is good programming style to always use a for loop when the number of times to repeat the action is known.The following code uses a while loop that mimics what a for loop should do.Re-write it using a for loop that accomplishes exactly the same thing.
- Write a script that will prompt the user for a quiz grade and error-check until the user enters a valid quiz grade.The script will then echo print the grade.For this case, valid grades are in the range from 0 to 10 in steps of 0.5.Do this by creating a vector of valid grades and then use any or all in the condition in the while loop.
- Write a script beautyofmath that produces the following output.The script should iterate from 1 to 9 to produce the expressions on the left, perform the specified operation to get the results shown on the right, and print exactly in the format shown here.
- (Optional) Write a â€œGuess My Number Gameâ€ program.The program generates a random integer in a specified range, and the user (the player) has to guess the number.The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again.
x = rand;
y = rand;
for it = 1:10000
choic = round(rand*2);
if choic == 0
x = x/2;
y = y/2;
elseif choic == 1
x = (x+1)/2;
y = y/2;
x = (x+0.5)/2;
y = (y+1)/2;
myprod = 1;
i = 1;
while i <= 4
num = input(‘Enter a number: ‘);
myprod = myprod * num;
i = i + 1;
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
The basic algorithm is:
- The program starts by printing instructions on the screen.
- For every game:
- After all games have been played, print a summary showing the average number of guesses.
- the program generates a new random integer in the range from MIN to MAX.Treat MIN and MAX like constants; start by initializing them to 1 and 100
- loop to prompt the player for a guess until the player correctly guesses the integer
- for each guess, the program prints whether the playerâ€™s guess was too low, too high, or correct
- at the conclusion (when the integer has been guessed):
- print the total number of guesses for that game
- print a message regarding how well the player did in that game (e.g the player took way too long to guess the number, the player was awesome, etc.).To do this, you will have to decide on ranges for your messages and give a rationale for your decision in a comment in the program.
- Prompt the user for an integer n and print â€œMATLAB rocks!â€ n times.
- Given the following loop:
- Trace this to determine the output, and then enter it to verify your answer.
- Write code that will prompt the user for 5 numbers, and store them in a vector.Make sure that you preallocate the vector!
- Write a function myones that will receive two input arguments n and m and will return an n by m matrix of all ones.Do NOT use any built-in functions (so, yes, the code will be inefficient).
while x < 10
- For what values of the variable x would the action of the loop be skipped entirely?
- If the variable x is initialized to have the value of 5 before the loop, what would the action have to include in order for this to not be an infinite loop?
for i = 2:2:6
fprintf(‘%d: ‘, i)
for j = i:-1:1