Introduction To Programming: CSCE 110
Introduction To Programming: CSCE 110
CSCE 110
Drawn from James Tams material
Computer Programs
Binary is the language of the computer
________ e.g., gpc
1) A programmer writes a computer program 2) A ___________ converts the program into a form that the computer can understand
4) Anybody who has this ___________ installed on their computer can run (use) it.
Translators
Convert computer programs to machine language Types
________________
Each time that the program is run the ____________ translates the program (translating a part at a time). If there are any errors during the process of __________ the program, the program will stop running right when the error is encountered.
Translators
Convert computer programs to machine language Types
___________________ ___________________
Before the program is run the _________ translates the program (compiling it all at once). If there are any errors during the ____________ process, no machine language executable will be produced. If there are no errors during _______________ then the translated machine language program can be run.
a.out
Part I: Header
Program documentation program name (input, output);
Header
Program documentation
Comments for the reader of the program (and not the computer)
(* *) Marks the ____________ of the documentation Marks the ____________ of the documentation
Program heading
Keyword: program, Name of program, if input and/or output operations performed by the program.
Example Header
(* * Tax-It v1.0: This program will * electronically calculate your tax * return. * This program will only allow you to * complete a Canadian tax return *) program taxIt (input, output);
Documentation
Heading
Declarations
List of constants More to come later during this term regarding this section
Statements
The instructions in the program that actually gets things done They tell the computer what to do as the program is running Statements are __________________________ Example statements: display a message onscreen, prompt the user for input, open a file and write information to that file etc. Much more to come later throughout the rest of the term regarding this section
Note: The name in the header "smallest" should match the filename "smallest.pas". You can find an copy of this program here and the compiled version is here.
Pacal Program
filename.pas (Unix file)
Pacal Compiler
gpc Machine Language Program
a.out (Unix file)
A file that contains machine language (binary) code. By default this file will be called a.out. It cannot be directly viewed or edited (meaningless). It can be executed.
1. Syntax/Compile Errors
Text editor PSPad
Pascal program filename.p (Unix file)
2. Runtime Errors
Text editor PSPad Pascal program filename.p (Unix file)
Executing a.out
Runtime error
(________________)
3. Logic Errors
Text editor
PSPad Pascal program filename.p (Unix file)
Reserved Words
Have a predefined meaning in Pascal that cannot be changed
Reserved Words
Have a predefined meaning in Pascal that cannot be changed
Standard Identifiers
Have a predefined meaning in Pascal that SHOULD NOT be changed Predefined constants
false true maxint
Predefined types
boolean char integer real text
Predefined files
input output
Predefined Functions
abs arctan chr cos eof eoln exp ln odd ord pred round sin sqr sqrt succ trunc
Predefined Procedures
dispose get new pack page put read readln reset rewrite unpack write writeln
Variables
Set aside a location in memory
This location can store __________ piece of information
Variable Types
integer whole numbers real whole numbers and fractions char alphabetic, numeric and miscellaneous symbols (in UNIX type man ascii) boolean a true or false value
Using Variables
Usage (__________________________!)
Declaration Accessing or assigning values to the variables
Declaring Variables
Sets aside memory Memory locations are addressed through the name of the variable
Declaring Variables
Sets aside memory Memory locations are addressed through ________ _______________
Name of variable
RAM
RESERVED
Declaring Variables
Declare variables between the begin and end.
Part I: Header
Program documentation program name (input, output);
Declaring Variables
Format:
var name of first variable : type of first variable; var name of second variable : type of second variable;
Variable Declaration
You can find an copy of this program here and the compiled version is here.
Global Variables
Variables declared outside of the begin-end pair.
program anExample; var num1 : integer; Global variable: DONT DO IT THIS WAY begin var num2 : integer; Non-global variable (local variable): DO IT THIS WAY end.
For now avoid doing this (additional details will be provided later in the course): generally this is regarded as ______ programming style.
Not Okay
(___________________) - 1abc test.msg good-day program
Not okay
(_________________) x writeln
Accessing Variables
Can be done by referring to the name of the variable Format:
name of variable
Example:
num
NO!
You can find an copy of this program here and the compiled version is here.
_____________
Important lesson: ALWAYS _____________ your variables to some default starting value before using them.
Reminder
Variables Must First Be Declared Before They Can Be Used!
RAM
Reminder
Variables Must First Be Declared Before They Can Be Used!
RAM
Compile Error:
Where is num???
num := 888;
var num : integer; end.
Named Constants
A memory location that is assigned a value that ______________________________ Declared in the constant declaration ("const") section The naming conventions for choosing variable names generally apply to constants but the name of constants should be all ____________. (You can separate multiple words with ___________________).
Part I: Header
Program documentation program name (input, output);
Better
const Magic Numbers BIRTH_RATE = 0.1758; (_____________ _____________!) DEATH_RATE = 0.1257; begin populationChange := (BIRTH_RATE DEATH_RATE) * currentPopulation;
Performing Calculations
Operation
Addition Subtraction Multiplication Real number division Integer division
Symbol (Operator)
+ * / DIV
Remainder (modulo)
MOD
Priority of Operators
High:
* / DIV MOD
Low:
+ unary + unary -
Program Documentation
It doesnt __________________________ It doesnt __________________________ __________________________________ It is for ____________________________
Program Documentation
What does the program do e.g., tax program. What are its capabilities e.g., it calculates personal or small business tax. What are its limitations e.g., it only follows Canadian tax laws and cannot be used in the US.
Program Documentation
What is the version of the program
If you dont use numbers for the different versions of your program then consider using dates.