Post on 20-Jan-2016
1
Programming in Pascal
Presented by:-Zonal ICT Section-Kurunegala Education
Zone
2
Machine Languages
Assembly Languages
Procedure Languages
Object Oriented
Languages
Non-procedural Languages
Visual
Programming
Languages
AI Languages
1st Generation
2nd
Generation
3rd
Generation
4th
Generation
5th
Generation
High Level Languages
Low Level Languages
Easier to program
Generation of Programming Languages
3
Today’s Content
• Introduction to Programming– Classification, Generations,
compilers/interpreters• Sample Pascal Program• Data types & Operators• Algorithms • Program Structure • Data Input/output
4
Pascal Background
• Developed by Swiss scientist Nikalus Wirth in 1970.
• Named after the 17th- Century French mathematician Blaise Pascal.
• Developed to teach programming as a systematic and structured activity.
• Easy to learn.
5
Sample Pascal Program
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
BackHello World Hello World
Output Screen
6
Sample Pascal Program
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
Every Pascal program must have a name.
Every Pascal program must have a name.
7
Sample Pascal Program
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
The name of thisprogram is called Hello
The name of thisprogram is called Hello
8
Sample Pascal Program
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Pascal Command are separated by Semi colons
Pascal Command are separated by Semi colons
9
Sample Pascal Program
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');End.
•Text between (* and *) is taken as aComment. •These are ignored by the compiler.•Comments are used for documentation.
•Text between (* and *) is taken as aComment. •These are ignored by the compiler.•Comments are used for documentation.
10
Sample Pascal Program
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Instructions must bebetween a begin-end. Instructions must be
between a begin-end.
11
Sample Pascal Program
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World');End.
Instructs the computer toprint what is given within
single quotations
Instructs the computer toprint what is given within
single quotations
12
Compiling and executing • Compiling a Program
– The computer cannot understand the instructions we have given in Pascal.
– We need to convert the instructions given in Pascal into a language the computer can understand (Machine Code)
– This conversion is called compiling
13
Compiling and executing • Executing a Program
– Once you have compiled and linked a program there will be an executable machine code program saved in the computer.
– You can ask the computer to perform the instructions given by running it from the programming environment (IDE) you are in, or by directly executing the Executable file.
14
Compiling and executing
• Type in Program in Editor• Compile Program• Link Program• Run Program
15
Write, Writeln Statements
• Used to display a message on the screen.
• Write statement will display the message on the screen starting from the current cursor location.
• Writeln statement will move the cursor to the next line after displaying the message.
16
Writeln, Write Example
Writeln('One');Writeln('One');Writeln('Two');Write('Three');Writeln('Four');
Writeln('One');Writeln('One');Writeln('Two');Write('Three');Writeln('Four');
_
_
Output Screen
17
Writeln, Write Example
Writeln('One');Writeln('Two');Writeln('Two');Write('Three');Writeln('Four');
Writeln('One');Writeln('Two');Writeln('Two');Write('Three');Writeln('Four');
One_
One_
Output Screen
18
Writeln, Write Example
Writeln('One');Writeln('Two');Write('Three');Write('Three');Writeln('Four');
Writeln('One');Writeln('Two');Write('Three');Write('Three');Writeln('Four');
OneTwo_
OneTwo_
Output Screen
19
Writeln, Write Example
Writeln('One');Writeln('Two');Write('Three');Writeln('Four');Writeln('Four');
Writeln('One');Writeln('Two');Write('Three');Writeln('Four');Writeln('Four');
OneTwoThree_
OneTwoThree_
Output Screen
20
Writeln, Write Example
Writeln('One');Writeln('Two');Write('Three');Writeln('Four');
Writeln('One');Writeln('Two');Write('Three');Writeln('Four');
OneTwoThreeFour_
OneTwoThreeFour_
Output Screen
21
Statements and Expressions
• A Statement is the simplest task you can accomplish in Pascal.
height : integer; Program test;Writeln('Height is', height);Avg := (mrks1+mrks2+mrks3)/3;
height : integer; Program test;Writeln('Height is', height);Avg := (mrks1+mrks2+mrks3)/3;
You need to put a semi colon ; to separateStatements.
You need to put a semi colon ; to separateStatements.
22
Sample Pascal Program
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.
Pascal statements are Separated by semi colons
Pascal statements are Separated by semi colons
23
Sample Pascal Program
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.
Program Hello;(* My First Pascal Prg *)Begin Writeln('Hello World'); Writeln('second line'); Writeln('Last Line')End.•Semi colons are used to separate statements.
•The semi colon for the last statement is not required.
•Semi colons are used to separate statements. •The semi colon for the last statement is not required.
Begin stmt1 ;; stmt2 ;; stmt3End.
Begin stmt1 ;; stmt2 ;; stmt3End.
24
Sample Pascal Program
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');Writeln('second line'); Writeln('Last Line') End.
Program Hello;(* My First Pascal Prg *) Begin Writeln('Hello World');Writeln('second line'); Writeln('Last Line') End.
• It is possible write a program as shown above. • Having a program Indented makes it more easy to understand.
• It is possible write a program as shown above. • Having a program Indented makes it more easy to understand.
25
A Program to Add two numbers
• Lets consider a program that is used to calculate the addition of two numbers.
• We will need to make use of variables in the Pascal program to store data.
26
Program that adds two numbers
Program Addition;Var number1, number2 : integer; sum : integer;Begin number1 := 20; number2 := 56; sum := number1 + number2; Writeln('Sum is ',sum);end.
Program Addition;Var number1, number2 : integer; sum : integer;Begin number1 := 20; number2 := 56; sum := number1 + number2; Writeln('Sum is ',sum);end.
Sum is 76_
Sum is 76_
Output Screen
Number1, Number2 andSum are variables.
Number1, Number2 andSum are variables.
27
Mr Kitsiri Almeda Mr Rohan
Perera
Mr Nihal Peries
No, 128 Railway Road, Nugegoda
No, 130 Railway Road, Nugegoda
No, 132 Railway Road, Nugegoda
Analogy of Variables, Memory
28
Memory Locations
Memory
Addresses
Data values stored in memory locations
Animal
4504450545064507
4508
4509
4510451145124513
4514
344513
CAT
12456
AdmissionNo Marks1
VARIABLES
29
Variables
• In a Program we use Variables to store data.• Variables are locations in memory which are
used to store data
30
Type of Variable
• You need to specify what type of data is to be stored. e.g. integer, real
TYPE
31
Variable Name
• Each Variable must be identified by a name. e.g. Age, quantity
32
Value
• At a given time one value can be stored under the variable
33
Data Types in Pascal
• Integers e.g. 78, -9000, 4500• Real e.g. 5.67, 900.23, 5e6• Char e.g. 'A', 'C', '9'• Boolean e.g. True, False• String e.g. 'computer', 'Nimal'
34
Declaring Variables
var Count, I, R : Integer; Salary : Real; isAsleep : Boolean;
var Count, I, R : Integer; Salary : Real; isAsleep : Boolean;
Variables in Pascal need to be declared after the Var declarationVariables in Pascal need to be declared after the Var declaration
35
Valid Variable Names
A Variable Name should start with an letter or _ symbol.The rest can consist of letters, digits and the _ symbol.
A Variable Name should start with an letter or _ symbol.The rest can consist of letters, digits and the _ symbol.
36
Age : Integer;my_char : char;_no : integer;No2 : real;
Age : Integer;my_char : char;_no : integer;No2 : real;
A Variable Name should start with an Alphabetical letter or _ symbolYou can also use digits.But you cannot use symbols like @, #, etc
A Variable Name should start with an Alphabetical letter or _ symbolYou can also use digits.But you cannot use symbols like @, #, etc
Valid Variable Names
37
Variable Names
my age : Integer;@money : Real;6my_char : Char;no* : Integer;
my age : Integer;@money : Real;6my_char : Char;no* : Integer;
The above names are incorrect.You cannot have spaces and otherspecial symbols.
The above names are incorrect.You cannot have spaces and otherspecial symbols.
38
Arithmetic Operators
Addition +Substraction -Division /Multiplication *Modulus modInteger Division div
39
Assignment Operator
Var age : integer; choice : char;Begin age := 17; choice := 'c';
Var age : integer; choice : char;Begin age := 17; choice := 'c';
Variables age, choice are assigned theValues 17 and 'c' respectively.Variables age, choice are assigned theValues 17 and 'c' respectively.
40
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
41
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100;a := 100; b := 50;b := 50; c := 75;c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Program Sample;Var a, b, c : Integer;begin a := 100;a := 100; b := 50;b := 50; c := 75;c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Variables a, b, c are assigned the values100, 50, 75 respectively.Variables a, b, c are assigned the values100, 50, 75 respectively.
100
50
75
aa
bb
cc
42
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
The expression on the right handSide is simplified and assigned toThe left side variable
The expression on the right handSide is simplified and assigned toThe left side variable
100
50
75
aa
bb
cc
43
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c;a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
100
50
75
aa
bb
cc
a := 50 + 75; The value 125 will be stored in Variable aa := 50 + 75; The value 125 will be stored in Variable a
44
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c);Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c);Writeln(a, b, c); c := a - 20;Writeln(a, b, c); b := a * 2; Writeln(a, b, c); a := c / 5; Writeln(a, b, c)End.
The Writeln statement will print the Values of a, b, cThe Writeln statement will print the Values of a, b, c
125
50
75
aa
bb
cc
1255075_
1255075_
Output Screen
45
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
C := 125 - 20 The value 105 will be stored in Variable c
C := 125 - 20 The value 105 will be stored in Variable c
125
50
75
aa
bb
cc
46
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c);Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c);Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
125
50
105
aa
bb
cc
1255075125 105_
1255075125 105_
Output Screen
47
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2;b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2;b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
125
50
105
aa
bb
cc
B := 125*2;250 will be stored in Variable B.
B := 125*2;250 will be stored in Variable B.
48
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b);Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b);Writeln(a,'', b); a := c / 5; Writeln(a,'', c)End.
125
250
105
aa
bb
cc
1255075125 105125 250_
1255075125 105125 250_
Output Screen
49
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5;a := c / 5; Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5;a := c / 5; Writeln(a,'', c)End.
125
250
105
aa
bb
cc
a := 105/5;21 will be stored in Variable a
a := 105/5;21 will be stored in Variable a
50
Numeric CalculationsProgram Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)Writeln(a,'', c)End.
Program Sample;Var a, b, c : Integer;begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c)Writeln(a,'', c)End.
21
250
105
aa
bb
cc
1255075125 105125 25021 105_
1255075125 105125 25021 105_
Output Screen
51
Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3; Writeln(a, b, c)End.
Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3; Writeln(a, b, c)End.
100331_
100331_
Output Screen
52
Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3;b := a div 3; c := a mod 3; Writeln(a, b, c)End.
Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3;b := a div 3; c := a mod 3; Writeln(a, b, c)End.
Integer Division 100 div 3 is 33Integer Division 100 div 3 is 33
53
Integer OperatorsProgram Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3;c := a mod 3; Writeln(a, b, c)End.
Program Sample2;Var a, b, c : Integer;Begin a := 100; b := a div 3; c := a mod 3;c := a mod 3; Writeln(a, b, c)End.
Integer Division 100 mod 3 is 1Integer Division 100 mod 3 is 1
54
Char, Boolean typesProgram Sample3;Var status : boolean; grade : char;Begin status := true; grade := 'A'; Writeln('Grade : ',grade); Writeln('Status : ',status)End.
Program Sample3;Var status : boolean; grade : char;Begin status := true; grade := 'A'; Writeln('Grade : ',grade); Writeln('Status : ',status)End.
Grade : AStatus : true_
Grade : AStatus : true_
Output Screen
55
A Program to find the avg
• Lets develop a program to calculate the average marks a student gets in an AL term test.
• The marks of three subjects will be entered into the program. Your program should calculate and print the average marks of the three subjects.
56
Development and Design
• Analyze the problem• Develop a solution• Code the solution• Test and Debug
57
Programming Development Cycle
Start
ProblemDefinition
AlgorithmDesign
DesktopTesting
Coding
Testing
Completed Program
Problem Solving Phase
Implementation Phase
Pascal
58
Analyze the Problem
• Determine the information the computer is to produce, i.e. the output.
• Determine the information required to produce the output, i.e. the input data.
• Determine if any special cases exist, and how they are to be handled.
59
Develop the solution
• Determine an algorithm which will transform the input to the output
• If the problem is complex, divide the problem into subtasks, where the completion of all the subtasks in some specified order will produce the desired output.
60
Design Components
• Data Structures Description of data, its name, type,
and range of values.• Algorithms
An algorithm is a precise, unambiguous, step-by-step description of how to do a task in a finite amount of time.
61
Code the Solution
• Draw a flowchart for the algorithm• Convert your flowchart to a Pascal
program.• Look up the syntax of any Pascal
statements you are unsure of as you write them.
• Compile the program, and fix any syntax errors.
62
Test and Correct the Program
• Determine a set of test data, and hand calculate the expected answer.
• Run the program, and compare the computer's answer to the expected answer.
• If they do not agree, examine the program; determine where the error is; and fix the error.
63
Applying the Design Process to an Example
• The marks of three subjects will be entered to the program. Your program should calculate and print the average of the three subjects.
64
Problem Definition
• Understand the problem
• What are the Inputs ?
• What are the Outputs ?
• Calculations ?
• Background
3 Marks
Avg
Calculate Avg
Additional Data if any
We need to calculate the sum
65
Algorithm Design
Flowchart
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
66
Test Data
• Marks1 = 60, Marks2 = 70, Marks3 = 50• Sum = 180, Avg = 60
• Marks1 = 90, Marks2 = 60, Marks3 = 60• Sum = 210, Avg = 70
• Marks1 = 50, Marks2 = 0, Marks3 = 25• Sum = 75, Avg = 25
67
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
68
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1);
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1);
Readln() statement will allowthe user to type a value fromthe keyboard. The value willbe stored under variable Marks1
Readln() statement will allowthe user to type a value fromthe keyboard. The value willbe stored under variable Marks1
69
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3);
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3);
70
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2Sum := Marks1+Marks2
+Marks3;+Marks3; Avg := Sum/3;Avg := Sum/3;
Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2Sum := Marks1+Marks2
+Marks3;+Marks3; Avg := Sum/3;Avg := Sum/3;
71
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2
+Marks3; Avg := Sum/3; Writeln('Avg :',Writeln('Avg :', Avg);Avg);
Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2
+Marks3; Avg := Sum/3; Writeln('Avg :',Writeln('Avg :', Avg);Avg);
72
Start
Input Marks1
Calculate Sum, Avg
Stop
Input Marks2
Input Marks3
Print Avg
Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2
+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.End.
Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2
+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.End.
73
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : ');Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : _
Marks 1 : _
Output Screen
74
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1);Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60_
Marks 1 : 60_
Output Screen
75
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : ');Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : ');Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : _
Marks 1 : 60Marks 2 : _
Output Screen
76
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2);Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2);Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : 70_
Marks 1 : 60Marks 2 : 70_
Output Screen
77
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : ');Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : 70Marks 3 : _
Marks 1 : 60Marks 2 : 70Marks 3 : _
Output Screen
78
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3);Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : 70Marks 3 : 50_
Marks 1 : 60Marks 2 : 70Marks 3 : 50_
Output Screen
79
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3;Sum := Marks1+Marks2+Marks3; Avg := Sum/3;Avg := Sum/3; Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3;Sum := Marks1+Marks2+Marks3; Avg := Sum/3;Avg := Sum/3; Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : 70Marks 3 : 50_
Marks 1 : 60Marks 2 : 70Marks 3 : 50_
Output Screen
Sum and Avg will be calculatedSum := 60+70+50=180Avg := 180/3 = 60
Sum and Avg will be calculatedSum := 60+70+50=180Avg := 180/3 = 60
80
Program that calculates avgProgram Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);Writeln('Avg :', Avg);End.
Program Avg;Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real;Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg);Writeln('Avg :', Avg);End.
Marks 1 : 60Marks 2 : 70Marks 3 : 50Avg : 60.0
Marks 1 : 60Marks 2 : 70Marks 3 : 50Avg : 60.0
Output Screen
81
Essay type Model Questions
(i) Write a program to print the following words on the console screen.
• Hello. How are you?• I'm just fine.
82
Sample Answer – (i)
Program ModelAns1;Begin Writeln ('Hello. How are you ?'); Writeln ('I”m just fine.') End.
Program ModelAns1;Begin Writeln ('Hello. How are you ?'); Writeln ('I”m just fine.') End.
83
Essay type Model Questions
(ii) The following program contains a few errors. Identify each error (there areseven), and show the correct version on the right.
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
84
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end
ErrorError
85
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
No Semi ColonsNo Semi Colons
86
Sample Answer – (ii)Progam TEST (output)Var number1, number2;; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2;; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
You need to use colon. ::You need to use colon. ::
87
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;;number1 = 24;number2 : = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;;number1 = 24;number2 : = numberi * 4;writeln('Help)end
You should not use a semi colonafter beginYou should not use a semi colonafter begin
88
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 == 24;number2 : = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 == 24;number2 : = numberi * 4;writeln('Help)end
The assignment operator shouldBe used here.The assignment operator shouldBe used here.
89
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : =: = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : =: = numberi * 4;writeln('Help)end
You cannot separate an operatorby a space.You cannot separate an operatorby a space.
90
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberinumberi * 4;writeln('Help)end
There is no variable calledNumberi it should be number1There is no variable calledNumberi it should be number1
91
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help'Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help'Help)end
There should be an end singleQuotation.There should be an end singleQuotation.
92
Sample Answer – (ii)Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
Progam TEST (output)Var number1, number2; integer;begin;number1 = 24;number2 : = numberi * 4;writeln('Help)end
Should use a full stop to end programShould use a full stop to end program
93
Essay type Model Questions
• (iii) Each of the following expressions is wrong. Rewrite each using correct Pascal.
Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
94
Sample Answer – (ii)Firstletter : = A;Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Firstletter : = A;Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Firstletter := 'A';Firstletter := 'A';
95
Sample Answer – (ii)Firstletter : = A;
StartCount := Initial := 0;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Firstletter : = A;
StartCount := Initial := 0;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Initial := 0;StartCount := 0;
Initial := 0;StartCount := 0;
96
Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;
Taxrate := 5%;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Firstletter : = A;StartCount := Initial := 0;
Taxrate := 5%;Taxrate := 5%;Total := 5 plus 7;Efficiency := .35;
Taxrate := 0.05;Taxrate := 0.05;
97
Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;
Total := 5 plus 7;Total := 5 plus 7;Efficiency := .35;
Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;
Total := 5 plus 7;Total := 5 plus 7;Efficiency := .35;
Total := 5 + 7;Total := 5 + 7;
98
Sample Answer – (ii)Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;
Efficiency := .35;Efficiency := .35;
Firstletter : = A;StartCount := Initial := 0;Taxrate := 5%;Total := 5 plus 7;
Efficiency := .35;Efficiency := .35;
Efficiency := 0.35;Efficiency := 0.35;
99
Essay type Model Questions• (iv) What is displayed by the following
program?program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.
100
Sample Answer – (ii)
program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.
program EXERCISEl (output) ;var a, b : integer;c : real ;begina := 1; b := 5; c := 1.20;writeln ('A = ', a + 3) ;writeln ('B = ', b - 2) ;Writeln ('C =', c / 2)end.
A = 4 B = 3C = 0.6
A = 4 B = 3C = 0.6
Output Screen
101
Temperature Conversion
•Write a program to input a temperature in Fahrenheit. Convert and Print this temperature in Celsius.
•Celsius = 5 x (Fahrenheit-32)/9
102
Problem Definition
• Understand the problem
• What are the Inputs ?
• What are the Outputs ?
• Calculations ?
• Background
Temperature in Fahrenheit
Temperature in Celsius
Calculate Temperature in Celsius
103
Algorithm Design
Flowchart
Start
Input Fahrenheit
Calculate Celsius
Stop
PrintCelsius
104
Test Data
• Fahrenheit = 212• Celsius = 100
• Fahrenheit = 32• Celsius = 0
• Fahrenheit = 98.7• Celsius = 37.056
105
Program Temperature;var Fahrenheit, Celsius
: Real;begin
end.
Program Temperature;var Fahrenheit, Celsius
: Real;begin
end.
Start
Input Fahrenheit
Calculate Celsius
Stop
PrintCelsius
Write('Fahrenheit : '); Readln(Fahrenheit); Celsius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2);
106
Temperature ConversionProgram Temperature;var Fahrenheit, Celsius : Real;begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2Celsius:7:2);end.
Program Temperature;var Fahrenheit, Celsius : Real;begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2Celsius:7:2);end.
Print Celsius to a width of 7 digitsAnd 2 decimal places
Print Celsius to a width of 7 digitsAnd 2 decimal places
Fahrenheit : 212Celsius : _100.00_
Fahrenheit : 212Celsius : _100.00_
Output Screen
107
User Defined Types
• Enumerated Types– e.g Type Days = (Mon, Tue, Wed, Thu, Fri,
Sat, Sun);– Increases the Readability of the
program.– You cannot input or print
enumerated types
108
User Defined Types
• Sub Ranges– e.g Type MarksType = 0..100;Var Marks : MarksType; Age : 5..19; letter : 'a' .. 'z';– Specify a lower limit and a upper
limit for ordinal type data.
109
User Defined Types Sample PrgProgram UserDefined;type Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); MarksType = 0..100;Var today : Days; Marks : MarksType; Age : 5..19; begin today := Tue; Marks := 78; Age := 17;end.
Program UserDefined;type Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); MarksType = 0..100;Var today : Days; Marks : MarksType; Age : 5..19; begin today := Tue; Marks := 78; Age := 17;end.
110
Constants
• Constants are data that do not change in the program.
• e.g.pi = 3.1416
rate = 100Constants are defined under the heading
const.
111
Constants Sample ProgramProgram AreaOfCircle;
constconst
pi = 3.1416;pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Program AreaOfCircle;
constconst
pi = 3.1416;pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Defines a constant called pi withthe value 3.1416
Defines a constant called pi withthe value 3.1416
112
Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘);Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘);Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Enter Radius : _
Enter Radius : _
Output Screen
radiusradius
areaarea
113
Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius);Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius);Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Enter Radius : 1010_
Enter Radius : 1010_
Output Screen
10radiusradius
areaarea
114
Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius;area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius;area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);end.
10
314.16
radiusradius
areaarea
Enter Radius : 1010_
Enter Radius : 1010_
Output Screen
115
Constants Sample ProgramProgram AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);Write(‘Area is : ‘, area:7:2);end.
Program AreaOfCircle;const pi = 3.1416;Var radius : integer; area : real;begin Write(‘Enter Radius : ‘); Readln(Radius); area := pi*Radius*Radius; Write(‘Area is : ‘, area:7:2);Write(‘Area is : ‘, area:7:2);end.
10
314.16
radiusradius
areaarea
Enter Radius : 10Area is : _314.16_
Enter Radius : 10Area is : _314.16_
Output Screen
End
116