Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
INTRODUCTION TO PROGRAMMING
LESSON 2
Introduction to Programming
Preface: Explain the importance of psuedocode algorithm and write basic algorithms
involving mathematical operations.
Psuedocode are written in a form, which looks like a combination of English and a Programming
Language. These are a technique for structured program design that uses English and Mathematical
statements to outline the logic of a program
The definitions of a Program and Algorithm sound alike because all programs are Algorithms. A
Program is simply an Algorithm that has been written for a computer using a given programming language.
An Algorithm may be looked at as a verbal or written description of a logical sequence of actions that is to
be carried out (by a computer).
When you start a car, you follow a step-by-step procedure. The Algorithm looks like this:
NB Without the phrase “but not more than 5 times” in Step 6, you could be trying to start the car forever
because if something is wrong with the car, you would be repeating Steps 3 – 6 over and over again and
the car would still not start. This kind of never-ending situation is an INFINITE LOOP. If we leave out
the phrase “but not more than 5 times” out of Step 6, the procedure does not fit the definition of an
Algorithm because an Algorithm must be terminated in a specified amount of time for all possible
conditions.
1
After an Algorithm is properly structured, the next step is to code it into a
Programming Language such as Pascal, Visual Basic, C++, Basic, Java, etc.
Repetition (LOOP)
WHILE Condition DO
IDENTIFIER An Identifier is the name invented by the Programmer for a data item. An
identifier can be the name of a variable or constant.
2
PARTS OF THE PSUEDOCODE
1. INPUT STATEMENT
The Input Statement is used to get data from outside of the computer via some input device into a
variable for manipulation by the Psuedocode.
Solution:Read A
Read B
END
TASK 1 Write a Psuedocode Algorithm to accept the variables for month, year, and date.
Solution:
2. ASSIGNMENT STATEMENT
The Assignment Statement is used to assign/give a value to or change the content (value) of a
variable. The Assignment Statement has two (2) parts – LVALUE and RVALUE.
The LVALUE refers to the variable as the storage location where the RVALUE will be stored. The
RVALUE refers to a value, which may the result of an expression or the content of another variable.
Solution A = 3090
(LVALUE) (RVALUE)
Mathematical Operators
* Multiplication / Division
+ Addition - Subtraction
Question Write a Psuedocode to read two (2) numbers and find their sum.
Solution Read A
Read B
SUM := A + B
END
3
TASK 2 Write an algorithm to accept two numbers and calculate their total and
average.
Solution:
TASK 3 Write an Algorithm to accept 3 numbers (A, B and C) and find their sum as D
and Average as AVG.
Solution:
TASK 4 Write an algorithm to accept two numbers and calculate their total and
average.
Solution:
TASK 5 Write an Algorithm that gives the correct results for the following: A+B/C.
Solution:
TASK 6 Write an Algorithm to accept the price of three products being Price 1, Price 2
and Price 3 and give the total of Price 2 and Price 3.
Solution:
TASK 7 Write an Algorithm to accept the Price and Rate for a service. Find the Cost,
which is derived from the formula Price times Rate divided by hundred.
Solution:
4
TASK 8 Write an Algorithm to accept the Height and Base of a triangle and calculate the
area.
Solution:
Solution:
Class Discussion What is wrong with the following Psuedocode used to interchange the
value of two variables A and B?
CA
BA
BC
TASK 9 Write a Psuedocode Algorithm to calculate the salary of a days worker. Input to
the Psuedocode are hours worked for the day and hourly rate.
Solution:
TASK 10 Write Psuedocode to read in 3 numbers and find their product, sum and average
Solution:
5
3. OUTPUT STATEMENT
The Output Statement is used to get information to the Programmer or computer user. Output can take
the form of sound, text, voice, graphics, etc. For output, use the statement Print.
1. Information between double quotes will be printed as is. E.g. „Sum of numbers is‟, SUM.
2. When a variable is used with the print statement, the content of the variable is printed. E.g. Print Total
3. Variable not included between quotation mark but separated by a comma. E.g. Print A, B, C.
TASK 11 Write a Psuedocode Algorithm to calculate the cost of a product given the
Quantity, and the Unit Price. The Cost, Quantity and Unit Price must be printed.
Solution:
TASK 12 Write Psuedocode which prompts the user to enter the price of an item and which
calculates and print the new price after a 12% discount.
Solution:
6
INFORMATION TECHNOLOGY
INTRODUCTION TO PROGRAMMING
LESSON 2 HOMEWORK A
Fundamentals of Programming
1. Write a Psuedocode Algorithm that prompts the user to enter three numbers as variables and find
and print their sum, mean and product of the first two numbers. (5 marks)
2. Create the Psuedocode to calculate the area and perimeter of a triangle. Display the perimeter and
area of the triangle. (5 marks)
3. Create a suitable Psuedocode Algorithm to read in two values A and B and find and print their
difference, the sum and average of the two variables inputted. (5 marks)
4. Write a Psuedocode Algorithm to accept the Unit Price of an article, the quantity purchased and
calculate the cost before tax. Your algorithm should also calculate the final cost after tax is
added. The Tax Rate is the same as the current GCT amount. Display and print the Quantity, the
Unit Cost, the Cost Before Tax and Cost After Tax. (5 marks)
b. Modify the algorithm above including a discount of 10% off the cost of the ticket.
Calculate the new cost of the ticket after the discount. (5 marks)
TOTAL MARKS 25