Exercises Loops
Exercises Loops
Problem 1: How many times will the for loop in the following code run and what will the
output be? Work this out by hand and just use MATLAB to check your answer.
x = 2; sum = 1;
for k = 1:5
sum = 1 + 1/x*sum;
end
disp('sum =');disp(sum)
Problem 2: What will the following code produce? Work this out by hand then check result in
MATLAB
% Geometric Series
x = 4; N = 5;
series = 0;
for m = 1:N
series = series + x^m;
end
fprintf('The sum for the geometric series with x = %0.5f and %d
terms is: %0.5f \n',x,N,series)
Problem 3: How many times will the while loop in the following code run and what will the
ouput be? Again, work this out by hand and just use MATLAB to check your answer.
sum = 0;
while sum <=10
sum = sum + 3;
end
disp('sum =');disp(sum)
Problem 4: The wind chill factor (WCF) describes how cold it “feels” for a given temperature
T, in Fahrenheit, and a given wind speed V (in miles per hour). The equation for wind chill
factor is:
Write a script that has three inputs: temp in Fahrenheit, minimum wind speed, and maximum
wind speed. Use a for loop to compute and display the wind chill factor (WCF) using the given
temperature over the wind speed range in increments of 5 miles per hour. For example, suppose
the user inputs a temperature of 20o, a minimum wind speed of 5 mph., and a maximum wind
speed of 20 mph. The output of the program should look like this (that is, use fprintf to insert
your calculated values into the text):
Problem 5: Consider the following simple savings plan. On Day 1 you put aside one penny.
On Day 2, you put aside two pennies. On Day 3 you put aside three pennies. You continue this
simple savings plan for several years.
How much money do you think you will have in 30 years? Make a guess: ________________
Write a script that will compute the amount of money accumulated for a specified number of
years. The input to your program will be number of years. The outputs of your program will be
the amount of money saved (in dollars, not pennies) and the amount of money contributed on the
very last day of your savings plan (in dollars, not pennies).
Note: Don’t worry about leap years – just assume 365 days per year.
Problem 6: Write a MATLAB script that will allow a user to play the dice game: Under and
Over Seven. The rules are pretty simple:
Your program should begin by asking the player the total amount of money that he/she
has to play this game. Throughout the game, your function will keep track of how much
money the player has left based on wins and losses.
The player should then be asked to place his/her bet (Over 7, Under 7, Exactly 7) and the
amount of money for the bet. If the player tries to bet more than he/she has, prompt for a
new bet amount.
Roll the two dice and display the results (the number on each dice and win or lose). Use
the randi function to do this.
Calculate and display to the user his/her new balance (total amount of money he/she has
now).
If the balance is greater than zero, ask the user if he/she would like to play again. If the
user says answers yes, prompt for a bet and a betting amount and run through the cycle
again.
The game (program) should end when the user has no money left to bet or the user
decides to quit.