You are on page 1of 16

Sep 21, 2021

Instruction: Complete a defining diagram and write a pseudocode algorithm to solve each of
the following:

1. Write a pseudocode algorithm to read three numbers and find their products and
average and print the sum, average and product.

INPUT PROCESS OUTPUT

Num1, num2, num3 Sum ← num1 + num2 + Sum


num3 Average
Product
Avg ← sum/3

Prod ← num1 * num2 *


num3

Alt Alt
Alt
Three numbers Calculate sum by adding Sum
all three numbers Average
Product
Calculate average by
dividing sum by three

Calculate product by
multiplying all three
numbers
BEGIN

PRINT “ Please enter 3 numbers”

READ num1, num2, num3

Sum ← num1 + num2 + num3

Avg ← sum/3

Prod ← num1*num2*num3

PRINT “Your sum is”, Sum

PRINT ”Your product is”, product

PRINT “Your average is”, avg

ENDq

2. Write a pseudocode algorithm to accept the current year and the year a person was
born. Calculate and print the age of the person.

INPUT PROCESS OUTPUT

current_year Age ← current_year - Age


birth_year birth_year

BEGIN
PRINT “Please enter the current year and your birth year”
READ current_year, birth_year
Age ← current_year - birth_year
PRINT “You are”, Age, “years old”
END

3. In groups, create a problem statement for any given problem. Write a pseudocode algorithm
to solve the problem.
IF Statements

1. A company gives out bonuses based on the amount of income generated by their sales
representatives per month. Once the income is greater than $50,000.00, a bonus of 10%
of the generated income is given to the employees. Read the income generated and
print the bonus.

BEGIN

CONSTANT

BonusPercent = 0.1

DECLARE

Income, bonus AS REAL

PRINT “Please enter sales rep income”

READ income

IF income > 50000.00 THEN

Bonus ← income * BonusPercent

ENDIF

PRINT “The sales rep bonus is”, Bonus

END
2. A company gives out bonuses based on the amount of income generated by their sales
representatives per month. Once the income is greater than $50,000.00, a bonus of 10%
of the generated income is payable; otherwise the bonus is 3% of the generated income.
Read the income generated and print the bonus.

BEGIN

CONSTANT

BonusPercent_1 = 0.1

BonusPercent_2 = 0.03

DECLARE

Sr_income, bonus AS REAL

PRINT “Please enter your income.”

READ Sr_income

IF sr_income > 50000.00 THEN

Bonus ← sr_income * BonusPercent_1

ELSE

Bonus ← sr_income * BonusPercent_2

ENDIF

PRINT “Your bonus is”, Bonus

END
3. Write a pseudocode algorithm to accept the name of a student, subject, score on test,
and the amount the test was marked out of. Calculate the percentage grade the student
received. The algorithm should also determine the letter grade based on the following:

•Grade Letter Grade


•90 – 100 A
•80 – 89 B
•70 – 79 C
•60 – 69 P
•< 60 F
The algorithm should print the student’s name, subject, percentage grade and letter grade.

BEGIN
DECLARE
Student_name, subject AS STRING
percentage_grade AS REAL
Total_marks, Test_score AS INTEGER
Letter_grade AS CHAR

PRINT “Please enter Student name, subject, Test score, Total marks”
READ Student_name, subject, Test_score, Total_marks

Percentage_grade ← (test_score/total_marks) *100

IF Percentage_grade >= 90 THEN


Letter_grade ← “A”
ELSE
IF Percentage_grade >= 80 THEN
Letter_grade ← “B”
ELSE
IF Percentage_grade >= 70 THEN
Letter_grade ← “C”
ELSE
IF Percentage_grade >= 60 THEN
Letter_grade ← “P”
ELSE
Letter_grade ← “F”
ENDIF
ENDIF
ENDIF
ENDIF
PRINT “The student name is”, Student_name

PRINT “The subject is”, subject

PRINT “The percentage grade is”, Percentage_grade

PRINT “The letter grade is”, Letter_grade

END

Loops

Categories:

- DEFINITE (Runs for a specific number of times - FOR)


- INDEFINITE (Runs for an unspecified number of times - WHILE-DO,
REPEAT-UNTIL)

FOR Construct

FOR counter ← starting value TO ending value DO

Statements to be repeated

ENDFOR

Example

FOR counter ← 1 TO 20 DO

PRINT “Hello world!”

ENDFOR

Practice Question

(1) Write an algorithm to read ELEVEN numbers, find their average and print it.
The algorithm should also print the number of times the number 6 occurs in
the data. For example, given the input data:

4 6 9 6 5 6 10 7 0 16

The algorithm should print 7 as the average and 3 as the number of times 6
occurs.

BEGIN

Declare
Num1, num2, num3, sum, product, difference As integer

Average As real

Choice As string

PRINT “Please enter three numbers”

READ Num1, Num2, Num3

Print “Enter a choice you wish to execute: sum, avg, prod or diff”

Read choice

IF choice = “sum” Then

Sum ← Num1 + Num2 + Num3

Print “ Sum is”, sum

Else

IF choice = “avg” Then

Average ← (num1 + num2 + num3)/3

Print “Average is”, Average

Else

IF choice = “prod” Then

Product ← Num1 * Num2 * Num3

OUTPUT “The product is”, Product

Else

IF choice = “diff” Then

Difference ← num1 - num2 - num3

Print “The difference is”, difference

Else

Print “That was not a valid option”


ENDIF

ENDIF

ENDIF

ENDIF

END

Q. A car rental company leases 4 cars in one day.


Read the number of days for lease of each car, and
the name of each customer. Calculate the amount
each customer should pay, and the total rent paid to
the company if a car is leased for 2500. Print the
name, total the customer should pay and the total
rent paid to the rental company.

Pascal - WHILE - DO

WHILE condition DO
BEGIN
Statements;
END; // end of while loop

WHILE condition DO
Statement; // end of while loop
REPEAT
Statements;
UNTIL Condition;

BEGIN

DECLARE

QUANTITY AS INTEGER

UNIT_PRICE, COST AS REAL

Res AS Char

Res ← ‘Y’

WHILE res = ‘Y’ DO

PRINT “please enter the quantity and the unit price of the product”

READ QUANTITY

READ UNIT_PRICE

COST ← QUANTITY*UNIT_PRICE

PRINT “the quantity of the product is”, QUANTITY

PRINT “the unit price of the product is”, UNIT_PRICE

PRINT “the cost of the product is”, COST

PRINT “ Would you like to enter another product? Y/N”

READ res

ENDWHILE

END
BEGIN

CONSTANT

VIP= 5000

Regular = 2000

DECLARE

num_of_reg_tickets, num_of_vip_tickets, age AS INTEGER

Res, Ticket AS CHAR

Reg_revenue, VIP_revenue, Total_revenue AS REAL

Num_of_reg_tickets ← 0

Num_of_vip_tickets ← 0

Res ← “Y”

WHILE Res = “Y” DO

REPEAT

PRINT “How old are you”

READ Age

UNTIL Age >= 18

REPEAT

WRITE "Please enter the type of ticket (R or V)."

READ Ticket

IF Ticket = “R” THEN

num_of_reg_tickets <-- num_of_reg_tickets + 1

ELSE

IF Ticket = “V” THEN


num_of_vip_tickets <-- num_of_vip_tickets + 1

ELSE

PRINT “Not a valid ticket entry”

ENDIF

ENDIF

UNTIL (Ticket = “R”) OR (Ticket = “V”)

PRINT “Entering another patron? Y/N”

READ Res

ENDWHILE

Reg_revenue ← num_of_reg_tickets * Regular

VIP_revenue ← num_of_vip_tickets * VIP

Total_revenue <-- Reg_revenue + VIP_revenue

WRITE "The revenue for regular tickets is", Reg_revenue

WRITE "The revenue for VIP tickets is", VIP_revenue

WRITE "The total revenue is", Total_revenue

END
Arrays - Pascal

Declaration

VAR

name: Array[1..5] OF STRING;

BEGIN

PRODUCT← 1
FOR X ← 1 TO 10 DO
PRINT “Enter a number”
READ num[X]
SUM ← SUM + num[X]

PRODUCT ← PRODUCT*num[X]
ENDFOR
AVG← SUM/10
PRINT “the sum is”, SUM
PRINT “the product is”, PRODUCT
PRINT “the average is”, AVG
PRINT “The numbers entered are:”
FOR X ← 1 TO 10 DO
PRINT num[x]
ENDFOR
END
Q. Write a program to accept the names and ages of 10 patients to see a pediatrician. Only
children from 0 to 17 are allowed to see the doctor. If the user enters an age outside of this
range, the program should force the user to enter another age. In the end, the program should
print the average age of the children and all the names and ages entered by the user.

FOR x ← 1 to 10 DO
Print “enter a name and age of a person”
Read name[x], age[x]

Totalage ← Totalage + age[x]

ENDFOR
PROCEDURES - A distinct portion of a program that
carries out a set of tasks and returns control to the main part
of the program.

Outline

Procedure name;
Var
Local variables (variable that can only be used in this
section of the program)

BEGIN
Statements;

END; // End of procedure

Procedure Welcome_Screen;
BEGIN
Writeln (‘********************Welcome*************************’);

END;

Procedure Get_data
Declare
X As integer
BEGIN
SUM ← 0
PRODUCT← 1
FOR X ← 1 TO 10 DO
PRINT “Enter a number”
READ num[X]
SUM ← SUM + num[X]

PRODUCT ← PRODUCT*num[X]
ENDFOR
AVG← SUM/10

END

PROCEDURE display_Results;
BEGIN
PRINT “the sum is”, SUM
PRINT “the product is”, PRODUCT
PRINT “the average is”, AVG
END

PROCEDURE DisplayALLNum
BEGIN
PRINT “The numbers entered are:”
FOR i ← 1 TO 10 DO
PRINT num[i]
ENDFOR
END

BEGIN
DECLARE
Array num[10] as Integer
SUM, product, i as INTEGER
AVG as REAL
Welcome_Screen;
Get_Data;
Display_results;
DisplayALLNum;
END.

You might also like