Getting Started With Pascal Programming How are computer
- Slides: 39
Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with Pascal Common programming errors Useful mathematical functions in Pascal James Tam
What You Know: A Model For Creating Computer Software Specify the problem Develop a design Implement the design Maintain the design James Tam
Implement The Design Involves the writing of the program in a programming language e. g. , Pascal, Java, C, C++ etc. This program must then be translated into a machine understandable form. James Tam
Translators Convert computer programs to machine language Types 1) Interpreters • Translate the program as it's executed. 2) Compilers • Translate the program before it's executed. James Tam
Compiling Programs: Basic View Pascal program anything. p Machine language program Pascal compiler input gpc output a. out James Tam
Compiling Programs On Different Operating Systems Solaris compiler a. out (Solaris) Pascal program Windows compiler a. out (Windows) Amiga. DOS compiler a. out (Amiga. DOS) James Tam
Basic Structure Of Pascal Programs Header Program documentation Program name (input and output operations); Declarations const var : Statements begin : end. James Tam
Details Of The Parts Of A Pascal Program Headers • Program documentation - Version number, date of last modification, what does the program do etc. - Comments for the reader of the program (and not the computer) (* Marks the beginning of the documentation *) Marks the end of the documentation • Program heading - Name of program, input and/or output operations performed by the program • Example (* * Tax-It v 1. 0: This program will electronically calculate your tax return. *) program tax. It (input, output); James Tam
Details Of The Parts Of A Pascal Program (2) Declarations • List of constants and variables • More to come later in this section of notes Statements • • The instructions in the program that actually gets stuff done They tell the computer what to do as the program is running Each statement is separated by a semicolon "; " Much more to come later in the course James Tam
The Smallest Pascal Program (* * Smallest. p: * The smallest Pascal program that will still compile written by James Tam * v 1. 0. *) program smallest; begin end. Note: The name "smallest" should match the filename "smallest. p". You can find an online version of this program in the Unix file system under /home/231/examples/intro/smallest. p (the compiled version is called "smallest"). James Tam
Creating And Compiling Programs: On The Computer Science Network High level algorithm Flowchart or pseudo-code (on paper) Hand-written or hand-drawn To begin creating the Pascal program, in Unix type "XEmacs filename. p" Text editor XEmacs Pascal program anything. p (Unix file) Pascal compiler gpc To compile the program, in Unix type "gpc filename. p" Machine language program a. out (Unix file) To run the program, in Unix type ". /a. out" James Tam
Variables Set aside a location in memory Used to store information (temporary) Types: • integer – whole numbers • real – whole numbers and fractions - Can't start or end with a decimal • char – alphabetic, numeric and miscellaneous symbols • boolean – true or false values Usage: • Declaration • Accessing or assigning values to the variables James Tam
Declaring Variables Sets aside memory Memory locations addressed through the name Naming conventions • Should be meaningful • Any combination of letters, numbers or underscore (can't begin with a number and shouldn't begin with an underscore) • Can't be a reserved word e. g. , program, begin, end (see Appendix B) • Avoid using words with an existing meaning e. g. , integer, real, boolean, writeln, readln • Avoid distinguishing variable names only by case • For variable names composed of multiple words separate each word by capitalizing the first letter of each word (save for the first word) or by using an underscore. • Okay: - tax_rate - first. Name • Not Okay - 1 abc x test. msg good-day James Tam
Declaring Variables (2) Occurs in the variable declaration ("var") section i. e. , var name of first variable, name of second variable…: type of variables; e. g. , var height, weight: real; age: integer; James Tam
Accessing And Assigning Values To Variables Accessing • Can be done by referring to the name of the variable • Syntax: name • Example: num James Tam
Accessing And Assigning Values To Variables (2) Assignment • Performed via the assignment operator : = • Usage: - Destination : = Source; 1 • Example: - x : = 5; - x: = y; - interest : = principle * rate; - character : = 'a'; • Avoid assigning mixed types e. g. , var num 1: integer; num 2: real; begin num 1 = 12; Not allowed! num 2 = 12. 5; num 2 : = num 1; num 1 : = num 2; 1 The source can be any expression (constant, variable or formula) James Tam
Named Constants A memory location that is assigned a value that cannot be changed Occurs in the constant declaration ("const") section The naming conventions for choosing variable names also applies to constants but constants should be all UPPER CASE. Syntax: const NAME OF FIRST CONSTANT = value of first constant; NAME OF SECOND CONSTANT = value of second constant; etc. James Tam
Named Constants (2) Examples: const TAXRATE = 0. 25; SAMPLESIZE = 1000; YES = True; NO = False; James Tam
Purpose of Named Constants 1) Makes the program easier to understand e. g. , begin population_change : = (0. 1758 – 0. 1257) * current_population; Vs. const BIRTHRATE = 0. 1758; Magic Numbers (avoid!) DEATHRATE = 0. 1257; begin population_change : = (BIRTHRATE - DEATHRATE) * current_population; James Tam
Purpose of Named Constants 2) Makes the program easier to maintain If the constant is referred to several times throughout the program. const BIRTHRATE = 0. 1758; DEATHRATE = 0. 1257; begin BIRTHRATE DEATHRATE BIRTHRATE James Tam
Output Displaying information onscreen Done via the write and writeln statements Syntax (either write or writeln): write ('text message'); or writeln('text message'); write(name of variable or constant); or writeln (name of variable or constant); write('message', name of variable, 'message'…); or writeln('message', name of variable, 'message'…); James Tam
Output (2) Examples: var num : integer; begin num : = 10; writeln('line 1'); write('line 2 A'); writeln('line 2 B'); writeln(num); writeln('num=', num); James Tam
Formatting Output Automatic formatting of output • Field width: The computer will insert enough spaces to ensure that the information can be displayed. • Decimal places: For real numbers the data will be displayed in exponential form. Manually formatting of output: Syntax: • write or writeln (data: Field width for data: Number decimal places for data); James Tam
Formatting Output (2) Examples var num : real; begin num : = 12. 34; writeln(num); writeln(num: 5: 2); James Tam
Formatting Output (3) If the field width doesn’t match the actual size of the field • Field width too small – extra spaces will be added for numerical variables but not for other types of data. • Examples: num : = 123456; writeln(num: 3); writeln('123456': 3); • Field width too large – the data will be right justified (extra spaces will be put in front of the data). • Examples: num : = 123; writeln(num: 6); Writeln('123': 6); James Tam
Formatting Output (4) If the number of decimal places doesn’t match the actual number of decimal places. • Set number of decimal places less than the actual number of decimal places – number will be rounded up. • Example: num 1 : = 123. 4567 writeln (num 1: 6: 2); • Set number of decimal places greater than the actual number of decimal places – number will be padded with zeros. • Example: num 1 : = 123. 4567; writeln(num 1: 6: 6); James Tam
Formatting Output: A Larger Example For the complete program and executable look under /home/231/examples/intro/out 1. p (out 1 for the compiled version) program out 1; var num 1 : integer; num 2 : real; begin num 1 : = 123; num 2 : = 123. 456; writeln('Auto formatted by Pascal', num 1, num 2); writeln('Manual format': 13, num 1: 3, num 2: 7: 3); writeln('Manual not enough': 13, num 1: 2, num 2: 6: 3); writeln('Manual too much': 16, num 1: 4, num 2: 8: 4); end. James Tam
Input The computer program getting information from the user Done via the read and readln statements Syntax: (single input) read (name of variable); or readln (name of variable); (multiple inputs) read (nv 1, nv 2…); or readln (nv 2, nv 3…); James Tam
Input (2) Examples: var num 1, num 2 : integer begin read (num 1); read (num 2); read (num 1, num 2); James Tam
Input: Read Vs. Readln Both: • Reads each value inputted and matches it to the corresponding variable. Read • If the user inputs additional values they will remain Readln • Any additional values inputted will be discarded James Tam
Input: Read Vs. Readln (An example) For the complete version of this program look in Unix under: /home/231/examples/intro/read 1. p (or read 1 and read 2 for the compiled version) e. g. , read 1. p write('Input some integers making sure to separate each one with a space '); write('or a new line: '); read (num 1, num 2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); read(num 3, num 4); James Tam
Input: Read Vs. Readln (An example (2)) For the complete version of this program look in Unix under: /home/231/examples/intro/read 2. p (or read 2 for the compiled version) e. g. , read 2. p write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln (num 1, num 2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln(num 3, num 4); James Tam
Extra Uses Of Readln To filter out extraneous input As an input prompt e. g. , writeln('To continue press return'); readln; James Tam
Common Programming Errors Syntax/compile errors Runtime errors Logic errors James Tam
Syntax/Compile Errors High level algorithm Flowchart or pseudo-code (on paper) Text editor XEmacs Pascal program anything. p (Unix file) Pascal compiler gpc Syntax error: No executable (a. out) produced. James Tam
Runtime Errors High level algorithm Flowchart or pseudo-code (on paper) Text editor XEmacs Pascal program Pascal compiler anything. p (Unix file) gpc Machine language program a. out (Unix file) Executing a. out Runtime error (execution stops) James Tam
Logic Errors High level algorithm Flowchart or pseudo-code (on paper) Text editor XEmacs Pascal program Pascal compiler anything. p (Unix file) gpc Machine language program a. out (Unix file) Executing a. out Program finishes execution but may result in incorrect result James Tam
Some Useful Functions See also Appendix D in Pascal Programming and Problem Solving by Leestma S. and Nyhoff L. Name Description Input type Type of result Example absolute value integer real abs(-2) = 2 abs(-2. 2) = 2. 2 rounding real integer round(2. 6) = 3 truncation real integer trunc(2. 6) = 2 sqr squaring integer real sqr(2) = 4 sqr(1. 1) = 1. 21 sqrt square root integer real sqrt(4) = 2. 00 or real James Tam
Summary What is involved in creating and running computer programs • Typing in programs with a text editor • Translating the computer program into machine language What are the fundamental parts of a Pascal program What are the basic types of variables employed in Pascal and how are they used How to output information with the write and writeln statements Getting information from the user through the read and readln statements What are the different types of programming errors How are some common mathematical functions performed in Pascal James Tam
- Getting ahead
- Antigentest åre
- Getting started with vivado ip integrator
- Getting started with unix
- Getting started with splunk
- Rancher getting started
- Getting started with excel
- Microsoft outlook 2010 tutorial
- Counter code
- Lua getting started
- Lesson 1 hobbies
- Unit 1 local environment
- Unit 1 getting started
- Infuecers gone wild
- Getting started with vivado
- Dr jeffrey roach
- Getting started with ft8
- English 9 unit 3
- Unit 1 getting started
- Getting started with poll everywhere
- Android development getting started
- Getting started with access
- Getting started with eclipse
- Hakan kutucu
- Pascal algorithm examples
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic
- Components of system programming
- Integer programming vs linear programming
- Definisi integer
- Nocti exam study guide
- Concept of nano programming
- Concepts, techniques, and models of computer programming
- Language
- Vallath nandakumar
- Types of variables in computer programming
- Programming raster display system in computer graphics
- Components of computer programming
- Computer programming chapter 1
- Define part program