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

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Introduction to C Programming
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Basic Elements of C++ Chapter 2.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Input, Output, and Processing
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Introduction to Programming with RAPTOR
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Introduction to Pascal The Basics of Program writing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
INPUT / OUTPUT STATEMENTS
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Pascal Programming Today Chapter 11 1 Chapter 11.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Pascal Programming Today Chapter 2 1 Chapter 2. Pascal Programming Today Chapter 2 2 »Output statements write data to output devices (e.g. VDU). »Two.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
INPUT & OUTPUT scanf & printf.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
DATA TYPES AND OPERATIONS
Presentation transcript:

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: 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.

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.

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.

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

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

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

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.

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.

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.

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.

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);

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

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.

Formatting Integer Output  Formatting Integer Values – b stands for blank space ValueFormatPrinted Output 234:4b234 :5bb234 :1234 :Lenbb234 (if Len=5) -234:4-234 :5b-234 :1-234

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: :

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.

Formatting Real Output Examples: b stands for blank space ValueFormatPrinted Output :5:2b3.14 :4:23.14 :3:23.14 :5:1bb3.1 :8:5b :93.142E :9-3.14E :4:

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.

Formatting Real Output Eliminating Leading Blanks: –Choose a format that will display the smallest value expected without leading blanks.. –Examples: :1is displayed as :3:1is displayed as –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.

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: b stands for blank space StringFormatPrinted Value '*':1* :2b* 'ACES':1ACES :2ACES :3ACES :4ACES :5bACES

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

Self-Check Show how the value (stored in X) would be displayed using the following formats X :8:4 X:8:3X:8:1X:8:0X:8 Assuming X = (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);