## Pre-Lab 5 CS 122L – 5 Points Total Objectives ● Learn how to use the input function to accept string arguments ● Learn how to use if/elseif/else (logical) statements ● Learn how to use pseudo-code to develop a simple algorithm Deliverables ● Submit your pre-lab answers in Bblearn under the Lab 5 pre-lab assignment area prior to the start of class. 1) As you have learned, you can use the input function to accept numbers. For example, the following code accepts a number and stores it in the variable named coin: coin = input(‘Enter a coin’); This works great if we want to enter a numeric value (like 25), but what if we wanted to enter a string, or word, like ‘quarter’? Well, input can handle that as well, but there is a bit of extra syntax. In order to allow the user to enter strings, we change the previous statement to: coin = input(‘Enter a coin’,’s’) Notice that extra ‘s’? It is an extra argument that we have placed in the input function to account for the user typing in a string. You can also think of it as the flag that allows input to accept strings. It is worth noting that when the ‘s’ argument is provided, then entering a numeric value will most likely not produce the result you are expecting. a) Based on the above discussion, write the MATLAB code to ask the user to enter a coin (‘q’ for quarter, ‘d’ for dime, ‘n’ for nickel, ‘p’ for penny). 2) If/else statements allow programmers to provide logic to programs. If we wanted to compare two numbers, x and y, we could write the following code: if x < y; %if x is less than y, then we do something end We can also have multiple branches of statements: if x < y; % if x is less than y, then we do something elseif y < x; % else, if y is less than x, then we do someting else %finally, if none of the above statements are true, we default to this end %Notice only one end for the whole set of if/else/if/else branches above We can also compare single characters, like ‘A’ or ‘b’ (the code to compare whole words is a bit more complicated, so we can keep it simple for now). For example, we could write the following: if x == ‘A’; % if the character in x equals ‘A’, then do something end Don’t forget to always put an ‘end’ after your set of logical statements, and that you only need one ‘end’ for a single series of branches! a) Write the MATLAB code that compares if x is greater than or equal to 25. b) Write the MATLAB code to check if a variable, named coin, equals ‘q’,’d’,’n’,or ‘p’. If it doesn’t equal any of those, then have a default else statement that displays ‘none of the above’. 3) Pseudo-code is where you write out the correct English-like statements but are not worrying about the MATLAB syntax being exactly correct. For example, a file called setPrice.m in MATLAB might look like this in pseudo-code: Input price if (cents < 0) print "Illegal price" else price = cents print "price set to" + price + "cents" Notice how much of this pseudo-code would not work if plugged into MATLAB. This is fine! The whole idea is to create a very basic outline of the code, and you can worry about the syntax when you implement the function. a) Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies. Write the pseudo-code to do the following: ● Initialize counter variables for quarters, dimes, nickels and pennies to 0. Counter variables are used to keep track of how many of each coin are to be returned. ● Calculate the amount of change to be given using the ‘total’ and ‘cost’ variables. ● While there is still change to be given, find the largest coin that can be returned, increment the coin’s counter, and subtract the coin value from the amount of change to be given. ● Display the total change, as well as how many of each coin are being returned.

Pre-Lab 5 CS 122L – 5 Points Total Objectives ● … Read More...