1 / 24

CMP 131 Introduction to Computer Programming

CMP 131 Introduction to Computer Programming. Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday). TODAY. Input/Output and Formatting Your questions on everything before the Quiz tomorrow. Input/Output ( Read / Write ) Procedures. Definitions: Input Operation:

bert-davis
Download Presentation

CMP 131 Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMP 131Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)

  2. TODAY • Input/Output and Formatting • Your questions on everything before the Quiz tomorrow

  3. Input/Output (Read/Write) Procedures • Definitions: • Input Operation: • An instruction that reads data into memory. • Output Operation: • An instruction that displays information stored in memory. • Procedure: • A piece of code that is invoked from within the program and returns control to the program when finished • I/O Procedure: • Pascal procedure that performs I/O operation. • Procedure Call Statement: • An instruction that calls or activates a procedure.

  4. Input/Output (Read/Write) Procedures • Data can be stored in memory in three different ways: • Associated as a constant • Assigned to a variable • Read into a variable • Reading is used if the data varies each time the program executes. • Input/output procedures are supplied as part of Pascal compiler. Their names are standard identifiers.

  5. The Same Identifier has Different Meanings in Different Places • When a variable name occurs in a read/readln and a write/writeln statement, it has a different meaning: • write(MyVar) : MyVar is an expression The arguments to write are expressions (literals, constants, variables, combinations) whose value is retrieved for display • read(MyVar) : MyVar is interpreted as the address of a memory location to read into.You cannot have an expression as an argument to read • Similarly, when a variable name occurs in an assignment statement, it has a different value on the right hand side (RHS) of the := and on the left hand side of the := • On the RHS, it is (part of) an expression whose value is being retrieved • On the LHS, it is the address of a memory location where the RHS value will be stored.

  6. Reading Data … • Procedures: read, readln • Read data from standard input device “input” (e.g. keyboard) • Syntax: read/readln(inloc1, inloc2, …, inlocN)inlocX is a location where to store the data • Examples: read(SqMeters); readln (Letter1, Letter2, Letter3); read (Age, FirstInitial) • Commas separate the variable names in input list

  7. Reading Data … • Use write/writeln to display a prompt message when the user is required to enter data • Otherwise the program is waiting and you don’t know why or what to enter. • The order of data enter must correspond to the order of variables in the input list • Reading numeric data: • Insert one or more blanks between numeric data items • Reading character or string data: • Don't insert any blanks between consecutive data items unless the blank character is one of the data items being read

  8. Read vs. Readline Procedure • Read: • Will read from input just as much as it needs for its arguments and leave the rest to be consumed later. • Extra characters on the data line after a read operation are not read until the next read or readln operation • Readln • Will read from input just as much as it needs for its arguments • It waits for a Return/Enter key character • Extra characters on the data line between what is needed and the end of line will be ignored after ReadLn is finished

  9. PROGRAM ReadTest; VAR x,y: integer; BEGIN write('Type 2 integers separated by a space > '); read(x); writeln; writeln('Read x ',x); read(y); writeln; writeln('Read y ',y); writeln('Type ENTER to exit program.'); readln; END.

  10. PROGRAM ReadLnTest; VAR x,y,z: integer; BEGIN writeln('Type 2 or more integers separated by a space.'); writeln('Terminate your input by pressing ENTER.'); readln(x,y); writeln('Read x ',x,' and y ',y); read(z); writeln('Read z ',z); END.

  11. Writing/Displaying Data • Procedures: write, writeln • Write data to standard output device “output” (e.g. terminal) • Syntax: write/writeln(expr1, expr2, …, exprN)where exprX is an expression, whose value is to be written, with optional formatting information • Examples: writeln (Letter1, Letter2, Letter3); write (‘Age = ’, Age, ‘First Initial = ’,FirstInitial) write(SqMeters:10:2); • Commas separate the variable names in output list • Writes every argument in the output list in the order in which it is given.

  12. write vs. writeln Procedure • writeln • used to display a line of program output, including a newline at the end; cursor advances to the next line after the output is displayed. • without any output list instructs the output device to advance by a blank line • write • Cursor doesn't advance to the next line after the output is displayed.

  13. Self-Check • What is the output if the entered data are 5 7? writeln (‘Enter two integers>’); readln(M, N); M := M + 5; N := 3 + N; writeln (‘M = ‘, M); writeln (‘N = ‘, N); • What of the output of this code? write (‘My name is’); writeln (‘Doe, Jane’); writeln ; write(‘I live in ‘); write(‘Ann Arbor, MI’); writeln (‘and my zip code is ‘, 48109);

  14. No Formatting with write/writeln • Integer values: take the space they need • Real values: Most Pascal compilers use scientific notation (floating-point notation) to display real values, but you can change that with formatting directives. • Character values display as themselves, without quotes • Boolean values display as FALSE and TRUE with no quotes • String literals, the characters inside the quotes are printed, but not the quotes themselves

  15. PROGRAM TestOutputNoFormat; BEGIN writeln('An integer with no formatting : ', 45); writeln('A real with no formatting: ', 45.67); writeln('A character with no formatting: ', '#'); writeln('Boolean values, no formatting : ', false, ' and ', true); END.

  16. Formatting Integer Output • Formatting Integer Values – b stands for blank space Value Format Printed Output 234 :4 b234 :5 bb234 :1 234 :Len bb234 (if Len=5) -234 :4 -234 :5 b-234 :1 -234

  17. Formatting Integer Output • Add the symbol ‘:n’ to the integer output list item, where n specifies the number of digits to be displayed (field width). • The digits will be right-justified. • For negative numbers, the minus sign is included in the counts of digits displayed • The format specification ‘:1’ can always be used to display any integer values without leading blanks. • The field width specification may be a variable, or even an expression • The syntax for each integer item in an output list is: <integer expression>:<integer expression>

  18. PROGRAM TestOutputIntFormat; VAR Dollars, Cents : integer; BEGIN Dollars := 5; Cents := 33; writeln('Your collection was worth '); writeln(Dollars : 1, ' dollars', ' and ', Cents :1, ' cents. '); writeln(Dollars : 2, ' dollars', ' and ', Cents :2, ' cents. '); writeln(Dollars : 3, ' dollars', ' and ', Cents :3, ' cents. '); writeln(Dollars : 4, ' dollars', ' and ', Cents :4, ' cents. '); writeln(Dollars : 5, ' dollars', ' and ', Cents :5, ' cents. '); Dollars := -2; Cents := 89; writeln('but is now worth '); writeln(Dollars : 1, ' dollars', ' and ', Cents :1, ' cents. '); writeln(Dollars : 2, ' dollars', ' and ', Cents :2, ' cents. '); writeln(Dollars : 3, ' dollars', ' and ', Cents :3, ' cents. '); writeln(Dollars : 4, ' dollars', ' and ', Cents :4, ' cents. '); writeln(Dollars : 5, ' dollars', ' and ', Cents :5, ' cents. '); END.

  19. Formatting Real Output • Examples: b stands for blank space Value Format Printed Output 3.14159 :5:2 b3.14 :4:2 3.14 :3:2 3.14 :5:1 bb3.1 :8:5 b3.14159 :9 3.142E+00 -3.14159 :9 -3.14E+00 -124.3163 :4:2 -124.32

  20. Formatting Real Output • Total field width & desired number of decimal places should be indicated. • Add “:n:d“ to the integer. • n = total field width • d = number of decimal digits. • Total field width should be large enough to accommodate all digits before and after the decimal point • Pascal will use the extra space it needs anyhow so your nicely formatted data may not look so well formatted after all. • Decimal point & sign for negative numbers are included in the count. • It is possible to use the form “:n“ for real numbers. In this case, the real value is printed in scientific notation using a total of n print positions.

  21. Formatting Real Output • Eliminating Leading Blanks: • Choose a format that will display the smallest value expected without leading blanks.. • Examples: 29397 :1 is displayed as 29397 99397.567 :3:1 is displayed as 99397.6 • To display a real value without leading blanks, use format specification ‘:3:1’ for one decimal place or ‘:4:2’ for two decimal places. • If you use ‘:n’, the scientific notation will be displayed.

  22. Formatting Character & String Output • Formatting Strings: • A string value is always printed right-justified in its field. • If the field width is too small to accommodate the string value, Pascal will take the space it needs. • Examples: bstands for blank space String Format Printed Value '*' :1 * :2 b* 'ACES' :1 ACES :2 ACES :3 ACES :4 ACES :5 bACES

  23. PROGRAM TestOutputStringFormat; BEGIN writeln('ACES':1); writeln('ACES':2); writeln('ACES':3); writeln('ACES':4); writeln('ACES':5); writeln('ACES':6); END.

  24. Self-Check • Show how the value -15.564 (stored in X) would be displayed using the following formats X :8:4 X:8:3 X:8:1 X:8:0 X:8 • Assuming X = 12.345 (type Real) and I = 100 (type Integer). What will the output of the following statements look like? writeln(‘X is ‘ :10, X :6:2, ‘I is ‘ :4, I :5); write(‘I is ‘ :10, I : 1); writeln(‘X is ‘ :10, X :2:1);

More Related