information technology csec cxc

23
INFORMATION TECHNOLOGY CSEC CXC 03/11/22 1

Upload: jackson-pearson

Post on 04-Jan-2016

343 views

Category:

Documents


22 download

DESCRIPTION

Introduction to Pascal Programming. INFORMATION TECHNOLOGY CSEC CXC. Pascal Programming. PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides a teaching language that highlights concepts common to all computer languages - PowerPoint PPT Presentation

TRANSCRIPT

INFORMATION TECHNOLOGY

CSEC CXC

04/20/23 1

•PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides a teaching language that highlights concepts common to all computer languages •standardizes the language in such a way that it makes programs easy to write •Strict rules make it difficult for the programmer to write bad code!

04/20/23 2

•Allows programmers to use complex data types (structured)

•easier to build dynamic and recursive data structures such as lists, trees and graphs

Blaise Pascal , June 19, 1623 – August 19, 1662

04/20/23 3

defines a variable (data item) in such a way as a range of values which the variable is capable of storing, and it also defines a set of operations that are permissible to be performed on variables of that type

Data

type

Range of values which the variable

is capable of storing

Integer Whole numbers from -32768 to 32767

Byte The integers from 0 to 255

Real Floating point numbers from 1E-38 to 1E+38

Boolean Can only have the value TRUE or FALSE

Char Any character in the ASCII character set

Shortint The integers from -128 to 127

Word The integers from 0 to 65535

Longint The integers from -2147483648 to 2147483647

04/20/23 4

Please note that four of these types of data (char, shortint, word, and longint) are not a part of the standard Pascal definition but are included as extensions to the TURBO Pascal compiler.

These statements are used to announce the Variables and Constants that are to be used in the program as well as the appropriate Data Type format they will take.

04/20/23 5

var  Identifier1 , Identifier2: Data Type  Identifier3 : Data Type; 

const  Name = 'Tao Yue';  FirstLetter = 'a';  Year = 1997;  pi = 3.1415926535897932;  UsingNCSAMosaic = TRUE;

var  age, year, grade : integer;  circumference : real;  LetterGrade : char [1];  DidYouFail : Boolean;

const  Identifier1 = value;  Identifier2 = value;  Identifier3 = value;

Write a program in Pascal to display the phrase Hello, World!

 

PROGRAM HelloWorld (output); 

BEGIN

Writeln ('Hello, World!');

END.

Parts of a Pascal Program:

1. Program Title (Header) – this tells the name of the program and may state the actions the program carries out. The title should not contain spaces or start with a number or have in a special character sign

04/20/23 6

2. Body – this refers to the statements to be executed by the Compiler.

•An Identifier is a name given to an item of data in a program.•The Identifier name once used can only represent that one item of data when called or used in a program.•The Identifier name should not begin with a number, special character or contains special characters.

04/20/23 7

Name

Age #Room Team 1 Full

•Constants represents a value of data that remains fixed is not designed or expected to change. E.g. the number of days in a week could be set to 7 as there can be only 7 days in 1 week.•The number of ounces in a pound of flour will always be 16 ounces represents 1 pound.

04/20/23 8

Assignment Statements in programming are used to give a value to a variable/constant. The Assignment Statement can also change the value of a variable or constant.

How to use this statement:

variable_name := expression;

E.g.

carry a single value

some_real := 385.385837;

or an arithmetic sequence

some_real := 37573.5 * 37593 + 385.8 / 367.1;

04/20/23 9

04/20/23 10

div and mod only work on integers. / works on both reals and integers but will always yield a real answer. The other operations work on both reals and integers. When mixing integers and reals, the result will always be a real since data loss would result otherwise. This is why Pascal uses two different operations for division and integer division. 7 / 2 = 3.5 (real), but 7 div 2 = 3 (and 7 mod 2 = 1 since that's the remainder).

04/20/23 11

Pascal compiler will not know if its toMultiply or Subtract. Use parentheses.

Above is Pascal code not indented or properly, which makes for hard reading. Instead, indents are used on the right.

04/20/23 12

Useful for on screen output

See example on Average of 5 numbers using constants and displaying results.

Use of read and readlnread treats input as a stream of characters, with lines separated by a special end-of-line character. readln, on the other hand, will skip to the next line after reading a value, by automatically moving past the next end-of-line character:

04/20/23 13

Use of the Write Statements

Used to communicate results to the outside world. Used to print words on the terminal screen.

Write (‘This is our first example of output.’);

This is our first example of output.

PROGRAM First;

{Demonstrates the use of Write Statements}

BEGIN

Write (‘This is our first example of output.’);

END.

04/20/23 14

OUTPUT ON SCREEN

This is our first example of output.

PROGRAM First; BEGIN write (‘This is our first example of output’.)

BAD CODING

04/20/23 15

With using the write statement in Pascal Programming, the write statement when used, leaves the cursor where it is after printing is complete.

write (‘My name’);write (‘is Andrew’) My nameis Andrew

PROGRAM OneLine (output);BEGINwrite (‘My name’);write (‘is Andrew’);Readln;END.

write (‘My name’);write (‘ is Andrew’);

SPACE USED

04/20/23

16

USE OF WRITELN STATEMENT

With the use of the writeln statement for output, the compiler returns the cursor to the next line on screen.

PROGRAM TwoLines (output);BEGIN

writeln (‘My name’);writeln (‘is Andrew’);

END.

My name is Andrew

04/20/23 17

PROGRAM Song (output);

BEGINwrite (‘This is the land’);writeln (‘ of my birth!’);write (‘This is the land’);writeln (‘ of my birth!’);writeln (‘this is Jamaica, my Jamaica,’);writeln (‘this is the land of my birth.’);ReadlnEND.This is the land of my birth!This is the land of my birth!this is Jamaica, my Jamaica,this is the land of my birth.

Write a program in Pascal that displays the first stanza of the song “This is the land of my birth” by Eric Donaldson. Each line of output, should start on a new line.

PROGRAM Storing (output);

{Demonstrates use of variables}

VAR Length, Width, Area: integer;

BEGIN

Length := 152;

Width := 32;

Area := Length * Width;

writeln(‘Length = ‘, Length, ‘ Width = ‘, Width, ‘ Area = ‘ , Area);

Readln;

END.

04/20/23 18Length = 152 Width = 32 Area = 4864

Program that takes the value for Width as 32’ and Length 152’ of a Rectangle

and calculate the Area.

PROGRAM Storing (output);

{Demonstrates use of variables with assigned values};

VAR Length, Width, Area: integer;

BEGIN

Length:= 15;

Width:= 20;

Area:= Length * Width;

writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘ ‘Area);

Length:= 20;

Area:= Length * Width;

writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘ ‘Area);

END.

04/20/23 19

Output Length = 15 Width 20 Area = 250Length = 20 Width 20 Area = 400

PROGRAM Salary (output);

{Adds two salaries};

VAR Salary1, Salary2, Total: real;

BEGIN

Salary1:= 500.29;

Salary2:= 500.01;

Total:= Salary1 + Salary2;

writeln (‘Salary Week 1 =$’, Salary1, ‘Salary Week 2 =$‘, Salary2, ‘and Total Salary =$’, Total);END.

04/20/23 20

Salary Week 1 =$500.29, Salary Week 2 = $500.01, and Total Salary = $1000.30

Program that takes the value of two salary

amounts $500.29 and $500.01 and provide the sum of both stored in a

variable Total.

Write a program code in Pascal to take a time of .3 seconds and velocity of 24 and calculate the distance by use of the formula Time travelled times Velocity and output this distance on a new line.

04/20/23 21

PROGRAM Distance (output);

VAR Time, Velocity, Distance: real;

BEGINTime:= .3;Velocity:= 24;Distance:= Time * Velocity;writeln (‘Distance travelled is =‘, Distance);

END.

Distance Travelled = 7.2

PROGRAM Area (input, output);

VAR Length, Width: integer;

Cost, Price: real;

BEGIN

writeln (‘Type in Length, Width and Price’);

readln (Length, Width, Price);

Cost:= (Length * Width) * Price;

writeln (‘The cost is:=$ ‘, Cost);

END.

04/20/23 22

Write a program in Pascal to accept the Length and Width of a piece of lumber along with the price and provide the cost where Cost = Length x Width x Price. An output should be provided.

PROGRAM AreaVolume (input, output);

CONST

Pi = 3.14159; H = 6;

VAR

Volume, R: real;

BEGIN

writeln (‘Type in the radius.’);

readln (R);

Volume:= Pi * (R * R) * H;

writeln (‘Volume = ‘, Volume);

END.

04/20/23 23

Write a program in Pascal that calculates the volume of a shape provided with the Pi as 3.14159 and Height being 6. The program should accept Radius and calculate the volume where Volume =