Select Page

Your Perfect Assignment is Just a Click Away

Starting at \$8.00 per Page

100% Original, Plagiarism Free, Customized to Your instructions!

# 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.

clear

clf

x = rand;

y = rand;

plot(x,y)

hold on

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;

else

x = (x+0.5)/2;

y = (y+1)/2;

end

plot(x,y,’r*’)

hold on

end

myprod = 1;

i = 1;

while i <= 4

num = input(‘Enter a number: ‘);

myprod = myprod * num;

i = i + 1;

end

>> beautyofmath

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.

Assignment 5

• 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

action

end

• 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

fprintf(‘*’)

end

fprintf(‘n’)

end

myones.m

"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you A results."

## Our Service Charter

1. Professional & Expert Writers: Eminence Papers only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.

2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.

3. Plagiarism-Free Papers: All papers provided by Eminence Papers are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.

4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Eminence Papers are known for the timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.

5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.

6. 24/7 Customer Support: At Eminence Papers, we have put in place a team of experts who answer all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.