CS 211 Term 1 Assignment PDF
CS 211 Term 1 Assignment PDF
QUESTION 1
(a) Pythagorean triples are sets of three integers that satisfy the Pythagorean
theorem. That is, integers a, b, and c such that a2 + b2 + c2. The integers 3, 4,
and 5 make up such a triple because 32 + 42 = 52. Formulas for generating
Pythagorean triples are a = m2 –n2, b= 2mn, and c = m2 + n2 where m and n are
positive integers such that m> n.
Write an interactive program that should allow the user to enter values for m
and n and then have the Pythagorean triple printed. [10 Marks]
(b) Write a complete program for the following structure chart for the Pizza
problem. [15 Marks]
1
QUESTION 2
Councils on the Copperbelt have established parking lots where customers are
charged K1000.00 each time people park vehicles in town center. There is no half
charge. Thus if someone has used the lot for less or more than an hour, the charge
would still be the same, K 1000.00.
You are required to develop a solution and write a program to assist the attendant.
The input consists of a starting and ending time. Output should be a statement to
the customer indicating the input information, the total amount due, a heading,
and a closing message. Sample output for the data.
First-level development for this problem should be module specifications for the
three main modules, a structure chart for the parking lot program and Pseudocode
for the same. [10 Marks]
The second-level: Here you are required to write a program that should print
statements for customers of the Council’s parking lot. Interactive input should
include entry time and exit time from the lot. Output should consist of a customer
statement, placing emphasis on using functions when developing the program.
[15 Marks]
QUESTION 3
(a) Write a program that reads two integers and prints them in the order of larger
first, smaller second. [10 Marks]
(b) Write a program that will do the following: read three integers from the
keyboard, add them to a previous total, print the numbers on one line, skip a
line (output) and print the new total. [15 Marks]
QUESTION 4
BP Service Station sells gasoline and has a car wash. Fees for the car wash are
K15,000.00 with a gasoline purchase of K20,000.00 or more and K20,000.00
otherwise. Three kinds of gasoline are available: regular at K3,900.00, unleaded at
K 4, 200.00, and super unleaded at K 4 600.00 per litter.
You are required to write a program that prints a statement for a customer. Input
consists of number of litters purchased, kind of gasoline purchased (R, U, S, or for
no purchase, N), and car wash desired (Y or N).
Develop a Pseudocode and a structure chart for the service station problem.
[10 Marks]
Write a program to computer the amount due from a customer of the service
station. [15 Marks]
2
QUESTION 5
(b) Recursion can be direct or indirect. It is direct when a function calls itself
and it is indirect recursion when a function calls another function that then
calls the first function. To illustrate solving a problem using recursion,
consider the Fibonacci series: - 1,1,2,3,5,8,13,21,34...
The way to solve this problem is to examine the series carefully. The first two
numbers are 1. Each subsequent number is the sum of the previous two numbers.
Thus, the seventh number is the sum of the sixth and fifth numbers. More
generally, the nth number is the sum of n - 2 and n - 1, as long as n > 2.
Recursive functions need a stop condition. Something must happen to cause the
program to stop recursing, or it will never end. In the Fibonacci series, n < 3 is a
stop condition. The algorithm to use is this:
If you call fib(1), it returns 1. If you call fib(2), it returns 1. If you call fib(3), it
returns the sum of calling fib(2) and fib(1). Because fib(2) returns 1 and fib(1)
returns 1, fib(3) will return 2. If you call fib(4), it returns the sum of calling fib(3)
and fib(2). We've established that fib(3) returns 2 (by calling fib(2) and fib(1)) and
that fib(2) returns 1, so fib(4) will sum these numbers and return 3, which is the
fourth number in the series. Taking this one more step, if you call fib(5), it will
return the sum of fib(4) and fib(3). We've established that fib(4) returns 3 and
fib(3) returns 2, so the sum returned will be 5.
Write a code that will implement the above algorithm. [15 marks]