0% found this document useful (0 votes)
21 views37 pages

Class Work

Uploaded by

reyesvladimir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views37 pages

Class Work

Uploaded by

reyesvladimir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Problem Solving exercises for algorithms, flowcharts and Trace

tables
Practice questions Exercise
1 Write an algorithm and python code to print the value of sum1 if it is less than sum2.

Algorithm python
2 Write an algorithm and python code to print the smaller of the contents of two variables

Algorithm python
3. Write an algorithm and python code to read values into variables A & B. The algorithm should store the
smaller in A and the larger in B and then print A and B.

Algorithm python
4. Write an algorithm to read values in variables A & B and divide the larger number by the smaller number.
Algorithm python
5. Write an algorithm to read values into variables A, B, and C and print the smallest of the numbers.
6 Write an algorithm and python code to prompt the user for a number. If the number is greater than 45
the pseudocode should prompt the user for three additional numbers and find their sum otherwise it
should terminate.
7 Write an algorithm to read values into four variables A, B, C and D and print the smallest and the
largest of the four variables.

Algorithm python
# Step 1: Start

# Step 2: Read a value into variable A


A = float(input("Enter a value for A: "))

# Step 3: Read a value into variable B


B = float(input("Enter a value for B: "))

# Step 4: Read a value into variable C


C = float(input("Enter a value for C: "))

# Step 5: Read a value into variable D


D = float(input("Enter a value for D: "))

# Step 6: Set smallest = A


smallest = A

# Step 7: Set largest = A


largest = A

# Step 8: If B < smallest, then


if B < smallest:
# Step 9: Set smallest = B
smallest = B
# Step 10: If B > largest, then
elif B > largest:
# Step 10: Set largest = B
largest = B

# Step 11: If C < smallest, then


if C < smallest:
# Step 12: Set smallest = C
smallest = C
# Step 13: If C > largest, then
elif C > largest:
# Step 13: Set largest = C
largest = C
# Step 14: If D < smallest, then
if D < smallest:
# Step 15: Set smallest = D
smallest = D
# Step 16: If D > largest, then
elif D > largest:
# Step 16: Set largest = D
largest = D

# Step 17: Print "Smallest number:", smallest


print("Smallest number:", smallest)

# Step 18: Print "Largest number:", largest


print("Largest number:", largest)

# Step 19: Stop


8. Write an algorithm to read values into variables A, B and C. If the value in A is negative, the pseudocode
must print the sum of B and C ,otherwise the pseudocode must print the average of the three numbers.
Algorithm python

1. Start
2. Read “value into variable”, A
3. Read “value into variable”, B
4. Read “value into variable”, C
5. If A < 0, then
6. Set sum = B + C
7. Print sum
Else
8. Set average = (A + B + C) / 3
9. Print average
10. Stop
Main Menu

1. Print your name


2. Print your school name
3. Print your favorite color

Select option:
9 Write an algorithm to read a value into a variable of your choice. If the value entered is 1 the
pseudocode should print your name, if the value entered is 2 the pseudocode should print your school name,
if the value entered is 3 the pseudocode, it should print your favorite color, otherwise the program should
terminate.
Algorithm python
10. Write a program that converts the input Celsius degree into its equivalent Fahrenheit degree. Use the
formula: F = (9/5) *C+32.
Algorithm python

This code follows each step of the algorithm. The variables F and C are
initialized to 0 (step 2). The user is prompted to enter the temperature in
Celsius (step 3), and it is stored in the variable C. The code then converts
Celsius to Fahrenheit using the formula (1.8 * C) + 32 (step 4). Finally, the
converted temperature is printed to the console (step 5).
11. Write a program that converts the input dollar to its peso exchange rate equivalent. Assume that the
present exchange rate is 51.50 pesos against the dollar. Then display the peso equivalent exchange rate.
Algorithm python
12. Write a program that converts an input inch(es) into its equivalent centimeters. Take note that one inch is
equivalent to 2.54cms
Algorithm python

This code follows each step of the algorithm. The user is prompted to enter
the length in inches (step 2), and it is stored in the variable inch. The code
then calculates the equivalent length in centimeters using the formula 2.54 *
inch (step 3). Finally, the converted length is printed to the console (step 4).
Create an app to find a solution to the following problem.

Create a menu with 4 options

MENU
1. Converts Celsius degree into its equivalent Fahrenheit degree
2. Converts BZ dollar to Pesos
3. Converts an inch(es) into its equivalent centimeters
4. (0) to quit

Option:
13. Write a program that exchanges the value of two variables: x and y. The output must be: the value of
variable y will become the value of variable x, and vice versa.

Algorithm python

In this code, the user is prompted to enter the values of x and y (step 2),
which are then stored in the respective variables. The values of x and y are
swapped using a third variable z (step 3). Finally, the swapped values
of x and y are printed to the console (step 4).
14. Design a program to find the circumference of a circle. Use the formula: C=2πr, where π is approximately
equivalent 3.1416.
Algorithm python

In this code, we import the math module to access the value of pi. The user is
prompted to enter the radius (step 2), and it is stored in the variable r. The
code then calculates the circumference using the formula 2 * math.pi *
r (step 3). Finally, the calculated circumference is printed to the console (step
4).
14. Write a program that takes as input the purchase price of an item (P), its expected number of years of
service (Y) and its expected salvage value (S). Then outputs the yearly depreciation for the item (D). Use the
formula: D = (P - S) Y.
Algorithm python

In this code, the user is prompted to enter the values of P, S, and Y (steps 2, 3,
and 4). These values are stored in the respective variables. The code then
calculates D using the formula (P - S) * Y (step 5). Finally, the calculated value
of D is printed to the console (step 6).
15. Create a program to compute the volume of a sphere. Use the formula: V = (4/3) *pi*r3 where pi is equal
to 3.1416 approximately. The r is the radius of sphere. Display the result.

Algorithm python
PROBLEM SOLVING

Convert algorithms with the iteration


construct to python code
What does the following algorithm
prints?

SUM = 0
N = 20

WHILE N < 30 DO
SUM = SUM + N
N=N+3
Print n
ENDWHILE
Read X
For M = 1 to X do

Y=X–M
Z=5*Y–M
END

Print Z
Copy the following trace table. Complete the trace table, given that the number 4 is the input
value for X.
X=4

X M Y Z M=1
Read X
4 1 Y=0
For M = 1 to X do
4 8 Z=0

4
Y=X–M while M <= X:
4
Z=5*Y–M Y=X-M
END Z=5*Y-M

M += 1

Print Z
print(Z)

What does the algorithm prints? CXC 1996 Quest. 8


COUNT X
COUNT = 1
3. What is printed by the following algorithm? X = 2

COUNT = 1
while COUNT < 26:
X = X * 2
print(COUNT, X)
X=2
COUNT = COUNT + 5
WHILE COUNT < 26 DO
X=X*2
Print COUNT, X
COUNT = COUNT + 5
ENDWHILE

CXC June 1995 Quest 8


4 Copy and complete the following trace table below for the algorithm given:

X=5
X K SUM
X = 5
K = 10
K = 10
SUM = 45 5 10
SUM = 45

WHILE SUM < 75 DO 20 while SUM < 75:


5 SUM = SUM + K
SUM = SUM + K
print(K)
Print K
K = K + X
K=K+X
print(SUM)
ENDWHILE
Print SUM

What is printed by the algorithm? CXC 1998 Quest. 9


5. a. DIFFERENCE = 0
DIFFERENCE = 0
Input A, B
A = int(input("Enter the value for A: "))
IF A <=B THEN
B = int(input("Enter the value for B: "))

DIFFERENCE = A – B
if A <= B:
DIFFERENCE = A - B
else:
ELSE DIFFERENCE = B - A

DIFFERENCE = B – A print("DIFFERENCE:", DIFFERENCE)


ENDIF
Print DIFFERENCE

What does the algorithm above print if the input values are as stated in the table above?
CXC 1999 Quest 9 a
A B DIFFERENCE

20 30

100 100

50 10
Trace Table

dry run
5

a. This part is based on the algorithm given below:

A=3 Use the algorithm to complete


the following trace table.
B=5
A B SUM
SUM = 1
3 5 1
WHILE SUM <= 50 DO

A=A+B
B=B+A
SUM = SUM + B

ENDWHILE
Print SUM
STOP
b. State what the algorithm prints. CXC 2000 Quest. 8 b & c
2. Read Num
Num J X
J=1

WHILE J < Num DO

X = (J * 2) – 2
Print J
J=J+2
ENDWHILE

a. Given that 10 is the input value, create a trace table for the algorithm above using Num, J and X as table headings.

b. Given that 10 is the input value, determine the output of the algorithm.
CXC 2002 Jan.
3 Write an algorithm or program to read in three numbers and find the average of the numbers.
CXC 2002 June Quest 11

4. a. Copy and complete the following trace table, for the algorithm.

X=1
X Y Z
Y=2
1 2 3
Z=3
4

Z=Z+X
X=Z–X
Y=Z+Y
Z=Y–Z
Y=Y–X–Z
Print X, Y, Z
END

b. What is printed by the algorithm?


CXC 2002 June Quest. 14
5. a. Copy and complete the following trace table for the algorithm given. Note there are seven lines to be traced in the algorithm.

X=1 X Y Z
Y=2 1 2 3
Z=3

X=X+Y
Y=X*Y
Z=X+Y
X=X+Y
Y=X*Y
IF Z < X, THEN X = Z
IF Z > =X, THEN Y = X
Print X, Y, Z
END

b. What is printed by the algorithm? CXC Specimen 2002 Quest. 14 (6 marks)


6. Copy and complete the following trace table to show the final printed values of X, Y, and Z for the
algorithm given below.

X=3 X Y Z
Y=2 3 2 1
Z=1
DO WHILE Z > 0 THEN
X=X*Y
Y=Y+Z
Z=Z–1
LOOP
Print X, Y, Z
END

You might also like