Sei sulla pagina 1di 7

INFORMATION TECHNOLOGY

INTRODUCTION TO PROGRAMMING

LESSON 2
Introduction to Programming
Preface: Explain the importance of psuedocode algorithm and write basic algorithms
involving mathematical operations.

Psuedocode remains to be a popular form of describing algorithms because it is easy to write,


understand, and translate to the actual programming language being used to write the program. Psuedocode
uses keywords and control structures similar to those used in programming languages but without the strict
rules of Programming Language (Syntax).

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

An Algorithm is a sequence of steps designed to perform a particular task. Algorithms may


also be considered as a step-by-step procedure for solving a problem in a specified amount of time.

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:

1. Insert the key


2. Make sure the gear stick is in park or Neutral
3. Press down on the Gas Pedal
4. Turn the key to the start position
5. If the engine starts within 6 seconds, release the key to the ignition position
6. If the engine does not start in six seconds, release the key, wait ten seconds, and repeat steps 3 – 6, but
not more than five (5) times.
7. If the car does not start, call the garage

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.

Parts of a Psuedocode Algorithm


1. Input Statements
2. Output Statements
3. Assignment Statements
4. Control Statements (Sequence, Repetition, Selection)

1. Selection Condition True Statement 1


False Statement 2

IF Condition THEN Statement 1 ELSE Statement 2

Diagram of a Selection/Branch/Decision Control Statement


2. Repetition/Loop Condition False
True Statement 1

Repetition (LOOP)

WHILE Condition DO

Diagram of a Repetition (Loop) Control Statement


3. Sequence Statement 1 Statement 2, etc

Diagram of a Sequence Statement

Definition of Important Terms

VARIABLE A variable is a named quantity in Psuedocode whose value can be changed.

NB The value of a variable can be changed by an Input Statement can


changed.

CONSTANT A Constant is a value (OR VARIABLE) in a Psuedocode that represents a


value that does not change.

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.

Question: Write a Psuedocode to read in two numbers into variables A and B.

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.

Question Write an Assignment Statement to store the value of 3090 as A.

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:

Question Write a Psuedocode to interchange the values of two variables A and B.

Solution:

Class Discussion What is wrong with the following Psuedocode used to interchange the
value of two variables A and B?

CA
BA
BC
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.

We will print under the following circumstances:

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)

5. a. Write a Psuedocode Algorithm to accept the name of a person who is to travel to an


overseas country, their chosen destination and the cost of their ticket. (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

Potrebbero piacerti anche