Lab 01
Lab 01
Discussion
A. Explain the difference between using a computer program and programming a computer.
B. A toaster is a single-function device, but a computer can be programmed to carry out
different tasks. Is your cell phone a single-function device, or is it a programmable
computer? (Your answer will depend on your cell phone model.)
C. What are the characteristics of an algorithm that needs to be understood and executed by a
computer?
D. What is the relationship between the starting problem, the pseudocode, the computer
program, the flowchart, and the programming language?
Exercises
Part 1 – From problem to algorithm
Task: Asses whether the following problems are formulated in such a way that they can managed by
a computer program. If they are, write an algorithm (as pseudocode and/or flowchart) to solve
them. Otherwise, describe why they cannot be managed by a computer program. Complete at least
the first four exercises during the laboratory session and finish the remaining ones at home. It is not
required to write any Python code.
01.1.1 Scheduling. In a scheduling program, we want to check whether two appointments overlap.
For simplicity, appointments start at a full hour, and we use military time (with hours 0–23).
The following pseudocode describes an algorithm that determines whether the appointment
with starting time start1 and ending time end1 overlaps with the appointment with starting
time start2 and ending time end2.
1
07JCJLI Computer Science, 2022/23 Lab01
Check this algorithm with the following example values used as test cases: an appointment
from 10–12 and the second one from 11–13.
Second example: an appointment from 10–11 and one from 12–13. [R3.12]
01.1.2 Seasons. The following algorithm yields the season (Spring, Summer, Fall, or Winter) for a
given month and day.
If month is 1, 2 or 3
season = “Winter”
Else, if month is 4, 5 or 6
season = “Spring”
Else, if month is 7, 8 or 9
season = “Summer”
Else, if month is 10, 11 or 12
season = “Fall”
If month can be divided by 3 and day is >= 21
If season is “Winter”
season = “Spring”
Else, if season is “Spring”
season = “Summer”
Else, if season is “Summer”
season = “Fall”
Else
season = “Winter”
Draw the corresponding flowchart. Verify the correctness of the algorithm with a series of
test date. [R3.13]
01.1.3 On the road. You want to find out which fraction of your car’s use is for moving to work, and
which is for personal use. You know the one-way distance from your home to your work
place. For a particular period, you recorded the beginning and ending kilometers on the
odometer and the number of working days. Write an algorithm to settle this question.
[R1.16]
01.1.4 Checkboard tiling. Make a plan for tiling a rectangular bathroom floor with alternating black
and white tiles measuring 10 × 10 cm. The floor is of size 50 x 30 cm [Worked Example 1.1]
01.1.5 Future Decisions. A student that is about to finish his/her high school and wants to choose
which University apply to. They can base their decision on other people suggestions, and on
the mean salary obtainable one year after graduation. Most of the faculties they are
interested in offer statistics on the employment of grad students. How can they decide?
01.1.6 Know when to stop. The value of π can be computed according to the following formula:
2
07JCJLI Computer Science, 2022/23 Lab01
Write an algorithm to compute π. Because the formula is an infinite series and an algorithm
must stop after a finite number of steps, you should stop when you have the result
determined to six significant digits. [R1.18]
3
07JCJLI Computer Science, 2022/23 Lab01
return operating_cost
years = int(input("How many years will you drive this car? "))
km_per_year = int(input("On average, how many km will you drive each year? "))
fuel_cost = float(input("On average, how much dollars does it coast a liter of fuel? "))
4
07JCJLI Computer Science, 2022/23 Lab01
01.3.2 Write a program that prints the sum of the first ten positive integers, 1 + 2 + … + 10. [P1.2]
01.3.3 Write a program that prints the product of the first ten positive integers, 1 × 2 × … × 10. (Use
* to indicate multiplication in Python.) [P1.3]
01.3.4 Write a program that prints your name in large letters, such as:
[P1.6]
01.3.6 Write a program that prints the balance of an account after the first, second, and third year.
The account has an initial balance of $1,000 and earns 5 percent interest per year. [P1.4]
01.3.7 Write a program that displays your name inside a box on the screen, like this:
+------+
¦ Dave ¦
+------+
[P1.5]
01.3.8 Write a program that prints an animal speaking a greeting, similar to (but different from) the
following, without using the command cowsay:
[P1.10]
5
07JCJLI Computer Science, 2022/23 Lab01
01.3.10 A checkboard of size 5x5 where the white squares are indicated by a “0”, and the black
squares by a “1”.
01.3.13 The fourth element of the Fibonacci sequence, where every element is the sum of the two
preceding elements. The first two elements of the sequence are 0 and 1.
01.3.15 A closing phrase for this laboratory session, inside a box of your choosing, including the
computation of the percentage of exercises you completed.