Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

University College of the Caribbean Programming Techniques Lecture 4

Welcome to lecture 4. In this lecture we will examine the process of writing pseudocodes as well as the differences and similarities between flowcharts and pseudocodes.

Pseudocode Concepts Pseudocode is also known as false code. It provides a way of writing an algorithm in a way that is similar to that of writing actual programming statements. It is important to note however, that pseudocode will not execute within a programming language as it does not following the exact syntax (rules) of the language. (ie Pseudocode uses a loose syntax)

I/O operations (PRINT) To send output to the screen, the following terms are often used: Display, Print, Show or Write To display data exactly as it is typed, we use the double quotes eg Print Computer is Black this would display the enter phrase Computer is Black onto the screen We can also display the content of a variable eg assuming the variable name has the value Jane, then Print name would display Jane onto the screen The command Print name + -The Computer is Black would therefore now display the following on the screen: Jane The Computer is Black Assuming variable x has the value 5, and variable fn has the value Mary then what would we see on the screen given the following command?: Write My mother is + fn + and her age is + x Ans: My mother is Mary and her age is 5

1|P age

Prepared by Horrett Scarlett

University College of the Caribbean Programming Techniques Lecture 4


I/O operations (READ) The commands that enable the user to place a value inside a variable are as follows: Read, Enter, Accept, Input
Example: Declare n as Integer //creates an integer variable called n Print Enter a number: //displays Enter a number on screen Read n //allows user to enter an integer into variable n Print Two times + n + is + n x 2 //displays Two times 5 is 10 (assuming that user entered 5 into n)

We will now look at some problems as well as the pseudocode solutions for the problems. Several of the problems were already solved using flowcharts. Hence this exercise will enable you to compare the pseudocode solutions with the flowchart solutions so that you are better able to appreciate the similarities and the differences.

Pseudocode Examples and Solutions Problem 01 Write a pseudocode algorithm that will accept 2 numbers and will display the product of the two numbers with an appropriate message onto the screen

Solution 01
Declare x, y as Integer Print Enter num1: Read x Print Enter num1: Read y Print The product of + x + and + y + is + x * y

2|P age

Prepared by Horrett Scarlett

University College of the Caribbean Programming Techniques Lecture 4


Problem 02 Design a pseudocode solution that will accept a quantity measurement in feet (from the user) and that will convert it to inches and display the result on the screen (nb: 12 inches = 1 ft)

Solution 02
Declare quantityInFeet, quantityInInches as Integer Print Enter quantity in feet units: Read quantityInFeet quantityInInches = quantityInFeet * 12 Print The quanity + quantityInFeet + converted to inches is + quantityInInches

3|P age

Prepared by Horrett Scarlett

University College of the Caribbean Programming Techniques Lecture 4


Problem 03 Assuming that gct is calculated at 17.5%, design a solution that will enable the user to enter the price of an item. The solution should then display the total price that is to be paid as well as the amount money that is to be paid over as gct revenue to the government.

Solution 03
Declare price, gctRevenue, totalPrice as Float constant GCT = .165 Print Enter item price: Read price gctRevenue = price * GCT totalPrice = price +gctRevenue Print GCT revenue earned is + gctRevenue Print Total price to be paid is + totalPrice

The pseudocodes shown here all depict the use of the sequence control stucture

4|P age

Prepared by Horrett Scarlett

You might also like