>> y1 = input('Please enter a number'); >> y2 = input('Please enter a letter'); >> y3 = input('Please enter a letter','s'); >> y4 = input('Please enter a number','s'); Demo: input statements Input: 9 a 9
>> num = input('Please enter a number: '); >> name = input('Please enter your name: ','s'); >> fprintf('The number you''ve entered is %i',num); >> fprintf('The number you''ve entered is %i\n',num); >> fprintf('The number you''ve entered is %f\n',num); >> fprintf('The number you''ve entered is %0.2f\n',num); >> fprintf('The number you''ve entered is %8.2f\n',num); >> fprintf('Your name is %i\n',name); >> fprintf('Your name is %s\n',name); Demo: fprintf statements
Using input statements Problem 1: Write a script that will allow the user to enter the three coefficients of a quadratic expression (y = ax 2 + bx + c) and computes the roots of the polynomial. Problem 2: A metric ton is 35, ounces. Write a script that will allow the user to enter the weight of a box of cereal (in ounces) and compute the weight of the box of cereal in tons as well as how many boxes are needed to make one metric ton of cereal.
Using input statements Problem 3: You are the newest employee at VISA (Vending Innovations for Snacking Acquisition), and you have been put in charge of writing the new control system for the next wave of vending machines. Currently, the top-of-the-line model of vending machines produced by VISA only gives change in pennies. Before you implement your system on an actual vending machine, you need to do some testing. Write a script that will allow the user to enter the cost of the item and the amount of money entered and returns the least number of individual pieces of currency as change. The vending machine can return dollar bills, quarters, dimes, nickels, and pennies.
Write a script that will allow the user to select from a set of different dimensions for standard shipping boxes and return the volume and girth of each box. Menu Statements Dimensions: 1)L: 12” W: 12”H: 5.5” 2)L: 5.375” W: 8.675”H: 1.675” 3)L: 11” W: 8.5”H: 5.5” 4)L: ” W: 3.375”H: ” L W H girth
fprintf Statements Using fprintf statements, write a script that will display the lengths, widths, heights, volumes and girths for all 4 box options in a table format. Example Results: Length (in)Width (in)Height (in)Volume (in^3)Girth (in)
Useful String Functions When working with strings, there are two useful functions that can allow you to switch back and forth between numbers and strings: num2str(number) – converts a number from type double to the equivalent string of numbers str2double('string') – converts a string of number characters into a double value
Creating Strings Figure out 2 different ways to display the following statement: My name is X and I am Y years old. where X is a name entered by the user (using an input statement) and Y is the age entered by the user (using an input statement)