{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 2 Notes

R1 b d2a r2 b d2a a inputenter a

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: t & Output fprintf(‘message to print ’) 25 Lecture 2 The input Command Input & Output n༆  26 variable = input(‘prompt ’) Variable Name r= input(‘Enter radius: ’) = input(‘ Message’); n༆  fprintf(‘message to print ’) where to put the value fprintf(‘Increase ’) fprintf(‘is %f inches\n’, x) fprintf(‘Position (%d,%d)\n’, x,y) a prompt message in quotes Processed after the user hits the <enter> key. Lecture 2 27 Insight Through Computing 8 1/28/14 fprintf! fprintf Used to format output. Example: X = 1.23456789; fprintf(‘x = %5.2f\n’,x)) Possible output formats* fixed point (or floating point) decimal—whole number exponential general—Matlab chooses a format character string %f %d %e %g %c %s Output line will look like x= 1.23 The \n generates a carriage return Examples: %f %15.2f *Use doc fprintf, see formatSpec 28 Insight Through Computing Quad1 w/user input Quad2 w/formatted output % Quad2 % Solves ax^2 + bx + c = 0 % Assumes real roots. r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) a = input('Enter a: '); b = input('Enter b: '); c = input('Enter c: '); d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) r1 = (-b - d)/(2*a); r2 = (-b + d)/(2*a); disp(' ') fprintf('Root1 = %10.6f\n',r1)) fprintf('Root2 = %10.6f',r2)) Insight Through Computing Insight Through Computing Script, function and program operation Introduction to Conditionals computation input Insight Through Computing Boolean expressions The If- Else Construct and, or, not output Insight Through Computing 9 1/28/14 So Far n༆  n༆  All the statements in our scripts are executed in order We want to make computation contingent upon various things. We do not have a way to specify that some statements should be executed only under some condition Lecture 3 E.g., if the value of the arithmetic expression Dice1 + Dice2 is seven, then increase the value of the variable GamesWon by one. 55 Insight Through Computing The If- Else Construct Solves this Problem Assume Variables b,c,L,R
 are Initialized We will introduce this language feature by solving problems about the behavior of a given quadratic q( x) = x 2 + bx + c on a given interval L <= x <= R. E.g., Insight Through Computing b c L R = = = = input(‘Enter input(‘Enter input(‘Enter input(‘Enter b’:) c’:) L’:) R’:) Insight Through Computing The Situation Problem 1 xc = −b / 2 2 q( x) = x + bx + c Write a fragment that prints “yes” if q(x) increases across the interval and “no” if it does not. Insight Through Computing L R Insight Through Computing 10 1/28/14 No! Yes! xc = −b / 2 q( x) = x 2 + bx + c xc = −b / 2 q( x) = x 2 + bx + c Requir...
View Full Document

{[ snackBarMessage ]}