Pseudocode Algorithms: Data, I/O, Variables, Arithmetic, Procedures, Functions, Lecture notes of Computer science

An overview of algorithms with pseudocode, focusing on data declaration, receiving input and sending output, initializing variables and computations, functions and procedures, and parameters. It includes examples of keywords and techniques used in pseudocode for data declaration, input/output, variable assignment, and arithmetic operations.

Typology: Lecture notes

2020/2021

Uploaded on 05/16/2021

don-tharindu
don-tharindu 🇱🇰

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming
LESSON [04] ALGORITHMS WITH PSEUDOCODE
Algorithms with Pseudocode UNIT1 - PROGRAMMING 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Pseudocode Algorithms: Data, I/O, Variables, Arithmetic, Procedures, Functions and more Lecture notes Computer science in PDF only on Docsity!

Programming

LESSON [04] – ALGORITHMS WITH PSEUDOCODE

Lesson Outline

 Data Declaration

 Receiving Input / Sending Output

 Initializing Variables / Computations

 Functions / Procedures

 Parameters and their roles

 Call by Value / Call by Reference

 Exercises

Receiving Input

There are some keywords that can be used in

pseudocode for receiving input

 INPUT: Receive data values from keyboard

 GET: Receive data values from keyboard

 READ: Receive data values from a file

Some Examples:

INPUT marks1, marks

READ Student_Record

GET Emp_Name

Sending Output

When the computer is ready with the results it

has to supply this information to the display

unit or any other output device or a file.

 DISPLAY: Send output to the screen

 PUT: Send output to the screen

 OUPUT: Send output to the screen

 PRINT: Send the output to the printer

 WRITE: Send out to a file

Some Examples:

DISPLAY “Total = “,sum

OUTPUT sum,average

WRITE Emp_Record

PRINT name,age

Performing Computer Arithmetic

The results of such computations also must be

stored in variables. Following keywords are

used for this purpose.

 COMPUTE: Perform any arithmetic and

store the result in a variable

 CALCULATE: Perform any arithmetic and

store the result in a variable

 ADD, MULTIPLY, DIVIDE, SUBTRACT: These

can also be used to perform a specific

arithmetic operation

Some Examples:

CALCULATE Sum = x + y

COMPUTE Avg as Sum / 2

ADD marks into Total

Sum = x + y

SUBTRACT loanAmt from Salary

Procedures and Functions Procedure: A unit of program that carries out some well-defined operation on data specify by parameter. It can be called from anywhere in the program and different parameters can be provided for each call. Function: The difference between a Procedure and the function is that the function will return a single value after called , where as a procedure do a particular task but not necessarily return a value.

2 Techniques of Parameter Passing Call by Value : Module receives a copy of the original data item as a parameter. Therefore changes made within the module doesn't alter original data. Call by Reference: Module receives a reference to the original data item itself. Therefore any change made within the module will alter original data item also.

Procedure Example

INPUT “Enter Height “, height INPUT “Enter Width “, width COMPUTE area = ½ * height * width DISPLAY “Area = “,area

Count = 1 Sum = 0 WHILE Count <= 10 Sum = Sum + Count Count = Count + 1 END WHILE DISPLAY “Total = “,Sum INPUT “Enter Limit “, Last Count = 1 Sum = 0 WHILE Count <= Last Sum = Sum + Count Count = Count + 1 END WHILE DISPLAY “Total = “,Sum

PROCEDURE Swap(First, Second) Hold = First First = Second Second = Hold END PROCEDURE INPUT “Enter 3 Numbers”, Left, Mid, Right IF Mid < Left THEN Call Swap (Left, Mid) END IF IF Right < Left THEN Call Swap (Left, Right) END IF IF Right < Mid THEN Call Swap (Mid, Right) END IF DISPLAY Left, Mid, Right

FUNCTION FindSum (First, Last) Tot = 0 FOR Num = First TO Last STEP 1 Tot = Tot + Sum END FOR RETRUN Tot END FUNCTION INPUT “Start & Stop”, N 1 , N 2 WHILE N 1 <> N 2 IF N 1 < N 2 THEN DISPLAY “Invalid Data” ELSE DISPLAY “Tot = “, FindSum(N 1 , N 2 ) END IF INPUT “Start & Stop”, N 1 , N 2 END WHILE

SUMMARY Today we learnt  Data Declaration  Receiving Input / Sending Output  Initializing Variables / Computations  Functions / Procedures  Parameters and their roles  Call by Value / Call by Reference  Exercises