DOC PREVIEW
UIUC CS 101 - lect03

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Lecture 33-2What are relational operators and logical values?How to use the input and disp functions.Learn to use if, if-else and else-if conditional statements.What are logical operators?Using while to perform loops.Readings: Matlab by Pratap Chapters 3.2.2, 3.2.3, 4.3.4, 4.3.5 What will I learn in this lecture?3-3Logical values1. Problem Definition Write a Matlab function named count_axles to compute the number of 4 axle vehicles traveling past a specific location on a road over a fixed time interval.2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)The input to count_axles will be a vector containing the number of axles for each vehicle that passes a specific location. A second input will be the number of axels for which you want to obtain a count. In our example this value is 4.3-4Logical values3. Develop Algorithm (processing steps to solve problem) We first identify each vehicle with the desired number of axles. For example if, >> data = [2 3 4 4 2 4 4]; then the vector identifying the 4 axle vehicles is, match = [0 0 1 1 0 1 1] Use the relational operator == to find matches >> match = data == 4 match =[0 0 1 1 0 1 1] Next, use the sum function to add all the 1’s. Note that match is a logical valued vector >> whos matchName Size Bytes Class match 1x7 7 logical3-54. Write the “Function" (Code)function count = count_axles(data, target)% function count = count_axles(data, target) match = data == target ; count = sum( match);Logical values3-6Relational OperatorsThe relational operators are: < <= > >= == ~= Relational operators generate logical values. >> match = [2 3 4 4 2 4 4] ~= 4 match = 1 1 0 0 1 0 03-7Subscripting with LogicalsExample: Extract the bad data from the data vector. Suppose negative values are bad data.>> data = [2 -5 3 4 -1 4 2 4 4 -9];>> match = data > 0 match = 1 0 1 1 0 1 1 1 1 0 >> data = data(match) data = 2 3 4 4 2 4 43-8Subscripting with LogicalsProblems subscripting with logicals. Rather than using a relational operator to generate a logical array we can use the logical function. >> data = [2 -5 3 4 -1 4 2 4 4 -9];>> match = logical([1 0 1 1 0 1 1 1 1 0]); >> data = data(match) data = 2 3 4 4 2 4 4 Note that the following does NOT work!>> match = [1 0 1 1 0 1 1 1 1 0]; >> data = data(match) ??? Subscript indices must either be real positive integers or logicals.3-9Guessing Game-11. Problem Definition Write a Matlab function named “guess” that plays a guessing game with the user. The user will guess an integer number between 1 and x where x is given by the user as an input argument to the function.2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)This function has one input parameter x. Using a loop the function gets successive inputs from the user and compares to the correct answer. At each guess the user is told whether the guess is correct or not.3-103. Develop Algorithm (processing steps to solve problem) To generate a random number between 1 and x that the user must guess we use the Matlab rand function. For example (from lab 2) we learned about the use of the ceil function, so that if you typed, >> x = 10; >> correct_answer = ceil(rand()*x) then the correct_answer will have a randomly chosen value between 1 and x. We can use the Matlab input function to prompt the user for a guess and to read the guess from the keyboard. For example, you can type,>> guess = input(‘Enter your guess: ‘); Guessing Game-13-113. Develop Algorithm (processing steps to solve problem) To check whether the user has entered a correct value we can use the Matlab if-else statement as follows: if correct_answer == guess disp(‘Match!!!’); else disp(‘No Match!’); end Guessing Game-13-123. Develop Algorithm (processing steps to solve problem) To repeat one or more Matlab statements ( a loop) we can use the while statement as follows: while flag == 1 % one or more Matlab statements here end where the flag variable keeps track of whether the game is over (flag == 0) or is still in progress (flag = 1). Guessing Game-13-13obtain next guesststart functiontend functioniflag == 1?icorrect guess?generate random number and set flag = 1display MATCHset flag = 0display NO MATCHTRUEFALSETRUEFALSE3-144. Write the “Function" (Code)function guess(x)% x is an integer%generate random integer between 1 and xcorrect_answer = ceil(rand()*x); flag = 1; % why??? while flag == 1 guess = input(‘Enter your guess: ‘); if correct_answer == guess disp(‘Match!!!’); flag = 0; else disp(‘No Match!’); end end Guessing Game-1Loop3-15Guessing Game-21. Problem Definition Modify the guessing game function by giving the user feedback if their guess is higher or lower than the correct answer.2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.)We will use a variation of the if-else statement which we call the elseif statement.3-163. Develop Algorithm (processing steps to solve problem) To check whether the user has entered a correct value we can use the Matlab else-if statement as follows: if correct_answer < guess disp(‘Answer is too high!’); elseif correct_answer > guess disp(‘Answer is too low!’); else disp(‘Match!!!’); flag = 0; end Guessing Game-23-174. Write the “Function" (Code)function guess(x)% x is an integer%generate random integer between 1 and xcorrect_answer = ceil(rand()*x);flag = 1; while flag == 1 guess = input(‘Enter your guess: ‘); if correct_answer < guess disp(‘Answer is too high!’); elseif correct_answer > guess disp(‘Answer is too low!’); else disp(‘Match!!!’); flag = 0; endend Guessing Game-2else-if3-18Guessing Game-31. Problem Definition Modify the guessing game function


View Full Document

UIUC CS 101 - lect03

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect03
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect03 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect03 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?