0% found this document useful (0 votes)
4 views5 pages

3.8 Good Programming Habits: T C Q Exists

The document discusses good programming habits, emphasizing clarity over cleverness to facilitate easier code correction and modification. It provides guidelines for writing clear code, including the use of descriptive variable names, proper documentation, and code organization through comments and indentation. Additionally, it includes a series of exercises aimed at practicing programming concepts in R, such as defining functions, using loops, and implementing algorithms.

Uploaded by

alin186
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)
4 views5 pages

3.8 Good Programming Habits: T C Q Exists

The document discusses good programming habits, emphasizing clarity over cleverness to facilitate easier code correction and modification. It provides guidelines for writing clear code, including the use of descriptive variable names, proper documentation, and code organization through comments and indentation. Additionally, it includes a series of exercises aimed at practicing programming concepts in R, such as defining functions, using loops, and implementing algorithms.

Uploaded by

alin186
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/ 5

GOOD PROGRAMMING HABITS 43

3.8 Good programming habits

Good programming is clear rather than clever. Being clever is good, but given
a choice, being clear is preferable. The reason for this is that in practice much
more time is spent correcting and modifying programs than is ever spent
writing them, and if you are to be successful in either correcting or modifying
a program, you will need it to be clear.
You will find that even programs you write yourself can be very difficult to
understand after only a few weeks have passed.
We find the following to be useful guidelines: start each program with some
comments giving the name of the program, the author, the date it was written,
and what the program does. A description of what a program does should
explain what all the inputs and outputs are.
Variable names should be descriptive, that is, they should give a clue as to
what the value of the variable represents. Avoid using reserved names or func-
tion names as variable names (in particular t, c, and q are all function names
in R). You can find out whether or not your preferred name for an object is
already in use by the exists function.
Use blank lines to separate sections of code into related parts, and use in-
denting to distinguish the inside part of an if statement or a for or while
loop.
Document the programs that you use in detail, ideally with citations for spe-
cific algorithms. There is no worse feeling than returning to undocumented
code that had been written several years earlier to try to find and then ex-
plain an anomaly.

3.9 Exercises

1. Consider the function y = f (x) defined by


x ≤0 ∈ (0, 1] >1

f (x) −x3 x2 x

Supposing that you are given x, write an R expression for y using if state-
ments.
Add your expression for y to the following program, then run it to plot the
function f .
# input
x.values <- seq(-2, 2, by = 0.1)

# for each x calculate y


n <- length(x.values)
44 BASIC PROGRAMMING

8
6
y.values

4
2
0

−2 −1 0 1 2

x.values

Figure 3.2 The graph produced by Exercise 1.

y.values <- rep(0, n)


for (i in 1:n) {
x <- x.values[i]
# your expression for y goes here
y.values[i] <- y
}

# output
plot(x.values, y.values, type = "l")
Your plot should look like Figure 3.2. Do you think f has a derivative at
1? What about at 0?
We remark that it is possible to vectorise the program above, using the
ifelse function.
!n
2. Let h(x, n) = 1 + x + x2 + · · · + xn = i=0 xi . Write an R program to
calculate h(x, n) using a for loop.
3. The function h(x, n) from Exercise 2 is the finite sum of a geometric se-
quence. It has the following explicit formula, for x ̸= 1,
1 − xn+1
h(x, n) = .
1−x
EXERCISES 45
Test your program from Exercise 2 against this formula using the following
values
x n h(x, n)
0.3 55 1.428571
6.6 8 4243335.538178
You should use the computer to calculate the formula rather than doing it
yourself.
4. First write a program that achieves the same result as in Exercise 2 but
using a while loop. Then write a program that does this using vector
operations (and no loops).
If it doesn’t already, make sure your program works for the case x = 1.
5. To rotate a vector (x, y)T widdershins (anticlockwise) by θ radians, you
premultiply it by the matrix
" #
cos(θ) − sin(θ)
.
sin(θ) cos(θ)

Write a program in R that does this for you.


6. Given a vector x, calculate its geometric mean using both a for loop and
$n 1/n
vector operations. (The geometric mean of x1 , . . . , xn is ( i=1 xi ) .)
You might also like to have a go at calculating the harmonic mean,
!n −1
( i=1 1/xi ) , and then check that if the xi are all positive, the harmonic
mean is always less than or equal to the geometric mean, which is always
less than or equal to the arithmetic mean.
7. How would you find the sum of every third element of a vector x?
8. How does program quad2.r (Exercise 3.2.1) behave if a2 is 0 and/or a1 is
0? Using if statements, modify quad2.r so that it gives sensible answers
for all possible (numerical) inputs.
9. Chart the flow through the following two programs.
(a). The first program is a modification of the example from Section 3.6,
where x is now an array. You will need to keep track of the value of
each element of x, namely x[1], x[2], etc.
# program spuRs/resources/scripts/threexplus1array.r

x <- 3
for (i in 1:3) {
show(x)
if (x[i] %% 2 == 0) {
x[i+1] <- x[i]/2
} else {
x[i+1] <- 3*x[i] + 1
}
}
show(x)
46 BASIC PROGRAMMING
(b). The second program implements the Lotka–Volterra model for a
‘predator-prey’ system. We suppose that x(t) is the number of prey
animals at the start of a year t (rabbits) and y(t) is the number of
predators (foxes), then the Lotka–Volterra model is:
x(t + 1) = x(t) + br · x(t) − dr · x(t) · y(t);
y(t + 1) = y(t) + bf · dr · x(t) · y(t) − df · y(t);
where the parameters are defined by:
br is the natural birth rate of rabbits in the absence of predation;
dr is the death rate per encounter of rabbits due to predation;
df is the natural death rate of foxes in the absence of food (rabbits);
bf is the efficiency of turning predated rabbits into foxes.
# program spuRs/resources/scripts/predprey.r
# Lotka-Volterra predator-prey equations
br <- 0.04 # growth rate of rabbits
dr <- 0.0005 # death rate of rabbits due to predation
df <- 0.2 # death rate of foxes
bf <- 0.1 # efficiency of turning predated rabbits into foxes
x <- 4000
y <- 100
while (x > 3900) {
# cat("x =", x, " y =", y, "\n")
x.new <- (1+br)*x - dr*x*y
y.new <- (1-df)*y + bf*dr*x*y
x <- x.new
y <- y.new
}
Note that you do not actually need to know anything about the pro-
gram to be able to chart its flow.
10. Write a program that uses a loop to find the minimum of a vector x, without
using any predefined functions like min(...) or sort(...).
You will need to define a variable, x.min say, in which to keep the small-
est value you have yet seen. Start by assigning x.min <- x[1] then use
a for loop to compare x.min with x[2], x[3], etc. If/when you find
x[i] < x.min, update the value of x.min accordingly.
11. Write a program to merge two sorted vectors into a single sorted vector.
Do not use the sort(x) function, and try to make your program as efficient
as possible. That is, try to minimise the number of operations required to
merge the vectors.
12. The dice game craps is played as follows. The player throws two dice, and if
the sum is seven or eleven, then he wins. If the sum is two, three, or twelve,
then he loses. If the sum is anything else, then he continues throwing until
he either throws that number again (in which case he wins) or he throws a
seven (in which case he loses).
EXERCISES 47

0
y

−1

−2

−3

−3 −2 −1 0 1 2 3

Figure 3.3 The output from Exercise 13.

Write a program to simulate a game of craps. You can use the following
snippet of code to simulate the roll of two (fair) dice:
x <- sum(ceiling(6*runif(2)))

13. Suppose that (x(t), y(t)) has polar coordinates ( t, 2πt). Plot (x(t), y(t))
for t ∈ [0, 10]. Your plot should look like Figure 3.3.
14. Improve the code for program threexplus1.r as shown in Section 3.7.
Assume that the intermediary calls to show and cat are unnecessary.
15. A room contains 100 toggle switches, originally all turned off. 100 people
enter the room in turn. The first toggles every switch, the second toggles
every second switch, the third every third switch, and so on, to the last
person who toggles the last switch only.
At the end of this process, which switches are turned on?

You might also like