Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone.

Similar presentations


Presentation on theme: "1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone."— Presentation transcript:

1 1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone

2 2 Machine Languages Assembly Languages Procedure Languages Object Oriented Languages Non-procedural Languages Visual Programming Languages AI Languages 1 st Generation 2 nd Generation 3 rd Generation 4 th Generation 5 th Generation High Level Languages Low Level Languages Easier to program Generation of Programming Languages

3 3 Today’s Content Introduction to Programming –Classification, Generations, compilers/interpreters Sample Pascal Program Data types & Operators Algorithms Program Structure Data Input/output

4 4 Pascal Background Developed by Swiss scientist Nikalus Wirth in 1970. Named after the 17 th - Century French mathematician Blaise Pascal. Developed to teach programming as a systematic and structured activity. Easy to learn.

5 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. Back Hello World Output Screen

6 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 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 this program is called Hello The name of this program is called Hello

8 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 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 a Comment. These are ignored by the compiler. Comments are used for documentation. Text between (* and *) is taken as a Comment. These are ignored by the compiler. Comments are used for documentation.

10 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 be between a begin-end. Instructions must be between a begin-end.

11 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 to print what is given within single quotations Instructs the computer to print what is given within single quotations

12 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 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 14 Compiling and executing Type in Program in Editor Compile Program Link Program Run Program

15 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 16 Writeln, Write Example Writeln('One'); Writeln('Two'); Write('Three'); Writeln('Four'); Writeln('One'); Writeln('Two'); Write('Three'); Writeln('Four'); _ _ Output Screen

17 17 Writeln, Write Example Writeln('One');Writeln('Two'); Write('Three'); Writeln('Four'); Writeln('One');Writeln('Two'); Write('Three'); Writeln('Four'); One _ One _ Output Screen

18 18 Writeln, Write Example Writeln('One'); Writeln('Two');Write('Three'); Writeln('Four'); Writeln('One'); Writeln('Two');Write('Three'); Writeln('Four'); One Two _ One Two _ Output Screen

19 19 Writeln, Write Example Writeln('One'); Writeln('Two'); Write('Three');Writeln('Four'); Writeln('One'); Writeln('Two'); Write('Three');Writeln('Four'); One Two Three_ One Two Three_ Output Screen

20 20 Writeln, Write Example Writeln('One'); Writeln('Two'); Write('Three'); Writeln('Four'); Writeln('One'); Writeln('Two'); Write('Three'); Writeln('Four'); One Two ThreeFour _ One Two ThreeFour _ Output Screen

21 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 separate Statements. You need to put a semi colon ; to separate Statements.

22 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 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 ; stmt3 End. Begin ;; stmt1 ; stmt2 ; stmt3 End.

24 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 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 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 and Sum are variables. Number1, Number2 and Sum are variables.

27 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 28 Memory Locations Memory Addresses Data values stored in memory locations Animal 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 34 45 13 C A T 124 56 AdmissionNo Marks1 VARIABLES

29 29 Variables In a Program we use Variables to store data. Variables are locations in memory which are used to store data

30 30 Type of Variable You need to specify what type of data is to be stored. e.g. integer, real TYPE

31 31 Variable Name Each Variable must be identified by a name. e.g. Age, quantity

32 32 Value At a given time one value can be stored under the variable

33 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 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 declaration Variables in Pascal need to be declared after the Var declaration

35 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 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 _ symbol You can also use digits. But you cannot use symbols like @, #, etc A Variable Name should start with an Alphabetical letter or _ symbol You can also use digits. But you cannot use symbols like @, #, etc Valid Variable Names

37 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 other special symbols. The above names are incorrect. You cannot have spaces and other special symbols.

38 38 Arithmetic Operators Addition + Substraction - Division / Multiplication * Modulus mod Integer Divisiondiv

39 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 the Values 17 and 'c' respectively. Variables age, choice are assigned the Values 17 and 'c' respectively.

40 40 Numeric Calculations 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. 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 41 Numeric Calculations Program Sample; Var a, b, c : Integer; begin 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; 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 values 100, 50, 75 respectively. Variables a, b, c are assigned the values 100, 50, 75 respectively. 100 50 75 a b c

42 42 Numeric Calculations 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. 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 hand Side is simplified and assigned to The left side variable The expression on the right hand Side is simplified and assigned to The left side variable 100 50 75 a b c

43 43 Numeric Calculations 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. 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 a b c a := 50 + 75; The value 125 will be stored in Variable a a := 50 + 75; The value 125 will be stored in Variable a

44 44 Numeric Calculations Program Sample; Var a, b, c : Integer; begin a := 100; b := 50; c := 75; Writeln(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; Writeln(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 Writeln statement will print the Values of a, b, c The Writeln statement will print the Values of a, b, c 125 50 75 a b c 1255075 _ 1255075 _ Output Screen

45 45 Numeric Calculations 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. 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 a b c

46 46 Numeric Calculations Program Sample; Var a, b, c : Integer; begin a := 100; b := 50; c := 75; a := b + c; Writeln(a, b, c); Writeln(a,'', c); 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); Writeln(a,'', c); c := a - 20;Writeln(a,'', c); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c) End. 125 50 105 a b c 1255075 125 105 _ 1255075 125 105 _ Output Screen

47 47 Numeric Calculations 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. 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 a b c B := 125*2; 250 will be stored in Variable B. B := 125*2; 250 will be stored in Variable B.

48 48 Numeric Calculations 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,'', b); 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,'', b); b := a * 2; Writeln(a,'', b); a := c / 5; Writeln(a,'', c) End. 125 250 105 a b c 1255075 125 105 125 250 _ 1255075 125 105 125 250 _ Output Screen

49 49 Numeric Calculations 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. 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 a b c a := 105/5; 21 will be stored in Variable a a := 105/5; 21 will be stored in Variable a

50 50 Numeric Calculations 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,'', c) 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,'', c) a := c / 5; Writeln(a,'', c) End. 21 250 105 a b c 1255075 125 105 125 250 21 105 _ 1255075 125 105 125 250 21 105 _ Output Screen

51 51 Integer Operators Program 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 52 Integer Operators 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. 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 33

53 53 Integer Operators Program 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. Integer Division 100 mod 3 is 1

54 54 Char, Boolean types Program 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 : A Status : true _ Grade : A Status : true _ Output Screen

55 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 56 Development and Design Analyze the problem Develop a solution Code the solution Test and Debug

57 57 Programming Development Cycle Start Problem Definition Algorithm Design Desktop Testing Coding Testing Completed Program Problem Solving Phase Implementation Phase Pascal

58 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 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 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 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 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 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 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 65 Algorithm Design Flowchart Start Input Marks1 Calculate Sum, Avg Stop Input Marks2 Input Marks3 Print Avg

66 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 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 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 : '); Readln(Marks1); Readln(Marks1); Program Avg; Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real; Begin Write ('Marks 1 : '); Readln(Marks1); Readln(Marks1); Readln() statement will allow the user to type a value from the keyboard. The value will be stored under variable Marks1 Readln() statement will allow the user to type a value from the keyboard. The value will be stored under variable Marks1

69 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 : '); 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 : '); Readln(Marks3); Readln(Marks3);

70 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+Marks2 +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+Marks2 +Marks3; Avg := Sum/3; Avg := Sum/3;

71 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 :', 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 :', Avg); Avg);

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

73 73 Program that calculates avg 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. 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 74 Program that calculates avg Program Avg; Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real; Begin Readln(Marks1); 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 Readln(Marks1); 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 : 60 _ Marks 1 : 60 _ Output Screen

75 75 Program that calculates avg 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. 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 : 60 Marks 2 : _ Marks 1 : 60 Marks 2 : _ Output Screen

76 76 Program that calculates avg Program Avg; Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real; Begin Write ('Marks 1 : '); Readln(Marks1); Readln(Marks2); 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(Marks2); 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 2 : 70 _ Marks 1 : 60 Marks 2 : 70 _ Output Screen

77 77 Program that calculates 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); 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 : 60 Marks 2 : 70 Marks 3 : _ Marks 1 : 60 Marks 2 : 70 Marks 3 : _ Output Screen

78 78 Program that calculates avg Program Avg; Var Marks1, Marks2 Marks3, Sum :Integer; Avg: Real; Begin Write ('Marks 1 : '); Readln(Marks1); Write ('Marks 2 : '); Readln(Marks2); Readln(Marks3); 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(Marks3); Write ('Marks 3 : '); Readln(Marks3); Sum := Marks1+Marks2+Marks3; Avg := Sum/3; Writeln('Avg :', Avg); End. Marks 1 : 60 Marks 2 : 70 Marks 3 : 50 _ Marks 1 : 60 Marks 2 : 70 Marks 3 : 50 _ Output Screen

79 79 Program that calculates 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 : '); Readln(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; Avg := Sum/3; Avg := Sum/3; Writeln('Avg :', Avg); End. Marks 1 : 60 Marks 2 : 70 Marks 3 : 50 _ Marks 1 : 60 Marks 2 : 70 Marks 3 : 50 _ Output Screen Sum and Avg will be calculated Sum := 60+70+50=180 Avg := 180/3 = 60 Sum and Avg will be calculated Sum := 60+70+50=180 Avg := 180/3 = 60

80 80 Program that calculates 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 : '); 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 : 60 Marks 2 : 70 Marks 3 : 50 Avg : 60.0 Marks 1 : 60 Marks 2 : 70 Marks 3 : 50 Avg : 60.0 Output Screen

81 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 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 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 84 Sample Answer – (ii) Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; numberi number2 : = numberi * 4; writeln('Help) end Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; numberi number2 : = numberi * 4; writeln('Help) end Error

85 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 Colons

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

87 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 colon after begin You should not use a semi colon after begin

88 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 should Be used here. The assignment operator should Be used here.

89 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 operator by a space. You cannot separate an operator by a space.

90 90 Sample Answer – (ii) Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; numberi number2 : = numberi * 4; writeln('Help) end Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; numberi number2 : = numberi * 4; writeln('Help) end There is no variable called Numberi it should be number1 There is no variable called Numberi it should be number1

91 91 Sample Answer – (ii) Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; number2 : = numberi * 4; 'Help writeln('Help) end Progam TEST (output) Var number1, number2; integer; begin; number1 = 24; number2 : = numberi * 4; 'Help writeln('Help) end There should be an end single Quotation. There should be an end single Quotation.

92 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 program Should use a full stop to end program

93 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 94 Sample Answer – (ii) Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter := 'A';

95 95 Sample Answer – (ii) Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Initial := 0; StartCount := 0; Initial := 0; StartCount := 0;

96 96 Sample Answer – (ii) Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Taxrate := 0.05;

97 97 Sample Answer – (ii) Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Total := 5 + 7;

98 98 Sample Answer – (ii) Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Firstletter : = A; StartCount := Initial := 0; Taxrate := 5%; Total := 5 plus 7; Efficiency :=.35; Efficiency := 0.35;

99 99 Essay type Model Questions (iv) What is displayed by the following program? program EXERCISEl (output) ; var a, b : integer; c : real ; begin a := 1; b := 5; c := 1.20; writeln ('A = ', a + 3) ; writeln ('B = ', b - 2) ; Writeln ('C =', c / 2) end.

100 100 Sample Answer – (ii) program EXERCISEl (output) ; var a, b : integer; c : real ; begin a := 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 ; begin a := 1; b := 5; c := 1.20; writeln ('A = ', a + 3) ; writeln ('B = ', b - 2) ; Writeln ('C =', c / 2) end. A = 4 B = 3 C = 0.6 A = 4 B = 3 C = 0.6 Output Screen

101 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 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 103 Algorithm Design Flowchart Start Input Fahrenheit Calculate Celsius Stop Print Celsius

104 104 Test Data Fahrenheit = 212 Celsius = 100 Fahrenheit = 32 Celsius = 0 Fahrenheit = 98.7 Celsius = 37.056

105 105 Program Temperature; var Fahrenheit, Celsius : Real; begin end. Program Temperature; var Fahrenheit, Celsius : Real; begin end. Start Input Fahrenheit Calculate Celsius Stop Print Celsius Write('Fahrenheit : '); Readln(Fahrenheit); Celsius := 5 * (Fahrenheit-32)/9; Writeln('Celsius : ', Celsius:7:2);

106 106 Temperature Conversion Program Temperature; var Fahrenheit, Celsius : Real; begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Celsius:7:2 Writeln('Celsius : ', Celsius:7:2); end. Program Temperature; var Fahrenheit, Celsius : Real; begin Write('Fahrenheit : '); Readln(Fahrenheit); Celcius := 5 * (Fahrenheit-32)/9; Celsius:7:2 Writeln('Celsius : ', Celsius:7:2); end. Print Celsius to a width of 7 digits And 2 decimal places Print Celsius to a width of 7 digits And 2 decimal places Fahrenheit : 212 Celsius : _ 100.00 _ Fahrenheit : 212 Celsius : _ 100.00 _ Output Screen

107 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 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 109 User Defined Types Sample Prg 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. 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 110 Constants Constants are data that do not change in the program. e.g. pi = 3.1416 rate = 100 Constants are defined under the heading const.

111 111 Constants Sample Program Program AreaOfCircle;const 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;const 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 with the value 3.1416 Defines a constant called pi with the value 3.1416

112 112 Constants Sample Program 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); 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); end. Enter Radius : _ Enter Radius : _ Output Screen radius area

113 113 Constants Sample Program 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); 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); end. 10 Enter Radius : 10 _ 10 Enter Radius : 10 _ Output Screen 10 radius area

114 114 Constants Sample Program 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); 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); end. 10 314.16 radius area 10 Enter Radius : 10 _ 10 Enter Radius : 10 _ Output Screen

115 115 Constants Sample Program 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); 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); end. 10 314.16 radius area Enter Radius : 10 Area is : _ 314.16 _ Enter Radius : 10 Area is : _ 314.16 _ Output Screen

116 End 116


Download ppt "1 Programming in Pascal Presented by:- Zonal ICT Section- Kurunegala Education Zone."

Similar presentations


Ads by Google