REC09+Problems+Set+Solutions

# REC09+Problems+Set+Solutions - 14:440:127 Introduction to...

This preview shows pages 1–4. Sign up to view the full content.

14:440:127 Introduction to Computers for Engineers Recitation 9 - Problems for students to attempt - SOLUTIONS 1. Write a switch/case statement that displays the following menu for Taco Bell, allows the user to select one item, and then displays the amount of that item: Soft Taco \$0.69 Chicken Soft Taco \$1.99 Nachos Supreme \$2.09 Fruitista Freeze \$1.50 f = menu('Welcome.', 'Soft Taco', 'Chx Soft Taco', 'Nachos Supreme', 'Fruitista Freeze'); switch f case 1 disp('0.69'); case 2 disp('1.99') case 3 disp('2.09') case 4 disp('1.50') end 2. Edit the menu above to include a ±fth option: “done.” Each time the user selects an option, the menu should display again until the user chooses “done.” Then, display a list of what they ordered (and quantities), as well as the total amount of their purchase. Hint: use a while(true_condition) loop, which is an in±nite loop, and then have case 5 of your switch statement be a “break” statement, you will also need to create some variable to hold on to each item as it is being added. t = 0; f = menu('Welcome.', 'Soft Taco', 'Chx Soft Taco', 'Nachos Supreme', 'Fruitista Freeze','Done'); while(f<5) switch f case 1 t = t+0.69; case 2 t = t+1.99; case 3 t = t+2.09; case 4 t = t+1.50; case 5 break;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
end f = menu('Welcome.', 'Soft Taco', 'Chx Soft Taco', 'Nachos Supreme', 'FruitistaFreeze','Done'); end fprintf('Total: %.2f \n', t); 3. Write a GUI that has two editable text boxes and a button that says “Multiply”. When the button is clicked, the numbers in the two text boxes should be multiplied together, and the result should be displayed somewhere on the GUI (you choose). The only function you should change is the add_pushbutton_Callback in the lecture example, but of course, you need to also change the tag of this item. function mult_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to add_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a = get(handles.input1_editText, 'String' ); b = get(handles.input2_editText, 'String' ); % a and b are variables of Strings type, and need to be converted % to variables of Number type before they can be added together total = str2num(a) * str2num(b); c = num2str(total); % need to convert the answer back into String type to display it set(handles.answer_staticText, 'String' ,c); guidata(hObject, handles); 4. Congratulations, you just had a baby! Create a cell array of baby names. Then, randomly choose an integer between 1 and the number of baby names in that cell array, and display the corresponding baby name using an fprintf statement: “The baby will be called: Bob!” names = {'Lucas', 'Jack', 'Meghan', 'Danielle', 'Johnny', 'Alexandra', 'Soulja Boy'}; chosen = names{ceil(length(names)*rand(1))}; fprintf('The baby will be: %s \n', chosen) % OR using randi for random integer, % i.e. randi(3) results in random number from 1 to 3 chosen = names{randi(length(names))}; % need {} since it is cell fprintf(‘The baby will be named: %s \n’,chosen)
5. Write MatLab code that performs the basic tasks of an ATM machine.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/24/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.

### Page1 / 10

REC09+Problems+Set+Solutions - 14:440:127 Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online