Tutorial - Loops and Controls PDF
Tutorial - Loops and Controls PDF
Chikava F.K.
Department of Chemical and Processing Engineering
Question 1
Create a program that first prompts the user to enter a value for x and then prompts the user
to enter a value for y. If the value of x is greater than the value of y, send a message to the
command window telling the user that x > y. If is less than or equal to y, send a message to
the command window telling the user that y >=x.
Question 2
Create a program that prompts the user to enter a scalar value for the outside air
temperature. If the temperature is equal to or above 25 degrees, send a message to the
command window telling the user to wear shorts. If the temperature is between 20 and 25
degrees send a message to the command window telling the user that it is a beautiful day. If
the temperature is below 20 degrees then tell the user to wear a jacket.
Question 3
Question 4
Write a MATLAB function d = dsc(c) that takes a one-dimensional array of numbers c and
returns an array d consisting of all numbers in the array c with all neighbouring duplicated
numbers being removed. For instance, if c = [1 2 2 2 3 1], then d = [1 2 3 1].
Question 5
Write a Matlab script that calculates a user’s total mobile telephony costs at the end of a
month for a flexible post-paid billing system. Assume that the user’s service usage information
is stored somewhere and is retrievable by the user; and your script will calculate the total cost
depending on his/her input.
Question 6
Use the following algorithm for the calculation:
1. Ask the user if he/she is on a monthly post-paid billing system. If the response is ‘yes’ then
proceed otherwise end the program with the message: “This program is for only post-paid
clients”.
2. Request the user to put in the number of peak and off peak minute calls he/she made for
the month. Use R0.06/second and R0.015/second as rates for peak and off peak call charges
respectively.
2.1 Ask the user if he/she bought any SMS bundles this month. If the response is ‘yes,’ then,
2.2 Use the menu function to ask the user the type of bundle he/she purchased. Then use a
switch case statement to assign the cost per bundle according the rates in Table 1. YOU MUST
USE THE MENU FUNCTION AND THE SWITCH CASE STATEMENT FOR THIS STEP.